1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(appsettings): the panel backup dumps a PostgreSQL database

With PostgreSQL the daily backup left the database out and only warned. It now
writes a pg_dump in its custom format as db.pgdump and fails when pg_dump is
missing or the dump fails. The Docker image ships postgresql-client; the restore
and move steps say how to put the dump back.
This commit is contained in:
catborise 2026-11-25 13:56:04 +03:00
parent b4c7ecddc4
commit 2b5d0ac80e
6 changed files with 67 additions and 19 deletions

View file

@ -15,8 +15,8 @@ PREFIX = "webvirtcloud-backup-"
class Command(BaseCommand):
help = ("Copy the database, secret key, worker id, local settings and the SSH keys and TLS certificates of the "
"panel's user into a new directory in backups/, then delete all but the newest --keep backups.")
help = ("Copy the database (sqlite) or dump it (PostgreSQL), the secret key, worker id, local settings and the "
"SSH keys and TLS certificates of the panel's user into a new directory in backups/, then delete all but the newest --keep backups.")
def add_arguments(self, parser):
parser.add_argument("--keep", type=int, default=7, help="how many backups to keep (default 7)")
@ -65,6 +65,8 @@ class Command(BaseCommand):
result = copy.execute("PRAGMA integrity_check").fetchone()[0]
if result != "ok":
raise CommandError(f"The copy of the database is damaged: {result}")
elif database["ENGINE"] == "django.db.backends.postgresql":
self.dump_postgresql(database, to / "db.pgdump")
else:
self.stderr.write("The database is not sqlite and is not in the backup: back it up with its own tools.")
# the key in use: SECRET_KEY from the environment has no data/secret_key
@ -87,3 +89,16 @@ class Command(BaseCommand):
commit = subprocess.run(["git", "rev-parse", "HEAD"], cwd=settings.BASE_DIR, capture_output=True, text=True)
if commit.returncode == 0:
(to / "commit").write_text(commit.stdout)
@staticmethod
def dump_postgresql(database, to):
"""pg_dump's own format, which pg_restore reads; a consistent state while the panel writes."""
if shutil.which("pg_dump") is None:
raise CommandError("pg_dump is not installed: install the PostgreSQL client (postgresql-client)")
# libpq reads the password and the connect timeout from the environment, not the command line
env = {**os.environ, "PGPASSWORD": database["PASSWORD"], "PGCONNECT_TIMEOUT": "10"}
dump = subprocess.run(["pg_dump", "--format=custom", f"--file={to}", f"--host={database['HOST']}",
f"--port={database['PORT']}", f"--username={database['USER']}", f"--dbname={database['NAME']}"],
env=env, capture_output=True, text=True)
if dump.returncode != 0:
raise CommandError(f"pg_dump failed: {dump.stderr.strip()}")

View file

@ -6,11 +6,11 @@ from datetime import datetime
import os
import shutil
import sqlite3
import subprocess
import tempfile
from contextlib import closing
from io import StringIO
from pathlib import Path
from unittest import skipUnless
from unittest.mock import patch
from django.core.management import call_command
@ -21,8 +21,6 @@ from django.test import SimpleTestCase, override_settings
from appsettings.management.commands.backup import Command
SQLITE_ONLY = skipUnless(connection.vendor == "sqlite", "the backup copies only a sqlite database")
class BackupTests(SimpleTestCase):
def setUp(self):
@ -49,7 +47,8 @@ class BackupTests(SimpleTestCase):
db.execute("CREATE TABLE computes_compute (name)")
db.execute("INSERT INTO computes_compute VALUES ('kept')")
db.commit()
name = patch.dict(connection.settings_dict, NAME=str(self.base / "db.sqlite3"))
# a sqlite panel also on the PostgreSQL test run; the PostgreSQL tests patch their own
name = patch.dict(connection.settings_dict, ENGINE="django.db.backends.sqlite3", NAME=str(self.base / "db.sqlite3"))
name.start()
self.addCleanup(name.stop)
@ -61,7 +60,6 @@ class BackupTests(SimpleTestCase):
def kept(self):
return sorted(p.name for p in self.backups.iterdir() if p.name != ".lock")
@SQLITE_ONLY
def test_a_backup_holds_the_database_keys_and_settings_for_the_panel_user_only(self):
backup = self.backup()
with closing(sqlite3.connect(backup / "db.sqlite3")) as db:
@ -110,7 +108,6 @@ class BackupTests(SimpleTestCase):
self.backup("--keep", "1")
self.assertEqual(self.kept(), ["webvirtcloud-backup-20260101-000000"])
@SQLITE_ONLY
def test_a_damaged_copy_fails(self):
with patch("appsettings.management.commands.backup.sqlite3") as sqlite, \
self.assertRaisesRegex(CommandError, "damaged: row 3 missing from index"):
@ -126,10 +123,34 @@ class BackupTests(SimpleTestCase):
self.backup()
self.assertEqual(self.kept(), [])
def test_a_database_other_than_sqlite_is_left_out(self):
errors = StringIO()
with patch.dict(connection.settings_dict, ENGINE="django.db.backends.postgresql"):
call_command("backup", stdout=StringIO(), stderr=errors)
backup = self.backups / self.kept()[0]
self.assertNotIn("db.sqlite3", [p.name for p in backup.iterdir()])
self.assertIn("back it up with its own tools", errors.getvalue())
def postgresql(self):
return patch.dict(connection.settings_dict, ENGINE="django.db.backends.postgresql", HOST="192.0.2.10",
PORT="5432", NAME="webvirtcloud", USER="webvirtcloud", PASSWORD="db password")
def test_a_postgresql_database_is_dumped_with_its_password_out_of_the_command_line(self):
def pg_dump(args, env, **kwargs):
Path(args[2].removeprefix("--file=")).write_bytes(b"PGDMP")
return subprocess.CompletedProcess(args, 0, "", "")
with self.postgresql(), patch("shutil.which", return_value="/usr/bin/pg_dump"), \
patch("subprocess.run", side_effect=pg_dump) as run:
backup = self.backup()
args, env = run.call_args.args[0], run.call_args.kwargs["env"]
self.assertEqual(args, ["pg_dump", "--format=custom", f"--file={backup.parent}/.{backup.name}/db.pgdump",
"--host=192.0.2.10", "--port=5432", "--username=webvirtcloud", "--dbname=webvirtcloud"])
self.assertEqual(env["PGPASSWORD"], "db password")
self.assertEqual((backup / "db.pgdump").read_bytes(), b"PGDMP")
self.assertEqual(oct((backup / "db.pgdump").stat().st_mode & 0o777), "0o600")
def test_a_failed_dump_fails_the_backup(self):
failed = subprocess.CompletedProcess([], 1, "", "pg_dump: error: server version mismatch\n")
with self.postgresql(), patch("shutil.which", return_value="/usr/bin/pg_dump"), \
patch("subprocess.run", return_value=failed), self.assertRaisesRegex(CommandError, "server version mismatch"):
self.backup()
self.assertEqual(self.kept(), [])
def test_a_postgresql_database_without_pg_dump_fails_the_backup(self):
with self.postgresql(), patch("shutil.which", return_value=None), \
self.assertRaisesRegex(CommandError, "install the PostgreSQL client"):
self.backup()
self.assertEqual(self.kept(), [])