#!/usr/bin/env python3 """Back up the running MOYI database and its configuration before an upgrade.""" import argparse import hashlib import importlib.util import json import os from pathlib import Path import shutil import subprocess import sys import tempfile from datetime import datetime, timezone from admin_credentials import MaintenanceError, check_container, postgres_environment, require, run, write_private def backup(deploy, mode, output_root, database="moyi_control"): require(database == "moyi_control", "This upgrade backup is restricted to moyi_control.") programs = {name: shutil.which(name) for name in ("psql", "pg_dump", "pg_restore")} require(all(programs.values()), "Install postgresql-client before backing up.") env_file = deploy.HERE / ".env.cloud" config = deploy.preflight(env_file, mode) check_container(deploy, config, env_file, mode) pg_env = postgres_environment(config["services"]["moyi-api"]["environment"], database) actual = run([programs["psql"], "-X", "--no-password", "-qAt", "-v", "ON_ERROR_STOP=1", "-c", "SELECT current_database()"], env=pg_env) require(actual == database, "Connected database does not match moyi_control.") original = env_file.read_bytes() output_root.mkdir(mode=0o700, parents=True, exist_ok=True) destination = Path(tempfile.mkdtemp(prefix="pre-upgrade-", dir=output_root)) print("Private backup directory: " + str(destination), flush=True) write_private(destination / "env.cloud", original) if mode == "https": for name in ("fullchain.pem", "privkey.pem"): write_private(destination / name, (deploy.HERE / "certs" / name).read_bytes()) dump = destination / "moyi_control.dump" run([programs["pg_dump"], "--no-password", "--format=custom", "--quote-all-identifiers", "--lock-wait-timeout=10000", "--file=" + str(dump)], env=pg_env) require(dump.is_file() and dump.stat().st_size > 0, "Database backup is empty.") # Read/decompress the whole archive without connecting to any database. run([programs["pg_restore"], "--file=" + os.devnull, str(dump)]) require(env_file.read_bytes() == original, "Configuration changed during backup; repeat the backup.") digests = {} for item in destination.iterdir(): with item.open("rb") as stream: digests[item.name] = hashlib.file_digest(stream, "sha256").hexdigest() write_private(destination / "SHA256SUMS", "".join( value + " " + name + "\n" for name, value in sorted(digests.items())).encode("ascii")) # This marker is written only after export and full archive validation succeed. write_private(destination / "backup-complete.json", json.dumps({ "database": database, "deployment": str(deploy.HERE), "completedAt": datetime.now(timezone.utc).isoformat(), "sha256": digests, }, indent=2).encode("utf-8")) print("BACKUP_OK: moyi_control exported; archive read successfully; credentials not printed.") return destination def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--deployment-dir", type=Path, required=True) parser.add_argument("--mode", choices=("https", "tunnel"), default="https") parser.add_argument("--output-root", type=Path, default=Path("/opt/moyi/backups")) args = parser.parse_args() os.umask(0o077) directory = args.deployment_dir.resolve() spec = importlib.util.spec_from_file_location("running_moyi_deploy", directory / "deploy.py") deploy = importlib.util.module_from_spec(spec) spec.loader.exec_module(deploy) backup(deploy, args.mode, args.output_root.resolve()) if __name__ == "__main__": try: main() except (MaintenanceError, ValueError, OSError, subprocess.SubprocessError) as error: message = str(error) if isinstance(error, MaintenanceError) else type(error).__name__ + ": backup failed; no secret output shown." print("ERROR: " + message, file=sys.stderr) sys.exit(1)