Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion auto_backup/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ Known issues / Roadmap
Odoo instance. How to do this (for version 9.0) was outlined in `this
blog
post <https://web.archive.org/web/20240805225230/https://blog.laslabs.com/2016/10/running-python-scripts-within-odoos-environment/>`__.
- Backups won't work if list_db=False is configured in the instance.

Bug Tracker
===========
Expand Down
2 changes: 1 addition & 1 deletion auto_backup/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"name": "Database Auto-Backup",
"summary": "Backups database",
"version": "17.0.1.1.1",
"version": "17.0.1.1.2",
"author": "Yenthe Van Ginneken, "
"Agile Business Group, "
"Grupo ESOC Ingenieria de Servicios, "
Expand Down
35 changes: 29 additions & 6 deletions auto_backup/models/db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -175,9 +178,10 @@ def action_backup(self):
for rec in sftp:
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:
Expand All @@ -204,6 +208,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."""
Expand Down
1 change: 0 additions & 1 deletion auto_backup/readme/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
changing settings, you need to run the backup from outside of the main
Odoo instance. How to do this (for version 9.0) was outlined in [this blog
post](https://web.archive.org/web/20240805225230/https://blog.laslabs.com/2016/10/running-python-scripts-within-odoos-environment/).
- Backups won't work if list_db=False is configured in the instance.
1 change: 0 additions & 1 deletion auto_backup/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ <h2><a class="toc-backref" href="#toc-entry-11">Known issues / Roadmap</a></h2>
Odoo instance. How to do this (for version 9.0) was outlined in <a class="reference external" href="https://web.archive.org/web/20240805225230/https://blog.laslabs.com/2016/10/running-python-scripts-within-odoos-environment/">this
blog
post</a>.</li>
<li>Backups won’t work if list_db=False is configured in the instance.</li>
</ul>
</div>
<div class="section" id="bug-tracker">
Expand Down
9 changes: 9 additions & 0 deletions auto_backup/tests/test_db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,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")
Expand Down
Loading