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 000000000..24c59d3f5 Binary files /dev/null and b/auto_backup/static/description/icon.png differ 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. -
- - - - - - - - - - -