diff --git a/auto_backup/README.rst b/auto_backup/README.rst index 60c77d98a0a..a2ce3977d1c 100644 --- a/auto_backup/README.rst +++ b/auto_backup/README.rst @@ -111,7 +111,6 @@ Known issues / Roadmap you need to run the backup from outside of the main Odoo instance. How to do this is outlined in `this blog post `_. -* Backups won't work if list_db=False is configured in the instance. Bug Tracker =========== @@ -145,6 +144,7 @@ Contributors * Andrea Stirpe * Aitor Bouzas * Simone Vanin +* klodr * Vu Nguyen Anh * Alex Comba diff --git a/auto_backup/__manifest__.py b/auto_backup/__manifest__.py index 7a72b279c1b..4757f00a7e0 100644 --- a/auto_backup/__manifest__.py +++ b/auto_backup/__manifest__.py @@ -6,7 +6,7 @@ { "name": "Database Auto-Backup", "summary": "Backups database", - "version": "16.0.1.0.3", + "version": "16.0.1.0.4", "author": "Yenthe Van Ginneken, " "Agile Business Group, " "Grupo ESOC Ingenieria de Servicios, " diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index b8517bae55a..0305d67f342 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -163,9 +163,12 @@ 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 - ) + with self._db_management_enabled(): + db.dump_db( + self.env.cr.dbname, + destiny, + backup_format=rec.backup_format, + ) backup = backup or destiny.name successful |= rec @@ -176,9 +179,10 @@ 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 - ) + with self._db_management_enabled(): + cached = db.dump_db( + self.env.cr.dbname, None, backup_format=rec.backup_format + ) with cached: with rec.sftp_connection() as remote: @@ -205,6 +209,25 @@ def action_backup_all(self): """Run all scheduled backups.""" return self.search([]).action_backup() + @contextmanager + def _db_management_enabled(self): + """Temporarily allow database management functions during a backup. + + ``odoo.service.db.dump_db`` is protected by + ``check_db_management_enabled``, which raises ``AccessDenied`` when + ``list_db = False`` is set in the Odoo configuration. That option only + aims at hiding database management from the web interface; a scheduled + backup is a trusted server-side operation, so we re-enable the flag for + the duration of the dump and always restore its original value + afterwards. + """ + list_db = tools.config["list_db"] + tools.config["list_db"] = True + try: + yield + finally: + tools.config["list_db"] = list_db + @contextmanager def backup_log(self): """Log a backup result.""" diff --git a/auto_backup/readme/CONTRIBUTORS.rst b/auto_backup/readme/CONTRIBUTORS.rst index bac3ca7b129..f548b581f93 100644 --- a/auto_backup/readme/CONTRIBUTORS.rst +++ b/auto_backup/readme/CONTRIBUTORS.rst @@ -5,5 +5,6 @@ * Andrea Stirpe * Aitor Bouzas * Simone Vanin +* klodr * Vu Nguyen Anh * Alex Comba diff --git a/auto_backup/readme/ROADMAP.rst b/auto_backup/readme/ROADMAP.rst index e96ef407e25..555775cbe9c 100644 --- a/auto_backup/readme/ROADMAP.rst +++ b/auto_backup/readme/ROADMAP.rst @@ -3,4 +3,3 @@ you need to run the backup from outside of the main Odoo instance. How to do this is outlined in `this blog post `_. -* Backups won't work if list_db=False is configured in the instance. diff --git a/auto_backup/static/description/index.html b/auto_backup/static/description/index.html index ff75e85f681..373869b84e0 100644 --- a/auto_backup/static/description/index.html +++ b/auto_backup/static/description/index.html @@ -471,7 +471,6 @@

Known issues / Roadmap

settings. In order to circumvent this without frivolously changing settings, you need to run the backup from outside of the main Odoo instance. How to do this is outlined in this blog post. -
  • Backups won’t work if list_db=False is configured in the instance.
  • @@ -504,6 +503,7 @@

    Contributors

  • Andrea Stirpe <a.stirpe@onestein.nl>
  • Aitor Bouzas <aitor.bouzas@adaptivecity.com>
  • Simone Vanin <simone.vanin@agilebg.com>
  • +
  • klodr <klodr@users.noreply.github.com>
  • Vu Nguyen Anh <vuna2004@gmail.com>
  • Alex Comba <alex.comba@agilebg.com>
  • diff --git a/auto_backup/tests/test_db_backup.py b/auto_backup/tests/test_db_backup.py index b7dc6582bc2..1f7d547bab5 100644 --- a/auto_backup/tests/test_db_backup.py +++ b/auto_backup/tests/test_db_backup.py @@ -125,6 +125,15 @@ def test_action_backup_local(self): generated_backup = [f for f in os.listdir(rec_id.folder) if f >= filename] self.assertEqual(1, len(generated_backup)) + def test_action_backup_local_list_db_disabled(self): + """It should backup locally even when list_db is disabled""" + rec_id = self.new_record("local") + filename = rec_id.filename(datetime.now()) + with patch.dict(tools.config.options, {"list_db": False}): + rec_id.action_backup() + generated_backup = [f for f in os.listdir(rec_id.folder) if f >= filename] + self.assertEqual(1, len(generated_backup)) + def test_action_backup_local_cleanup(self): """Backup local database and cleanup old databases""" rec_id = self.new_record("local")