From d1899bb6436a6de095fb4f2fd7606d73d2cdfa7d Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Wed, 9 Mar 2016 09:21:23 +0100 Subject: [PATCH] [auto_backup] Refactor. - Follow template README. - Remove HTML README. - Move models to models folder. - Model and view file names follow guidelines. - Unused methods cleanup. - Remove unneeded `.pot` file. - Fix permissons. - Follow PEP8 in names everywhere. - Set more descriptive field names. - Disable backups for other databases, for security. - Remove db name from generated file, for easier cleanup. - EAFP logic everywhere. - More descriptive name. - Data files moved to YAML, with cleaner ir.cron record creation. - Add permissions for db.backup model. - Icons. - Update tests with new format. - Storage method is a selectable, for easier extensibility. - Instead of custom mailing, it just has a mail thread where you can subscribe. - Should fix almost all comments in https://github.com/OCA/server-tools/pull/203. --- auto_backup/README.rst | 42 +-- auto_backup/__init__.py | 3 +- auto_backup/__openerp__.py | 14 +- auto_backup/data/backup_data.xml | 18 - auto_backup/data/backup_data.yml | 28 ++ auto_backup/i18n/auto_backup.pot | 141 ------- auto_backup/model/backup_scheduler.py | 376 ------------------- auto_backup/{model => models}/__init__.py | 2 +- auto_backup/models/db_backup.py | 282 ++++++++++++++ auto_backup/security/ir.model.access.csv | 3 +- auto_backup/static/description/icon.png | Bin 0 -> 6074 bytes auto_backup/static/description/icon.svg | 51 +++ auto_backup/static/description/no_index.html | 100 ----- auto_backup/tests/test_auto_backup.py | 37 +- auto_backup/view/bkp_conf_view.xml | 100 ----- auto_backup/view/db_backup_view.xml | 94 +++++ 16 files changed, 501 insertions(+), 790 deletions(-) delete mode 100644 auto_backup/data/backup_data.xml create mode 100644 auto_backup/data/backup_data.yml delete mode 100644 auto_backup/i18n/auto_backup.pot delete mode 100644 auto_backup/model/backup_scheduler.py rename auto_backup/{model => models}/__init__.py (96%) create mode 100644 auto_backup/models/db_backup.py create mode 100644 auto_backup/static/description/icon.png create mode 100644 auto_backup/static/description/icon.svg delete mode 100644 auto_backup/static/description/no_index.html delete mode 100644 auto_backup/view/bkp_conf_view.xml create mode 100644 auto_backup/view/db_backup_view.xml diff --git a/auto_backup/README.rst b/auto_backup/README.rst index 769f80059..c9c315054 100644 --- a/auto_backup/README.rst +++ b/auto_backup/README.rst @@ -1,40 +1,40 @@ .. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg - :alt: License: AGPL-3 + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 -================= -Automated backups -================= +==================== +Database Auto-Backup +==================== A tool for all your back-ups, internal and external! Installation ============ -Before to install this module, you need to: +Before installing this module, you need to execute:: -instal pysftp via pip. + pip install pysftp Configuration ============= -Go to Settings -> Configuration -> Configure Backup +Go to *Settings -> Configuration -> Configure Backup* to create your configurations for each database that you needed to backups. Usage ===== - Keep your Odoo data safe with this module. Take automated back-ups, remove them automatically and even write them to an external server through an encrypted tunnel. You can even specify how long local backups and external backups should be kept, automatically! - Connect with an FTP Server -------------------------- -#### Keep your data safe, through an SSH tunnel! +Keep your data safe, through an SSH tunnel! +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Want to go even further and write your backups to an external server? You can with this module! Specify the credentials to the server, specify @@ -45,7 +45,8 @@ safe! Test connection --------------- -#### Checks your credentials in one click +Checks your credentials in one click +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Want to make sure if the connection details are correct and if Odoo can automatically write them to the remote server? Simply click on the ‘Test @@ -55,16 +56,15 @@ everything is OK, or what is wrong! E-mail on backup failure ------------------------ -#### Stay informed of problems, automatically! - -Do you want to know if the database backup failed? Check the checkbox 'Auto. E-mail on backup fail' and fill in your e-mail. -Every time a backup fails you will get an e-mail in your mailbox with technical details. +Stay informed of problems, automatically! +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Do you want to know if the database backup succeeded or failed? Subscribe to +the corresponding backup setting notification type. -Known issues / Roadmap -====================== - -* ... +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/149/8.0 Bug Tracker =========== @@ -74,7 +74,6 @@ In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed feedback `here `_. - Credits ======= @@ -83,6 +82,7 @@ Contributors * Yenthe Van Ginneken * Alessio Gerace +* Jairo Llopis Maintainer ---------- @@ -97,4 +97,4 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -To contribute to this module, please visit http://odoo-community.org. \ No newline at end of file +To contribute to this module, please visit https://odoo-community.org. diff --git a/auto_backup/__init__.py b/auto_backup/__init__.py index a8f54341e..c201f7b03 100644 --- a/auto_backup/__init__.py +++ b/auto_backup/__init__.py @@ -20,5 +20,4 @@ # ############################################################################## -from . import model -from . import tests +from . import models diff --git a/auto_backup/__openerp__.py b/auto_backup/__openerp__.py index 72f4389b7..9f465f04b 100644 --- a/auto_backup/__openerp__.py +++ b/auto_backup/__openerp__.py @@ -22,22 +22,26 @@ { "name": "Database Auto-Backup", + "summary": "Backups database", "version": "8.0.1.0.0", "author": ( "VanRoey.be - Yenthe Van Ginneken, Agile Business Group," + " Grupo ESOC Ingeniería de Servicios," " Odoo Community Association (OCA)" ), 'license': "AGPL-3", "website": "http://www.vanroey.be/applications/bedrijfsbeheer/odoo", "category": "Tools", - "summary": "Backups data base", "depends": ['email_template'], "demo": [], "data": [ - "view/bkp_conf_view.xml", - "data/backup_data.xml", - "security/ir.model.access.csv" + "data/backup_data.yml", + "security/ir.model.access.csv", + "view/db_backup_view.xml", ], "application": True, - "installable": True + "installable": True, + "external_dependencies": { + "python": ["pysftp"], + }, } diff --git a/auto_backup/data/backup_data.xml b/auto_backup/data/backup_data.xml deleted file mode 100644 index 8911a3bd6..000000000 --- a/auto_backup/data/backup_data.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/auto_backup/data/backup_data.yml b/auto_backup/data/backup_data.yml new file mode 100644 index 000000000..3b6b4bc29 --- /dev/null +++ b/auto_backup/data/backup_data.yml @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +# Cron job +- !record {model: ir.cron, id: ir_cron_backupscheduler0}: + name: Backup scheduler + user_id: base.user_root + interval_number: 1 + interval_type: days + numbercall: -1 + nextcall: !eval + (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d 02:00:00") + model: db.backup + function: action_backup_all + +# New message subtypes +- !record {model: mail.message.subtype, id: success}: + name: Backup successful + res_model: db.backup + default: False + description: Database backup succeeded. + +- !record {model: mail.message.subtype, id: failure}: + name: Backup failed + res_model: db.backup + default: True + description: Database backup failed. diff --git a/auto_backup/i18n/auto_backup.pot b/auto_backup/i18n/auto_backup.pot deleted file mode 100644 index c560ff6ee..000000000 --- a/auto_backup/i18n/auto_backup.pot +++ /dev/null @@ -1,141 +0,0 @@ -# Translation of OpenERP Server. -# This file contains the translation of the following modules: -# * auto_backup -# -msgid "" -msgstr "" -"Project-Id-Version: OpenERP Server 5.0.6\n" -"Report-Msgid-Bugs-To: support@openerp.com\n" -"POT-Creation-Date: 2009-11-24 13:49:51+0000\n" -"PO-Revision-Date: 2009-11-24 13:49:51+0000\n" -"Last-Translator: <>\n" -"Language-Team: \n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=UTF-8\n" -"Content-Transfer-Encoding: \n" -"Plural-Forms: \n" - -#. module: auto_backup -#: help:db.backup,name:0 -msgid "Database you want to schedule backups for" -msgstr "" - -#. module: auto_backup -#: constraint:ir.model:0 -msgid "The Object name must start with x_ and not contain any special character !" -msgstr "" - -#. module: auto_backup -#: constraint:ir.actions.act_window:0 -msgid "Invalid model name in the action definition." -msgstr "" - -#. module: auto_backup -#: model:ir.model,name:auto_backup.model_db_backup -msgid "db.backup" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "1) Go to Administration / Configuration / Scheduler / Scheduled Actions" -msgstr "" - -#. module: auto_backup -#: model:ir.actions.act_window,name:auto_backup.action_backup_conf_form -#: model:ir.ui.menu,name:auto_backup.backup_conf_menu -msgid "Configure Backup" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "Test" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "IP Configuration" -msgstr "" - -#. module: auto_backup -#: help:db.backup,bkp_dir:0 -msgid "Absolute path for storing the backups" -msgstr "" - -#. module: auto_backup -#: model:ir.module.module,shortdesc:auto_backup.module_meta_information -msgid "Database Auto-Backup" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "Database Configuration" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "4) Set other values as per your preference" -msgstr "" - -#. module: auto_backup -#: field:db.backup,host:0 -msgid "Host" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "Automatic backup of all the databases under this can be scheduled as follows: " -msgstr "" - -#. module: auto_backup -#: constraint:ir.ui.view:0 -msgid "Invalid XML for View Architecture!" -msgstr "" - -#. module: auto_backup -#: field:db.backup,bkp_dir:0 -msgid "Backup Directory" -msgstr "" - -#. module: auto_backup -#: field:db.backup,name:0 -msgid "Database" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "2) Schedule new action(create a new record)" -msgstr "" - -#. module: auto_backup -#: model:ir.module.module,description:auto_backup.module_meta_information -msgid "The generic Open ERP Database Auto-Backup system enables the user to make configurations for the automatic backup of the database.\n" -"User simply requires to specify host & port under IP Configuration & database(on specified host running at specified port) and backup directory(in which all the backups of the specified database will be stored) under Database Configuration.\n" -"\n" -"Automatic backup for all such configured databases under this can then be scheduled as follows: \n" -" \n" -"1) Go to Administration / Configuration / Scheduler / Scheduled Actions\n" -"2) Schedule new action(create a new record)\n" -"3) Set 'Object' to 'db.backup' and 'Function' to 'schedule_backup' under page 'Technical Data'\n" -"4) Set other values as per your preference" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "3) Set 'Object' to 'db.backup' and 'Function' to 'schedule_backup' under page 'Technical Data'" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "Help" -msgstr "" - -#. module: auto_backup -#: view:db.backup:0 -msgid "This configures the scheduler for automatic backup of the given database running on given host at given port on regular intervals." -msgstr "" - -#. module: auto_backup -#: field:db.backup,port:0 -msgid "Port" -msgstr "" - diff --git a/auto_backup/model/backup_scheduler.py b/auto_backup/model/backup_scheduler.py deleted file mode 100644 index 479023ff5..000000000 --- a/auto_backup/model/backup_scheduler.py +++ /dev/null @@ -1,376 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2009 Tiny SPRL (). All Rights Reserved -# Copyright 2015 Agile Business Group -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -############################################################################## -import socket -import os -import time -import datetime -import re -from openerp import models, fields, api, _ -from openerp.exceptions import except_orm, Warning as UserError -from openerp import tools -from openerp.service import db -import logging -_logger = logging.getLogger(__name__) -try: - import pysftp -except ImportError: - _logger.debug('Can not import pysftp') - - -def execute(connector, method, *args): - res = False - try: - res = getattr(connector, method)(*args) - except socket.error as e: - raise e - return res - - -class DbBackup(models.Model): - _name = 'db.backup' - - @api.model - def _get_db_name(self): - return self.env.cr.dbname - - name = fields.Char( - string='Database', size=100, required=True, - default=_get_db_name, - help='Database you want to schedule backups for' - ) - - bkp_dir = fields.Char( - string='Backup Directory', size=100, - default='/odoo/backups', - help='Absolute path for storing the backups', - required=True - ) - autoremove = fields.Boolean( - string='Auto. Remove Backups', - help=( - "If you check this option you can choose to " - "automaticly remove the backup after xx days" - ) - ) - daystokeep = fields.Integer( - string='Remove after x days', - default=30, - help=( - "Choose after how many days the backup should be " - "deleted. For example:\nIf you fill in 5 the backups " - "will be removed after 5 days." - ), required=True - ) - sftpwrite = fields.Boolean( - string='Write to external server with sftp', - help=( - "If you check this option you can specify the details " - "needed to write to a remote server with SFTP." - ) - ) - sftppath = fields.Char( - string='Path external server', - help=( - "The location to the folder where the dumps should be " - "written to. For example /odoo/backups/.\nFiles will then" - " be written to /odoo/backups/ on your remote server." - ) - ) - sftpip = fields.Char( - string='IP Address SFTP Server', - help=( - "The IP address from your remote" - " server. For example 192.168.0.1" - ) - ) - sftpport = fields.Integer( - string="SFTP Port", - default=22, - help="The port on the FTP server that accepts SSH/SFTP calls." - ) - sftpusername = fields.Char( - string='Username SFTP Server', - help=( - "The username where the SFTP connection " - "should be made with. This is the user on the external server." - ) - ) - sftppassword = fields.Char( - string='Password User SFTP Server', - help=( - "The password from the user where the SFTP connection " - "should be made with. This is the password from the user" - " on the external server." - ) - ) - daystokeepsftp = fields.Integer( - string='Remove SFTP after x days', - default=30, - help=( - "Choose after how many days the backup should be deleted " - "from the FTP server. For example:\nIf you fill in 5 the " - "backups will be removed after 5 days from the FTP server." - ) - ) - sendmailsftpfail = fields.Boolean( - string='Auto. E-mail on backup fail', - help=( - "If you check this option you can choose to automaticly" - " get e-mailed when the backup to the external server failed." - ) - ) - emailtonotify = fields.Char( - string='E-mail to notify', - help=( - "Fill in the e-mail where you want to be" - " notified that the backup failed on the FTP." - ) - ) - lasterrorlog = fields.Text( - string='E-mail to notify', - help=( - "Fill in the e-mail where you want to be" - " notified that the backup failed on the FTP." - ) - ) - - @api.multi - def _check_db_exist(self): - for rec in self: - db_list = db.exp_list(True) - if rec.name in db_list: - return True - return False - - _constraints = [ - ( - _check_db_exist, - _('Error ,No such database exists'), ['name'] - ) - ] - - @api.multi - def test_sftp_connection(self): - confs = self.search([]) - # Check if there is a success or fail and write messages - messageTitle = "" - messageContent = "" - conn_success = False - for rec in confs: - try: - conn_success = True - ipHost = rec.sftpip - portHost = rec.sftpport - usernameLogin = rec.sftpusername - passwordLogin = rec.sftppassword - # Connect with external server over SFTP, so we know sure that - # everything works. - srv = pysftp.Connection(host=ipHost, username=usernameLogin, - password=passwordLogin, port=portHost) - srv.close() - # We have a success. - messageTitle = _("Connection Test Succeeded!") - messageContent = _( - "Everything seems properly set up for FTP back-ups!") - except Exception as e: - conn_success = False - messageTitle = _("Connection Test Failed!") - if len(rec.sftpip) < 8: - messageContent += _( - "\nYour IP address seems to be too short.\n") - messageContent += _("Here is what we got instead:\n") - if not conn_success: - raise except_orm( - _(messageTitle), _( - messageContent + "%s") % - tools.ustr(e)) - else: - raise UserError(_(messageTitle), _(messageContent)) - - @api.model - def schedule_backup(self): - - for rec in self.search([]): - if not os.path.isdir(rec.bkp_dir): - os.makedirs(rec.bkp_dir) - # Create name for dumpfile. - bkp_file = '%s_%s.dump.zip' % ( - time.strftime('%d_%m_%Y_%H_%M_%S'), - rec.name) - file_path = os.path.join(rec.bkp_dir, bkp_file) - fbk = open(file_path, 'wb') - db.dump_db(rec.name, fbk) - fbk.close() - # Check if user wants to write to SFTP or not. - if rec.sftpwrite is True: - try: - # Store all values in variables - dir = rec.bkp_dir - pathToWriteTo = rec.sftppath - ipHost = rec.sftpip - portHost = rec.sftpport - usernameLogin = rec.sftpusername - passwordLogin = rec.sftppassword - # Connect with external server over SFTP - srv = pysftp.Connection( - host=ipHost, - username=usernameLogin, - password=passwordLogin, - port=portHost) - # Move to the correct directory on external server. If the - # user made a typo in his path with multiple slashes - # (/odoo//backups/) it will be fixed by this regex. - pathToWriteTo = re.sub('/+', '/', pathToWriteTo) - _logger.debug( - 'Start to copy files..' - ) - try: - srv.chdir(pathToWriteTo) - except IOError: - # Create directory and subdirs if they do not exist. - currentDir = '' - for dirElement in pathToWriteTo.split('/'): - currentDir += dirElement + '/' - try: - srv.chdir(currentDir) - except: - _logger.info( - _( - '(Part of the) path didn\'t exist. ' - 'Creating it now at %s' - ) % currentDir - ) - # Make directory and then navigate into it - srv.mkdir(currentDir, mode=777) - srv.chdir(currentDir) - pass - srv.chdir(pathToWriteTo) - # Loop over all files in the directory. - for f in os.listdir(dir): - fullpath = os.path.join(dir, f) - if os.path.isfile(fullpath): - srv.put(fullpath) - - # Navigate in to the correct folder. - srv.chdir(pathToWriteTo) - # Loop over all files in the directory from the back-ups. - # We will check the creation date of every back-up. - for file in srv.listdir(pathToWriteTo): - # Get the full path - fullpath = os.path.join(pathToWriteTo, file) - if srv.isfile(fullpath) and ".dump.zip" in file: - # Get the timestamp from the file on the external - # server - timestamp = srv.stat(fullpath).st_atime - createtime = ( - datetime.datetime.fromtimestamp(timestamp) - ) - now = datetime.datetime.now() - delta = now - createtime - # If the file is older than the daystokeepsftp (the - # days to keep that the user filled in on the Odoo - # form it will be removed. - if ( - rec.daystokeepsftp > 0 and - delta.days >= rec.daystokeepsftp - ): - # Only delete files, no directories! - srv.unlink(file) - # Close the SFTP session. - srv.close() - except Exception as e: - _logger.debug( - 'Exception We couldn\'t back ' - 'up to the FTP server..' - ) - # At this point the SFTP backup failed. - # We will now check if the user wants - # an e-mail notification about this. - if rec.sendmailsftpfail: - self.send_notification(rec, e) - - # Remove all old files (on local server) - # in case this is configured.. - if rec.autoremove is True: - try: - self.remove_folder(rec) - except Exception as e: - _logger.debug( - 'Exception when try to remove file' - ) - - return True - - def send_notification(self, rec, e): - try: - ir_mail_server = self.env['ir.mail_server'] - message = ( - "Dear,\n\nThe backup for the server %s" - " (IP: %s) failed.Please check" - " the following details:\n\n" - "IP address SFTP server: %s \nUsername: %s" - "\nPassword: %s" - "\n\nError details: %s \n\nWith kind regards" - ) % ( - rec.host, rec.sftpip, rec.sftpip, - rec.sftpusername, rec.sftppassword, - tools.ustr(e) - ) - msg = ir_mail_server.build_email( - "auto_backup@%s.com" % rec.name, - [rec.emailtonotify], - "Backup from %s ( %s ) failed" % ( - rec.host, rec.sftpip), - message) - ir_mail_server.send_email(msg) - - except Exception as e: - _logger.debug( - 'Exception %s' % tools.ustr(e) - ) - - # This is done after the SFTP writing to prevent unusual behaviour: - # If the user would set local back-ups to be kept 0 days and the SFTP - # to keep backups xx days there wouldn't be any new back-ups added - # to the SFTP. - # If we'd remove the dump files before they're writen to the SFTP - # there willbe nothing to write. Meaning that if an user doesn't want - # to keep back-ups locally and only wants them on the SFTP - # (NAS for example) there wouldn't be any writing to the - # remote server if this if statement was before the SFTP write method - # right above this comment. - def remove_folder(self, rec): - dir = rec.bkp_dir - # Loop over all files in the directory. - for f in os.listdir(dir): - fullpath = os.path.join(dir, f) - if os.path.isfile(fullpath) and ".dump.zip" in f: - timestamp = os.stat(fullpath).st_ctime - createtime = ( - datetime.datetime.fromtimestamp(timestamp) - ) - now = datetime.datetime.now() - delta = now - createtime - if delta.days >= rec.daystokeep: - _logger.debug( - 'Remove local file...' - ) - os.remove(fullpath) diff --git a/auto_backup/model/__init__.py b/auto_backup/models/__init__.py similarity index 96% rename from auto_backup/model/__init__.py rename to auto_backup/models/__init__.py index 995d7f02b..1f7194c4f 100644 --- a/auto_backup/model/__init__.py +++ b/auto_backup/models/__init__.py @@ -20,4 +20,4 @@ # ############################################################################## -from . import backup_scheduler +from . import db_backup diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py new file mode 100644 index 000000000..cbf652cb1 --- /dev/null +++ b/auto_backup/models/db_backup.py @@ -0,0 +1,282 @@ +# -*- coding: utf-8 -*- +############################################################################## +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (). All Rights Reserved +# Copyright 2015 Agile Business Group +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################################## +import os +import shutil +import tempfile +import traceback +from contextlib import contextmanager +from datetime import datetime, timedelta +from glob import iglob +from openerp import exceptions, models, fields, api, _, tools +from openerp.service import db +import logging +_logger = logging.getLogger(__name__) +try: + import pysftp +except ImportError: + _logger.warning('Cannot import pysftp') + + +class DbBackup(models.Model): + _name = 'db.backup' + _inherit = "mail.thread" + + _sql_constraints = [ + ("days_to_keep_positive", "CHECK(days_to_keep >= 0)", + "I cannot remove backups from the future. Ask Doc for that."), + ] + + name = fields.Char( + string="Name", + compute="_compute_name", + store=True, + help="Summary of this backup process", + ) + folder = fields.Char( + default=lambda self: self._default_folder(), + oldname="bkp_dir", + help='Absolute path for storing the backups', + required=True + ) + days_to_keep = fields.Integer( + oldname="daystokeep", + required=True, + default=0, + help="Backups older than this will be deleted automatically. " + "Set 0 to disable autodeletion.", + ) + method = fields.Selection( + selection=[("local", "Local disk"), ("sftp", "Remote SFTP server")], + default="local", + help="Choose the storage method for this backup.", + ) + sftp_host = fields.Char( + string='SFTP Server', + oldname="sftpip", + help=( + "The host name or IP address from your remote" + " server. For example 192.168.0.1" + ) + ) + sftp_port = fields.Integer( + string="SFTP Port", + default=22, + oldname="sftpport", + help="The port on the FTP server that accepts SSH/SFTP calls." + ) + sftp_user = fields.Char( + string='Username in the SFTP Server', + oldname="sftpusername", + help=( + "The username where the SFTP connection " + "should be made with. This is the user on the external server." + ) + ) + sftp_password = fields.Char( + string="SFTP Password", + oldname="sftppassword", + help="The password for the SFTP connection. If you specify a private " + "key file, then this is the password to decrypt it.", + ) + sftp_private_key = fields.Char( + string="Private key location", + help="Path to the private key file. Only the Odoo user should have " + "read permissions for that file.", + ) + + @api.model + def _default_folder(self): + """Default to ``backups`` folder inside current database datadir.""" + return os.path.join( + tools.config.filestore(self.env.cr.dbname), + "backups") + + @api.multi + @api.depends("folder", "method", "sftp_host", "sftp_port", "sftp_user") + def _compute_name(self): + """Get the right summary for this job.""" + for rec in self: + if rec.method == "local": + rec.name = "%s @ localhost" % rec.folder + elif rec.method == "sftp": + rec.name = "sftp://%s@%s:%d%s" % ( + rec.sftp_user, rec.sftp_host, rec.sftp_port, rec.folder) + + @api.multi + def action_sftp_test_connection(self): + """Check if the SFTP settings are correct.""" + try: + # Just open and close the connection + with self.sftp_connection(): + raise exceptions.Warning(_("Connection Test Succeeded!")) + except (pysftp.CredentialException, pysftp.ConnectionException): + _logger.info("Connection Test Failed!", exc_info=True) + raise exceptions.Warning(_("Connection Test Failed!")) + + @api.multi + def action_backup(self): + """Run selected backups.""" + backup = None + filename = self.filename(datetime.now()) + successful = self.browse() + + # Start with local storage + for rec in self.filtered(lambda r: r.method == "local"): + with rec.backup_log(): + # Directory must exist + try: + os.makedirs(rec.folder) + except OSError: + pass + + with open(os.path.join(rec.folder, filename), + 'wb') as destiny: + # Copy the cached backup + if backup: + with open(backup) as cached: + shutil.copyfileobj(cached, destiny) + # Generate new backup + else: + db.dump_db(self.env.cr.dbname, destiny) + backup = backup or destiny.name + successful |= rec + + # Ensure a local backup exists if we are going to write it remotely + sftp = self.filtered(lambda r: r.method == "sftp") + if sftp: + if backup: + cached = open(backup) + else: + cached = tempfile.TemporaryFile() + db.dump_db(self.env.cr.dbname, cached) + + with cached: + for rec in sftp: + with rec.backup_log(): + with rec.sftp_connection() as remote: + # Directory must exist + try: + remote.makedirs(rec.folder) + except pysftp.ConnectionException: + pass + + # Copy cached backup to remote server + with remote.open( + os.path.join(rec.folder, filename), + "wb") as destiny: + shutil.copyfileobj(cached, destiny) + successful |= rec + + # Remove old files for successful backups + successful.cleanup() + + @api.model + def action_backup_all(self): + """Run all scheduled backups.""" + return self.search([]).action_backup() + + @api.multi + @contextmanager + def backup_log(self): + """Log a backup result.""" + try: + _logger.info("Starting database backup: %s", self.name) + yield + except: + _logger.exception("Database backup failed: %s", self.name) + escaped_tb = tools.html_escape(traceback.format_exc()) + self.message_post( + "

