Skip to content

[17.0][FIX] auto_backup: allow scheduled backup when list_db is disabled - #3640

Open
klodr wants to merge 2 commits into
OCA:17.0from
klodr:17.0-fix-auto_backup-list_db
Open

klodr wants to merge 2 commits into
OCA:17.0from
klodr:17.0-fix-auto_backup-list_db

Conversation

@klodr

@klodr klodr commented Jun 6, 2026 •

Copy link
Copy Markdown

Problem

When list_db = False is set in the Odoo configuration, scheduled auto_backup
jobs fail with:

Database management functions blocked, admin disabled database listing

odoo.service.db.dump_db is decorated with @check_db_management_enabled, which
raises AccessDenied when list_db is False. The backup fails and leaves a
zero-byte dump file that cleanup() keeps and nothing flags.

Fix

  • _dump_db builds the dump in the module, with the same core tools and layout
    as dump_db (pg_dump, the filestore, osutil.zip_dir), taking the manifest
    from the undecorated dump_db_manifest. list_db is never touched: switching
    it on, even for the dump only, would reopen /web/database/list to anonymous
    callers on a threaded server.
  • pg_dump runs with check=True in both formats.
  • Local backups are written to a partial file of their own, renamed only once
    complete: a failed dump leaves no empty or truncated file behind.

Tests assert that pg_dump runs with list_db still off, that zip and dump
backups have content, and that a failed dump leaves no file; they fail without
the fix.

Port of #3639.

odoo.service.db.dump_db is guarded by check_db_management_enabled, which raises AccessDenied ("Database management functions blocked, admin disabled database listing") when list_db = False is set in the Odoo configuration. Scheduled backups then failed silently, leaving zero-byte files.

list_db only hides database management from the web interface; a scheduled backup is a trusted server-side operation. Re-enable the flag for the duration of the dump through a context manager that always restores its original value, even on error.

Signed-off-by: klodr <[email protected]>
@legalsylvain
legalsylvain marked this pull request as draft June 6, 2026 08:25
@klodr
klodr marked this pull request as ready for review August 6, 2026 20:57
@klodr

klodr commented Sep 12, 2026

Copy link
Copy Markdown
Author

@OCA/server-tools-maintainers gentle ping — 9/9 checks green, no conflicts, open since June without review. Same fix as #3639 / #3641 / #3642 / #3643, backported per branch.

@rrebollo rrebollo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran a shell scenario on 17.0 with list_db off: the dump succeeds and the flag is restored. I would not get there by switching off a security setting; which deployments pay for that is what I could not settle.

Comment thread auto_backup/models/db_backup.py Outdated
list_db = tools.config["list_db"]
tools.config["list_db"] = True
try:
yield

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching this setting off is what I would not do: --no-database-list closes /web/database/list, auth='none' (addons/web/controllers/database.py:173), so on a threaded deployment the list answers anonymous callers while pg_dump runs (workers>0 forks the cron: nothing leaks there). Core flips it in odoo db dump (odoo/cli/db.py:124) where no server runs, and the dump is callable without it (dump_db.__wrapped__ on 17.0). Which way you take, a clear refusal included, is yours.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed: switching list_db on, even for the dump only, reopens the database manager to every request the process serves meanwhile. I measured it on 17.0 with --workers 0 --no-database-list, polling /web/database/list without a session every 20 ms during a 315 MB zip backup: 139 of 303 polls got the database list with the previous commit, 0 of 322 with the new one (and 0 with --workers 2, as you said).

dump_db.__wrapped__ alone is not enough for zip backups: dump_db calls dump_db_manifest, which is guarded too. So _dump_db now builds the dump with the same core tools and layout as dump_db (pg_dump, the filestore, osutil.zip_dir), taking the manifest from dump_db_manifest.__wrapped__; list_db is never touched. The resulting zip restores with odoo db load (same installed modules, same filestore).

