feat(backup): restore at first-run setup, restore --in-place, docs lead with the UI (#5818, #5819)
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 1m28s
CI / unit (push) Successful in 1m37s
CI / publish (push) Successful in 1m17s

#5818 "First-run setup: Restore from a backup, by upload": beside creating
the first admin, setup offers "Or restore from a backup". Only while no
user exists; the upload is checked (a newer backup or one whose key can't
open its secrets is refused and not kept), then restored with no safety
backup (nothing to keep), and Steward restarts into the backup's users.

#5819 "Docs and CI": backups.md leads with restoring from Settings →
Backups and from setup; `steward restore --in-place` does the same-schema
restore from the shell, and CI's image check now uses it with a real
pg_restore: a changed setting comes back and the audit log stays.

Also exempts errors/restoring.html from the breadcrumb guard, as with
the other whole-app error pages: it replaces every page while a restore
runs, so there is nowhere to trail back to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-15 11:05:39 -04:00
co-authored by Claude Opus 5.5
parent fb99f8b551
commit e445c83e67
9 changed files with 225 additions and 19 deletions
+21 -7
View File
@@ -125,24 +125,37 @@ def check_backup_command(ctx: click.Context, backup_file: Path) -> None:
@click.argument("backup_file", type=click.Path(exists=True, dir_okay=False, path_type=Path))
@click.option("--wipe", is_flag=True, default=False,
help="Drop everything in the database first. Without it the database must be empty.")
@click.option("--in-place", "in_place", is_flag=True, default=False,
help="Replace what the backup carries in a database of the same schema, keeping "
"the audit log and history (what Settings → Backups does).")
@click.pass_context
def restore_command(ctx: click.Context, backup_file: Path, wipe: bool) -> None:
def restore_command(ctx: click.Context, backup_file: Path, wipe: bool, in_place: bool) -> None:
"""Bring Steward back from a backup file into its (empty) database, and
put the backup's secret key in place. Start Steward afterwards: it runs
any migrations the backup is behind on."""
any migrations the backup is behind on. With --in-place, the database
keeps its history and only what the backup carries is replaced."""
from .backup.dump import BackupError
from .backup.restore import restore
from .backup.restore import restore, restore_in_place
from .config import _SECRET_KEY_FILE, _env, load_bootstrap
if wipe and in_place:
raise click.UsageError("--wipe and --in-place don't go together.")
boot = load_bootstrap(Path(ctx.obj["config"]))
plugin_dirs = [Path(d) for d in boot["plugin_dirs"]]
try:
done = asyncio.run(restore(backup_file, boot["database_url"], wipe=wipe,
plugin_dirs=[Path(d) for d in boot["plugin_dirs"]]))
if in_place:
done = asyncio.run(restore_in_place(backup_file, boot["database_url"], plugin_dirs=plugin_dirs))
else:
done = asyncio.run(restore(backup_file, boot["database_url"], wipe=wipe, plugin_dirs=plugin_dirs))
except BackupError as exc:
raise click.ClickException(str(exc)) from None
made = done.manifest.get("created_at", "?")
click.echo(f"Restored the backup made {made} (Steward {done.manifest.get('steward_version', '?')}).")
if done.fixed:
if in_place:
click.echo(f"Replaced {len(done.tables)} tables ({done.rows} rows); the audit log and history stayed.")
if done.fixed:
click.echo(f"Put right {done.fixed} rows that pointed at something the backup doesn't have.")
elif done.fixed:
click.echo(f"Cleared {done.fixed} references to rows a backup leaves out (e.g. Ansible runs).")
env_key = _env("SECRET_KEY")
if env_key:
@@ -155,4 +168,5 @@ def restore_command(ctx: click.Context, backup_file: Path, wipe: bool) -> None:
_SECRET_KEY_FILE.write_text(done.secret_key)
_SECRET_KEY_FILE.chmod(0o600)
click.echo(f"Wrote the backup's secret key to {_SECRET_KEY_FILE}.")
click.echo("Start Steward now; it brings the database up to date on start.")
click.echo("Restart Steward now so it loads them." if in_place
else "Start Steward now; it brings the database up to date on start.")