feat(backup): check a backup against what is in place, changing nothing (#5823)
#5823 "Check a backup: compare what it would restore with what is in place, without restoring". - steward/backup/check.py: pg_restore writes the backup's rows as COPY text with no database involved. The rows in place go out through COPY in the same form. Tables are compared on their primary keys, over the columns both sides have, and grouped by area. - Sealed values are opened with each side's own key and compared as plaintext digests, so a secret sealed twice still matches. They are never shown. A backup whose key can't open its own secrets fails the check. - What Steward records as it runs (last-seen times, a schedule's last run, backup history, scraped UniFi fields) is left out of the comparison: contents.ACTIVITY, STATE_SETTINGS and HAND_SET_ONLY. - Settings → Backups: - a Check button on each sent backup, which fetches it from the bucket (new s3.get_file); - "Check a file" to upload one, with the size limit raised before the CSRF check reads the form; - a results page. - Each backup Steward sends checks itself once written, and its history row says how that went. - New CLI command `steward check-backup FILE`. CI's image smoke step checks a real backup against its source and expects a match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -81,6 +81,41 @@ def backup_command(ctx: click.Context, out_dir: Path) -> None:
|
||||
click.echo(f"Wrote {made.path} ({made.size} bytes). It holds the secret key: keep it safe.")
|
||||
|
||||
|
||||
@main.command("check-backup")
|
||||
@click.argument("backup_file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
|
||||
@click.pass_context
|
||||
def check_backup_command(ctx: click.Context, backup_file: Path) -> None:
|
||||
"""Compare a backup file with this Steward's database, changing nothing:
|
||||
what a restore would bring back, area by area. Exits 1 when the backup
|
||||
can't be read or its key doesn't open its secrets."""
|
||||
from .backup.check import check_backup
|
||||
from .backup.dump import BackupError
|
||||
from .config import load_bootstrap
|
||||
|
||||
boot = load_bootstrap(Path(ctx.obj["config"]))
|
||||
try:
|
||||
report = asyncio.run(check_backup(backup_file, boot["database_url"], boot["secret_key"],
|
||||
plugin_dirs=[Path(d) for d in boot["plugin_dirs"]]))
|
||||
except BackupError as exc:
|
||||
raise click.ClickException(str(exc)) from None
|
||||
click.echo(report.summary())
|
||||
click.echo(f"Made {report.manifest.get('created_at', '?')} by Steward "
|
||||
f"{report.manifest.get('steward_version', '?')}; schema: {report.schema}; "
|
||||
f"secret key: {'the one in use' if report.key_same else 'a different one'}.")
|
||||
for area in report.areas:
|
||||
for t in area.tables:
|
||||
for d in t.differ:
|
||||
click.echo(f" {area.name}: {d.label} differs ({', '.join(d.columns)})")
|
||||
for label in t.only_backup:
|
||||
click.echo(f" {area.name}: {label} is only in the backup")
|
||||
for label in t.only_here:
|
||||
click.echo(f" {area.name}: {label} is only here")
|
||||
if t.note:
|
||||
click.echo(f" {area.name} ({t.table}): {t.note}")
|
||||
if not report.ok:
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
@main.command("restore")
|
||||
@click.argument("backup_file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
|
||||
@click.option("--wipe", is_flag=True, default=False,
|
||||
|
||||
Reference in New Issue
Block a user