Comment thread auto_backup/models/db_backup.py Outdated
db.dump_db(
self.env.cr.dbname, destiny, backup_format=rec.backup_format
)
with self._db_management_enabled():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced: the destination is opened "wb" before the dump (db_backup.py:159), so a failed pg_dump, a full disk or a missing tool leaves a 0-byte 2026_11_23_14_06_56.dump.zip that cleanup() keeps and nothing flags. The sftp branch already dumps into a temporary file and only writes the destination afterwards; the local path leaving the same artefact on a failed dump is the part I would fix here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: local backups are written to a partial file of their own (<name>.<uuid>.part), renamed only once complete and removed on failure, so cleanup() never sees an empty or truncated backup. pg_dump also runs with check=True in both formats; the core dump format path did not check its exit status.

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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the fix and ran this scenario: dump_db raises AccessDenied, backup_log() logs it and returns, and the zero-byte file opened at db_backup.py:159 is still in the folder, so generated_backup holds one entry and this assertion passes without the change. Asserting the dump has content makes it a regression test:

Suggested change
self.assertEqual(1, len(generated_backup))
self.assertEqual(1, len(generated_backup))
backup_path = os.path.join(rec_id.folder, generated_backup[0])
self.assertGreater(os.path.getsize(backup_path), 0)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the tests now assert that pg_dump runs with list_db still off, that zip (dump.sql, manifest.json) and dump (PGDMP header) backups have content, and that a failed dump leaves no file behind and posts the failure. They fail against the previous commit (list_db seen on, 0-byte file left) and against the unfixed module.

Comment thread auto_backup/__manifest__.py Outdated
"name": "Database Auto-Backup",
"summary": "Backups database",
"version": "17.0.1.1.1",
"version": "17.0.1.1.2",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version bump comes from ocabot on merge ([BOT] post-merge updates is what last touched this line), so a [FIX] leaves it alone and the diff stays with the fix itself:

Suggested change
"version": "17.0.1.1.2",
"version": "17.0.1.1.1",

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted, left to ocabot.

Switching list_db on for the duration of the dump reopened the database
manager routes, /web/database/list included (auth='none'), to every
request served meanwhile by the same process: on a threaded server,
anonymous callers got the database list for the whole backup.

Build the dump in the module instead, with the same core tools and the
same layout as odoo.service.db.dump_db (pg_dump, the filestore,
osutil.zip_dir), taking the manifest from the undecorated
dump_db_manifest. list_db is never touched. Calling dump_db.__wrapped__
alone is not enough for zip backups: it calls dump_db_manifest, which is
guarded too.

pg_dump now runs with check=True in both formats, and local backups are
written to a partial file of their own, renamed only once complete: a
failed dump no longer leaves an empty or truncated file that cleanup()
keeps. The tests assert that list_db stays off while pg_dump runs, that
zip and dump backups have content, and that a failed dump leaves no file
behind; they fail without this change.

The version bump is left to ocabot.

Signed-off-by: klodr <[email protected]>
@klodr

klodr commented Nov 24, 2026

Copy link
Copy Markdown
Author

Reworked following the review: the dump no longer switches list_db on (details in the threads). Same change on 18.0 (#3639), 16.0 (#3641), 15.0 (#3642) and 14.0 (#3643), tested on each version.

@rrebollo

Copy link
Copy Markdown

On the same module: #3683 is auto_backup_fs_file, the sibling that stores the dump in the filestore. It ports the v18 fixes back to 17.0 (folder optional and hidden for fs_file, cleanup as a sweep instead of deleting record by record) and adds the regression test the module never had. Unanswered since July, so any read helps.

@rrebollo rrebollo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-ran the rework on a984eb7: list_db stays off through the pg_dump, the zip carries core's manifest and entry set, and odoo db load restores it. The two notes on the lines are optional.

reopen the database manager routes, ``/web/database/list`` included,
to every request served meanwhile by the same process. The dump is
therefore built here with the same core tools and the same layout as
``dump_db``, taking the manifest from the undecorated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now owns a copy of core's dump layout. Naming odoo/service/db.py::dump_db (17.0) as what it mirrors is enough for the next reader to diff it when core changes the zip or the manifest; today the two are identical, manifest included.

else:
self._dump_db(destiny, rec.backup_format)
os.replace(partial, destination)
finally:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The finally only runs in-process: a hard crash or a container stop leaves <name>.dump.zip.<uuid>.part behind, and cleanup() only globs *.dump.zip, so nothing ever removes it. A sweep of stale .part files in cleanup(), or a line in the readme, would close it; read from the code, not run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants