1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00
webvirtcloud/appsettings/management/commands/backup.py
catborise 2b5d0ac80e 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.
2026-11-26 14:21:36 +03:00

104 lines
5.2 KiB
Python

import fcntl
import os
import shutil
import sqlite3
import subprocess
from contextlib import closing
from datetime import datetime
from pathlib import Path
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.db import connection
PREFIX = "webvirtcloud-backup-"
class Command(BaseCommand):
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)")
def handle(self, *args, keep, **options):
if keep < 1:
raise CommandError("--keep must be at least 1")
os.umask(0o077) # the database holds compute passwords and keys
backups = settings.BASE_DIR / "backups"
backups.mkdir(mode=0o700, exist_ok=True)
with open(backups / ".lock", "w") as lock:
try:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
raise CommandError("Another backup is running") from None
self.stdout.write(str(self.backup(backups, keep)))
def backup(self, backups, keep):
name = PREFIX + datetime.now().strftime("%Y%m%d-%H%M%S")
partial = backups / f".{name}"
partial.mkdir()
try:
self.copy(partial)
partial.rename(backups / name)
except BaseException:
shutil.rmtree(partial, ignore_errors=True)
raise
# only after a good backup: a failing one never costs an older one;
# the partial ones are what a killed run left behind
# the newest by when they were made, not by name: a name is the local time, which goes back
# an hour in autumn (and was the panel's zone or UTC as an installation's TZ changed);
# by name among those of the same time, which a coarse file system clock gives
made = sorted(backups.glob(PREFIX + "*"), key=lambda path: (path.stat().st_mtime_ns, path.name))
for old in [*made[:-keep], *backups.glob(f".{PREFIX}*")]:
shutil.rmtree(old)
return backups / name
def copy(self, to):
database = connection.settings_dict
if database["ENGINE"] == "django.db.backends.sqlite3":
# the backup API copies a consistent state while the panel writes
read_only = Path(database["NAME"]).resolve().as_uri() + "?mode=ro"
with closing(sqlite3.connect(read_only, uri=True, timeout=30)) as source, \
closing(sqlite3.connect(to / "db.sqlite3")) as copy:
source.backup(copy)
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
(to / "secret_key").write_text(settings.SECRET_KEY + "\n")
data = settings.BASE_DIR / "data"
for source in (data / "node_id", data / "local_settings.py"):
if source.is_file():
shutil.copyfile(source, to / source.name)
ssh = Path.home() / ".ssh"
if ssh.is_dir():
(to / "ssh").mkdir()
for source in ssh.iterdir():
if source.is_file() and not source.is_symlink():
shutil.copyfile(source, to / "ssh" / source.name)
# the TLS client certificates libvirt reads (~/.pki/libvirt); a link stays a link
pki = Path.home() / ".pki"
if pki.is_dir():
shutil.copytree(pki, to / "pki", symlinks=True)
if (settings.BASE_DIR / ".git").exists():
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()}")