%s

%s
" % ( + _("Database backup failed."), + escaped_tb), + subtype=self.env.ref("auto_backup.failure")) + else: + _logger.info("Database backup succeeded: %s", self.name) + self.message_post(_("Database backup succeeded.")) + + @api.multi + def cleanup(self): + """Clean up old backups.""" + now = datetime.now() + for rec in self.filtered("days_to_keep"): + with rec.cleanup_log(): + oldest = self.filename(now - timedelta(days=rec.days_to_keep)) + + if rec.method == "local": + for name in iglob(os.path.join(rec.folder, + "*.dump.zip")): + if name < oldest: + os.unlink(name) + + elif rec.method == "sftp": + with rec.sftp_connection() as remote: + for name in remote.listdir(rec.folder): + if name.endswith(".dump.zip") and name < oldest: + remote.unlink(name) + + @api.multi + @contextmanager + def cleanup_log(self): + """Log a possible cleanup failure.""" + try: + _logger.info("Starting cleanup process after database backup: %s", + self.name) + yield + except: + _logger.exception("Cleanup of old database backups failed: %s") + escaped_tb = tools.html_escape(traceback.format_exc()) + self.message_post( + "

%s

%s
" % ( + _("Cleanup of old database backups failed."), + escaped_tb), + subtype=self.env.ref("auto_backup.failure")) + else: + _logger.info("Cleanup of old database backups succeeded: %s", + self.name) + + @api.model + def filename(self, when): + """Generate a file name for a backup. + + :param datetime.datetime when: + Use this datetime instead of :meth:`datetime.datetime.now`. + """ + return "{:%Y_%m_%d_%H_%M_%S}.dump.zip".format(when) + + @api.multi + def sftp_connection(self): + """Return a new SFTP connection with found parameters.""" + params = { + "host": self.sftp_host, + "username": self.sftp_user, + "port": self.sftp_port, + } + _logger.debug( + "Trying to connect to sftp://%(username)s@%(host)s:%(port)d", + extra=params) + if self.sftp_private_key: + params["private_key"] = self.stfpprivatekey + if self.sftp_password: + params["private_key_pass"] = self.sftp_password + else: + params["password"] = self.sftp_password + + return pysftp.Connection(**params) diff --git a/auto_backup/security/ir.model.access.csv b/auto_backup/security/ir.model.access.csv index e38dbf11b..ded4108f4 100644 --- a/auto_backup/security/ir.model.access.csv +++ b/auto_backup/security/ir.model.access.csv @@ -1,2 +1,3 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_db_backup,access_db_backup,model_db_backup,,1,0,0,0 +access_db_backup_read,Read db.backup,model_db_backup,base.group_erp_manager,1,0,0,0 +access_db_backup_write,Write db.backup,model_db_backup,base.group_system,1,1,1,1 diff --git a/auto_backup/static/description/icon.png b/auto_backup/static/description/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..24c59d3f524ce8088277cea5691c18684b9d92db GIT binary patch literal 6074 zcmV;r7e(laP)rPd7b$3;FC**t1JGH&7?(e;OfA{zO{(cV*<1h}RJhX{Eff(w5 zB#;8^*sDSo@JhTMQWy_|GywB~xxkUY9H6=Ic{VT)+4!SHH}EH5H_!^a6?=UXc!7h+ zaMd)31{?=01{MN~fKz~D%N;2mm1jQ4|q|p%6lp z`!50TBCrd17WiG^b#GPvb(JwzgB$_87g$|bX2pNhbzLM93C%Q3Ow&ZybqvG6FbtFt zss$kgVHgsIp)wLd5a9bhK@gNO*h|1ez%PKukX_(}V>B(Y0Qe|y9`bI`K{kFenbZ=A z1g2?{NF*=}ql%|GHpGmB=XrRZ7dMgzJOTV1_zAKLoDfuMkUHR#z^8#z3*QOLvNYSa zNhXsd5{a6eV67m8!1Fv@*Tr>ReBW0;g}^U?{{}Vz(RiNMa0Y1r{vP;SU>YE7+tyO4 z6t-egNW{tDPq>tmNP$W-7*z=fKoiF7)xrPJv{>TakMVHnce+eRRMMoun&U-jb@Uh9E9x~@0Onl(#%JB}BXA_xN7+uH%21eR5M|Ef=#4>YDy zsl&z?2Bv9ZSr)*%fT~ZvltJbIhX?QqQD`R<862%D^g*wOF)~!W-3}XKlHssHCK(PJ zWRl^qK_(fLL4pZAi-Q+cZ#QL-*FjEt4r6!}+R3E`G_W$r3&5UyJ}(qseHf+Td7dJ( z9;OE5m6{1gfDc^Y`#z~u>TMZr9}ZCz(b?H4gj8ExfmF6s74fP04&d}K3{UWV|Bzz@ z$BG~b==+#<52K4HKe2SGoB zljXW@Tr}5)9I(F+ygbA%&H!Ek_E+<~)Ja@|)E<2q_%DW5RiuIUBbAG*kiuzzqGC}k zrfE(JBgIvVzVB1u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oFTTHJXp4DnepI* z4_*XV${;TTgH_;Fftmykfg+6?54{YY;g@paoQK9nFPd~l*-R-yE&I1oTQ1w*r*hLdD2B&5A?AgKEwQK8| zo14)zjU`K#=v%gIc~5){`4`~lU0q$wm@#8qQ=`XHSw+4h5Ry&bFMpcAum1&wkZE9-LbGe)m zMbRHz*F6U*p#@l+NF*Mrudh!BL7@4*-|o8Zdl|aYY1z=pgGk=v+hG{`-QC@^wY7=f z-rk|)p@#$t<3#cQ3L!d?Ps@Hc{l-Wo5{bL6y6UR*Ew|iKw{hdf_GzJ2@1X0wOP z^F}<+qqn!0_V#ul`in2VI4Oisp`@9v z>mGl}C70+*UAbn>8q;x{GmsJOvZ!RH{w9(O_$sm}Y@(j9AcQET${#H7evAJ4 zy$>UMhT-srWm%iou3fwO(n~M3J3BksxN)QBd7gS|f@c`Ue}4VzU;oVImtSuCzR#_< z-s*&5_>JLqJldApjjZeYL!{fb1et|z?Gm4BXks zXl*6jCbem(x2W|O}xYN zye|YnfN7e{m@$LK#zxn5-QQ@MHYo+lQ4}pa`Q(#}p^%=Q9(><#KUAJ)^qOg!nP;DU zcFO6epH3o?VBNZP$!DHTR0lSDTxgzj)(~ zH`)`GM>twM^2j5sU%$S`_x-sCPe^wdL{apV>$)G6x}OAo z3FBDncARk(Qr>#xM4!kI$RpkGtsH(9@#Mw-1A$BQiO%}0761SM07*qoM6N<$g4H*z AdjJ3c literal 0 HcmV?d00001 diff --git a/auto_backup/static/description/icon.svg b/auto_backup/static/description/icon.svg new file mode 100644 index 000000000..b28b182e1 --- /dev/null +++ b/auto_backup/static/description/icon.svg @@ -0,0 +1,51 @@ + + + + + + + image/svg+xml + + + + + + + + + + diff --git a/auto_backup/static/description/no_index.html b/auto_backup/static/description/no_index.html deleted file mode 100644 index 6b3ba3e2b..000000000 --- a/auto_backup/static/description/no_index.html +++ /dev/null @@ -1,100 +0,0 @@ -
-
-
-

Automated backups

-

A tool for all your back-ups, internal and external!

-
-
-
- -
-
-
-

- Keep your Odoo data safe with this module. Take automated back-ups, remove them automatically - and even write them to an external server through an encrypted tunnel. - You can even specify how long local backups and external backups should be kept, automatically! -

- -
-
-
- -
-
-

Connect with an FTP Server

-

Keep your data safe, through an SSH tunnel!

-
-

- Want to go even further and write your backups to an external server? - You can with this module! Specify the credentials to the server, specify a path and everything will be backed up automatically. This is done through an SSH (encrypted) tunnel, thanks to pysftp, so your data is safe! - -

-
-
-
- -
-
-
-
- -
-
-
-

Test connection

-

Checks your credentials in one click

-
-
-
- - -
-
-
-

- Want to make sure if the connection details are correct and if Odoo can automatically write them to the remote server? Simply click on the 'Test SFTP Connection' button and you will get message telling you if everything is OK, or what is wrong! -

-
-
-
- -
-
-

E-mail on backup failure

-

Stay informed of problems, automatically!

-
-

- Do you want to know if the database backup failed? Check the checkbox 'Auto. E-mail on backup fail' and fill in your e-mail. - Every time a backup fails you will get an e-mail in your mailbox with technical details. -

-
-
-
- -
-
-
-
- -
-
-
-

Contact / Support

-

Need help or want extra features?

-
-
-
- -
-
-
-

- Need help with the configuration or want this module to have more functionalities? - Contact us through e-mail at yenthe.vanginneken@vanroey.be or tony.crols@vanroey.be -

-
-
-
diff --git a/auto_backup/tests/test_auto_backup.py b/auto_backup/tests/test_auto_backup.py index e2e6fee45..a23089144 100644 --- a/auto_backup/tests/test_auto_backup.py +++ b/auto_backup/tests/test_auto_backup.py @@ -19,38 +19,25 @@ # ############################################################################## -from openerp.tests import common -from openerp.exceptions import except_orm import os -import time +from datetime import datetime +from openerp.tests import common class TestsAutoBackup(common.TransactionCase): def setUp(self): super(TestsAutoBackup, self).setUp() - self.abk_model = self.env["db.backup"] - self.cron_model = self.env["ir.cron"] - - def test_0(self): - with self.assertRaises(except_orm): - self.abk_model.create( - { - 'name': 'abcd', - 'adminpassword': 'admin' - } - ) - - def test_1(self): - this = self.abk_model.create( + self.abk = self.env["db.backup"].create( { - 'bkp_dir': '/tmp' + 'name': u'Têst backup', } ) - self.assertEqual(this.bkp_dir, '/tmp') - bkp_file = '%s_%s.dump.zip' % ( - time.strftime('%d_%m_%Y_%H_%M_%S'), - this.name) - file_path = os.path.join(this.bkp_dir, bkp_file) - this.schedule_backup() - self.assertTrue(os.path.isfile(file_path)) + + def test_local(self): + """A local database is backed up.""" + filename = self.abk.filename(datetime.now()) + self.abk.action_backup() + generated_backup = [f for f in os.listdir(self.abk.folder) + if f >= filename] + self.assertEqual(len(generated_backup), 1) diff --git a/auto_backup/view/bkp_conf_view.xml b/auto_backup/view/bkp_conf_view.xml deleted file mode 100644 index cb2281b08..000000000 --- a/auto_backup/view/bkp_conf_view.xml +++ /dev/null @@ -1,100 +0,0 @@ - - - - - - Configure Backup - db.backup - form - -
- - - - - - - - - - - - -
- Warning: - Use SFTP with caution! This writes files to external servers under the path you specify. -
- - - - - - - - - - -