Python control-panel package driving the full provisioning flow from a site's site.yaml (docs/05): - provision: ZFS web dataset + ownership, per-site DB + least-priv user, generated .env encrypted to secrets.enc.yaml (SOPS/age), render the profile templates + persist site.yaml, per-customer chrooted SFTP account, docker compose up. - deprovision (gated: data destroyed only with --purge, after a final backup), backup (ZFS snapshot + mariadb-dump), restore (rollback + import), render (preview), list. Design: one command/file runner with a real --dry-run (prints every action, redacts secrets); idempotent steps; Config + Site validation mirroring docs/03; passwords never logged. Modules: cli, config, naming, context, render, runner, zfs, database, secrets, sftp, compose, provision, backup. Plus pyproject (heleosctl entry point), config.example.yaml, an example site, and a README. Tests: 22 pure-logic unit tests (naming, config validation, template render across all profiles + db on/off) — all passing. Full provision and deprovision verified end-to-end in --dry-run. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""Backup/restore for a single site: ZFS snapshot for files, mariadb-dump for DB.
|
|
|
|
Two decoupled streams (see docs/06-backup-and-dr.md). Offsite `zfs send` / rsync
|
|
are scheduled separately (Phase 5); this module covers the on-host operations the
|
|
CLI drives.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
from . import database, naming, zfs
|
|
from .config import Config, Site
|
|
from .runner import Runner
|
|
|
|
|
|
def db_backup_dir(cfg: Config, site: Site) -> str:
|
|
return f"/{cfg.pool}/platform/db-backups/{site.customer}/{site.site}"
|
|
|
|
|
|
def dump_database(runner: Runner, cfg: Config, site: Site) -> str | None:
|
|
if not site.database:
|
|
return None
|
|
db = naming.database_name(site.customer, site.site)
|
|
ts = datetime.now().strftime("%Y%m%d-%H%M%S")
|
|
out_dir = db_backup_dir(cfg, site)
|
|
dest = f"{out_dir}/{db}-{ts}.sql"
|
|
runner.mkdir(out_dir)
|
|
pw = "<redacted>" if runner.dry_run else database.root_password(cfg)
|
|
sql = runner.run(
|
|
["docker", "exec", cfg.mariadb_container, "mariadb-dump",
|
|
"--single-transaction", "--databases", db, "-uroot", f"-p{pw}"],
|
|
capture=True, secret=True,
|
|
)
|
|
if not runner.dry_run:
|
|
runner.write_file(dest, sql)
|
|
else:
|
|
runner._echo(f"DRY write {dest}")
|
|
return dest
|
|
|
|
|
|
def backup_site(runner: Runner, cfg: Config, site: Site) -> dict:
|
|
snap = zfs.snapshot(runner, zfs.web_dataset(cfg, site))
|
|
dump = dump_database(runner, cfg, site)
|
|
return {"snapshot": snap, "dump": dump}
|
|
|
|
|
|
def restore_files(runner: Runner, cfg: Config, site: Site, snapshot: str) -> None:
|
|
ds = zfs.web_dataset(cfg, site)
|
|
name = snapshot if "@" in snapshot else f"{ds}@{snapshot}"
|
|
# -r discards snapshots newer than the target; acceptable for a deliberate restore.
|
|
runner.run(["zfs", "rollback", "-r", name])
|
|
|
|
|
|
def restore_database(runner: Runner, cfg: Config, site: Site, dump_path: str) -> None:
|
|
if not site.database:
|
|
return
|
|
pw = "<redacted>" if runner.dry_run else database.root_password(cfg)
|
|
data = "" if runner.dry_run else Path(dump_path).read_text(encoding="utf-8")
|
|
runner.run(
|
|
["docker", "exec", "-i", cfg.mariadb_container, "mariadb", "-uroot", f"-p{pw}"],
|
|
input=data, secret=True,
|
|
)
|