From 8c6751f68455c3da0301434b19c8ea290cf0922b Mon Sep 17 00:00:00 2001 From: Jordi Riera Date: Wed, 1 Aug 2018 16:00:33 -0400 Subject: [PATCH 1/8] auto_backup: allow to change the format of backup --- auto_backup/models/db_backup.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index 464215cfb8d..884aa952556 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -83,6 +83,12 @@ class DbBackup(models.Model): "read permissions for that file.", ) + backup_format = fields.Selection( + [("zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)")], + default='zip', + help="Choose the format for this backup." + ) + @api.model def _default_folder(self): """Default to ``backups`` folder inside current server datadir.""" @@ -151,21 +157,22 @@ def action_backup(self): shutil.copyfileobj(cached, destiny) # Generate new backup else: - db.dump_db(self.env.cr.dbname, destiny) + db.dump_db(self.env.cr.dbname, destiny, backup_format=rec.backup_format) 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 = db.dump_db(self.env.cr.dbname, None) + for rec in sftp: + with rec.backup_log(): + + if backup: + cached = open(backup) + else: + cached = db.dump_db(self.env.cr.dbname, None, backup_format=rec.backup_format) - with cached: - for rec in sftp: - with rec.backup_log(): + with cached: with rec.sftp_connection() as remote: # Directory must exist try: From 481d4f5f41b5d76115ddb533c718e0e7e8f71ae9 Mon Sep 17 00:00:00 2001 From: Jordi Riera Date: Wed, 1 Aug 2018 16:04:50 -0400 Subject: [PATCH 2/8] auto_backup: add the view for backup_format --- auto_backup/models/db_backup.py | 1 + auto_backup/view/db_backup_view.xml | 1 + 2 files changed, 2 insertions(+) diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index 884aa952556..b026cc98cae 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -85,6 +85,7 @@ class DbBackup(models.Model): backup_format = fields.Selection( [("zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)")], + string="Backup Format", default='zip', help="Choose the format for this backup." ) diff --git a/auto_backup/view/db_backup_view.xml b/auto_backup/view/db_backup_view.xml index 42f826feb39..ebed80c91af 100644 --- a/auto_backup/view/db_backup_view.xml +++ b/auto_backup/view/db_backup_view.xml @@ -15,6 +15,7 @@ +
From 97293db2ce547251761cb55f411d6a60fef1cfb3 Mon Sep 17 00:00:00 2001 From: Jordi Riera Date: Wed, 1 Aug 2018 16:29:49 -0400 Subject: [PATCH 3/8] auto_backup: filename driven by the backup format --- auto_backup/models/db_backup.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index b026cc98cae..c5c2eaf920d 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -84,9 +84,9 @@ class DbBackup(models.Model): ) backup_format = fields.Selection( - [("zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)")], + [("dump.zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)")], string="Backup Format", - default='zip', + default='dump.zip', help="Choose the format for this backup." ) @@ -138,11 +138,11 @@ def action_sftp_test_connection(self): 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"): + filename = self.filename(datetime.now(), ext=rec.backup_format) with rec.backup_log(): # Directory must exist try: @@ -166,6 +166,7 @@ def action_backup(self): sftp = self.filtered(lambda r: r.method == "sftp") if sftp: for rec in sftp: + filename = self.filename(datetime.now(), ext=rec.backup_format) with rec.backup_log(): if backup: @@ -263,13 +264,14 @@ def cleanup_log(self): self.name) @staticmethod - def filename(when): + def filename(when, ext='dump.zip'): """Generate a file name for a backup. :param datetime.datetime when: Use this datetime instead of :meth:`datetime.datetime.now`. + :param str ext: Extension of the file. Default: dump.zip """ - return "{:%Y_%m_%d_%H_%M_%S}.dump.zip".format(when) + return "{:%Y_%m_%d_%H_%M_%S}.{ext}".format(when, ext=ext) @api.multi def sftp_connection(self): From 25b48b968a5b9726cca6118974b2dbb09a2729ff Mon Sep 17 00:00:00 2001 From: Jordi Riera Date: Wed, 1 Aug 2018 16:40:04 -0400 Subject: [PATCH 4/8] auto_backup: remove useless condition --- auto_backup/models/db_backup.py | 5 +---- docker-compose.yml | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 docker-compose.yml diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index c5c2eaf920d..7f026ecff73 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -169,10 +169,7 @@ def action_backup(self): filename = self.filename(datetime.now(), ext=rec.backup_format) with rec.backup_log(): - if backup: - cached = open(backup) - else: - cached = db.dump_db(self.env.cr.dbname, None, backup_format=rec.backup_format) + cached = db.dump_db(self.env.cr.dbname, None, backup_format=rec.backup_format) with cached: with rec.sftp_connection() as remote: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000000..fa647242cc4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +version: '3' +services: + odoo: + image: quay.io/numigi/odoo-base:11.12 + volumes: + - odoo-web-data:/var/lib/odoo + - ./log:/var/log/odoo + - ./auto_backup:/mnt/extra-addons/auto_backup + ports: + - "127.0.0.1:8069:8069" + - "127.0.0.1:8071:8071" + depends_on: + - db + environment: + - LOG_ODOO=/var/log/odoo + command: odoo + db: + image: quay.io/numigi/postgres-odoo:11.1 + environment: + - POSTGRES_PASSWORD=odoo + - POSTGRES_USER=odoo + - PGDATA=/var/lib/postgresql/data/pgdata + volumes: + - odoo-db-data:/var/lib/postgresql/data/pgdata + expose: + - 5432 +volumes: + odoo-web-data: + odoo-db-data: From eab4bb1b4bf1ad0fd5ebfc463253a4b3dec6d8ff Mon Sep 17 00:00:00 2001 From: Jordi Riera Date: Thu, 2 Aug 2018 14:20:38 -0400 Subject: [PATCH 5/8] remove useless file --- docker-compose.yml | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index fa647242cc4..00000000000 --- a/docker-compose.yml +++ /dev/null @@ -1,29 +0,0 @@ -version: '3' -services: - odoo: - image: quay.io/numigi/odoo-base:11.12 - volumes: - - odoo-web-data:/var/lib/odoo - - ./log:/var/log/odoo - - ./auto_backup:/mnt/extra-addons/auto_backup - ports: - - "127.0.0.1:8069:8069" - - "127.0.0.1:8071:8071" - depends_on: - - db - environment: - - LOG_ODOO=/var/log/odoo - command: odoo - db: - image: quay.io/numigi/postgres-odoo:11.1 - environment: - - POSTGRES_PASSWORD=odoo - - POSTGRES_USER=odoo - - PGDATA=/var/lib/postgresql/data/pgdata - volumes: - - odoo-db-data:/var/lib/postgresql/data/pgdata - expose: - - 5432 -volumes: - odoo-web-data: - odoo-db-data: From bc063be7b63cadf5e68d64d435f40d48a8d99938 Mon Sep 17 00:00:00 2001 From: Jordi Riera Date: Thu, 2 Aug 2018 14:26:50 -0400 Subject: [PATCH 6/8] auto_backup: fix the filenames --- auto_backup/models/db_backup.py | 8 ++++---- auto_backup/tests/test_db_backup.py | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index 7f026ecff73..c29e69c8f61 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -84,9 +84,9 @@ class DbBackup(models.Model): ) backup_format = fields.Selection( - [("dump.zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)")], + [("zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)")], string="Backup Format", - default='dump.zip', + default='zip', help="Choose the format for this backup." ) @@ -261,14 +261,14 @@ def cleanup_log(self): self.name) @staticmethod - def filename(when, ext='dump.zip'): + def filename(when, ext='zip'): """Generate a file name for a backup. :param datetime.datetime when: Use this datetime instead of :meth:`datetime.datetime.now`. :param str ext: Extension of the file. Default: dump.zip """ - return "{:%Y_%m_%d_%H_%M_%S}.{ext}".format(when, ext=ext) + return "{:%Y_%m_%d_%H_%M_%S}.{ext}".format(when, ext='dump.zip' if ext == 'zip' else ext) @api.multi def sftp_connection(self): diff --git a/auto_backup/tests/test_db_backup.py b/auto_backup/tests/test_db_backup.py index eac650c9b9b..13aa3ff9eb4 100644 --- a/auto_backup/tests/test_db_backup.py +++ b/auto_backup/tests/test_db_backup.py @@ -175,18 +175,6 @@ def test_action_backup_sftp_remote_open(self): 'wb' ) - def test_action_backup_sftp_remote_open(self): - """ It should open remote file w/ proper args """ - rec_id = self.new_record() - with self.mock_assets() as assets: - with self.patch_filtered_sftp(rec_id): - conn = rec_id.sftp_connection().__enter__() - rec_id.action_backup() - conn.open.assert_called_once_with( - assets['os'].path.join(), - 'wb' - ) - def test_action_backup_all_search(self): """ It should search all records """ rec_id = self.new_record() @@ -241,8 +229,20 @@ def test_sftp_connection_return(self, pysftp): pysftp.Connection(), res, ) - def test_filename(self): + def test_filename_default(self): """ It should not error and should return a .dump.zip file str """ now = datetime.now() res = self.Model.filename(now) self.assertTrue(res.endswith(".dump.zip")) + + def test_filename_zip(self): + """ It should return a dump.zip filename""" + now = datetime.now() + res = self.Model.filename(now, ext='zip') + self.assertTrue(res.endswith(".dump.zip")) + + def test_filename_dump(self): + """ It should return a dump filename""" + now = datetime.now() + res = self.Model.filename(now, ext='dump') + self.assertTrue(res.endswith(".dump")) From cc270b29e4b1eef80b66aa7e946623f507ba4732 Mon Sep 17 00:00:00 2001 From: Jordi Riera Date: Thu, 2 Aug 2018 14:31:38 -0400 Subject: [PATCH 7/8] auto_backup: style --- auto_backup/models/db_backup.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index c29e69c8f61..18d5428bf05 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -84,7 +84,10 @@ class DbBackup(models.Model): ) backup_format = fields.Selection( - [("zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)")], + [ + ("zip", "zip (includes filestore)"), + ("dump", "pg_dump custom format (without filestore)") + ], string="Backup Format", default='zip', help="Choose the format for this backup." @@ -158,7 +161,11 @@ def action_backup(self): shutil.copyfileobj(cached, destiny) # Generate new backup else: - db.dump_db(self.env.cr.dbname, destiny, backup_format=rec.backup_format) + db.dump_db( + self.env.cr.dbname, + destiny, + backup_format=rec.backup_format + ) backup = backup or destiny.name successful |= rec @@ -169,7 +176,11 @@ def action_backup(self): filename = self.filename(datetime.now(), ext=rec.backup_format) with rec.backup_log(): - cached = db.dump_db(self.env.cr.dbname, None, backup_format=rec.backup_format) + cached = db.dump_db( + self.env.cr.dbname, + None, + backup_format=rec.backup_format + ) with cached: with rec.sftp_connection() as remote: @@ -268,7 +279,9 @@ def filename(when, ext='zip'): Use this datetime instead of :meth:`datetime.datetime.now`. :param str ext: Extension of the file. Default: dump.zip """ - return "{:%Y_%m_%d_%H_%M_%S}.{ext}".format(when, ext='dump.zip' if ext == 'zip' else ext) + return "{:%Y_%m_%d_%H_%M_%S}.{ext}".format( + when, ext='dump.zip' if ext == 'zip' else ext + ) @api.multi def sftp_connection(self): From e67faefa126763e21c4bd71b6b14bbd9349c2d7c Mon Sep 17 00:00:00 2001 From: Jordi Riera <547282+foutoucour@users.noreply.github.com> Date: Wed, 8 Aug 2018 17:27:45 -0400 Subject: [PATCH 8/8] Update db_backup.py remove string parameter --- auto_backup/models/db_backup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index 18d5428bf05..4c89787ee75 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -88,7 +88,6 @@ class DbBackup(models.Model): ("zip", "zip (includes filestore)"), ("dump", "pg_dump custom format (without filestore)") ], - string="Backup Format", default='zip', help="Choose the format for this backup." )