From fa4792e0fea4246c78a6c37e033eff1e181f64d7 Mon Sep 17 00:00:00 2001 From: David Young Date: Sun, 22 Nov 2026 14:46:36 -0700 Subject: [PATCH] Phase 6 part 2: restore from a backup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A restore is staged (checked, the current database backed up) and swapped in when the app next starts. Settings → Backups restores with a self-restart, cancels a waiting restore, and takes uploads; supermarket restore takes a backup name or a file; supermarket backup --with-secrets makes an archive with the keys, for moving machines. Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 22 ++ TEST.md | 15 ++ bin/supermarket | 33 +++ cmd/supermarket/main.go | 40 +++- internal/backup/backup.go | 24 +++ internal/backup/restore.go | 298 ++++++++++++++++++++++++++++ internal/backup/restore_test.go | 145 ++++++++++++++ internal/web/backups.go | 88 +++++++- internal/web/backups_test.go | 63 ++++++ internal/web/server.go | 2 + internal/web/templates/backups.html | 18 ++ 11 files changed, 744 insertions(+), 4 deletions(-) create mode 100644 internal/backup/restore.go create mode 100644 internal/backup/restore_test.go diff --git a/PLAN.md b/PLAN.md index 655a498..5f289fb 100644 --- a/PLAN.md +++ b/PLAN.md @@ -1402,6 +1402,28 @@ in the WAL, as feared. The live database is 60 MB (mostly Stripe objects), so the backups folder holds about 620 MB; old legacy copies can be deleted from the page. +**Part 2 built (2026-11-22; no schema change).** A restore is staged, +then applied on the next start, since a running app can't swap its own +database: `backup.Stage` checks the file (integrity, a Supermarket +database, schema no newer than the app's), backs up the current database +(kind *restore*), and leaves `restore-pending.db` (plus +`restore-pending.json`); `ApplyPending` runs in `main` before anything +opens the database, re-checks it, removes the old `-wal`/`-shm`, renames +it over `app.db`, and records `restore-done.json`; migrations then run as +usual. From Settings → Backups, *Restore* (asks first) stages it and +stops the app (`Server.Restart`, the signal context's cancel); Docker's +`restart: unless-stopped` starts it again within a second, and the page +reloads (a `Refresh` header). A staged restore can be cancelled until +then. *Upload* takes a `.db` file, checks it and adds it to the list as +one of the user's backups. `supermarket restore ` copies a +host file into the data folder, stages it in the container, and restarts +the app. `supermarket backup --with-secrets` writes a mode-0600 +`app-