Phase 6 part 2: restore from a backup
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 <noreply@anthropic.com>
This commit is contained in:
+85
-3
@@ -3,9 +3,11 @@ package web
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/backup"
|
||||
@@ -68,9 +70,12 @@ func (s *Server) renderBackups(w http.ResponseWriter, r *http.Request, errMsg, f
|
||||
if s.MCPSocket != "" { // the data folder's path on the host
|
||||
hostDir = filepath.Join(filepath.Dir(s.MCPSocket), "backups")
|
||||
}
|
||||
s.render(w, r, "backups", page{Title: "Backups", Nav: "settings/backups", Error: errMsg, Flash: flash, Data: map[string]any{
|
||||
"Backups": bs, "Total": total, "Keep": keep, "Schema": db.Schema(), "Dir": hostDir,
|
||||
}})
|
||||
data := map[string]any{"Backups": bs, "Total": total, "Keep": keep, "Schema": db.Schema(), "Dir": hostDir,
|
||||
"Pending": backup.Pending(s.DataDir), "CanRestart": s.Restart != nil}
|
||||
if last := backup.LastRestore(s.DataDir); last != nil && time.Since(last.At) < 24*time.Hour {
|
||||
data["Restored"] = last
|
||||
}
|
||||
s.render(w, r, "backups", page{Title: "Backups", Nav: "settings/backups", Error: errMsg, Flash: flash, Data: data})
|
||||
}
|
||||
|
||||
func (s *Server) backupsAction(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -86,6 +91,16 @@ func (s *Server) backupsAction(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
s.renderBackups(w, r, "", "Backed up to "+b.Name+".")
|
||||
case "restore":
|
||||
b, err := backup.Find(s.backupDir(), r.PostFormValue("name"))
|
||||
if err != nil {
|
||||
s.renderBackups(w, r, "There's no backup called "+r.PostFormValue("name")+".", "")
|
||||
return
|
||||
}
|
||||
s.restore(w, r, b.Path)
|
||||
case "cancel":
|
||||
backup.Cancel(s.DataDir)
|
||||
s.renderBackups(w, r, "", "The restore is cancelled.")
|
||||
case "delete":
|
||||
name := r.PostFormValue("name")
|
||||
if err := backup.Delete(s.backupDir(), name); err != nil {
|
||||
@@ -116,3 +131,70 @@ func (s *Server) backupDownload(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
http.ServeFile(w, r, b.Path)
|
||||
}
|
||||
|
||||
// restore stages a restore from a backup file, then restarts the app to
|
||||
// apply it. Without a way to restart (tests, or not under Docker) it's
|
||||
// left staged for the next start.
|
||||
func (s *Server) restore(w http.ResponseWriter, r *http.Request, path string) {
|
||||
res, err := backup.Stage(s.DB.DB, s.DataDir, path, db.Schema(), time.Now())
|
||||
if err != nil {
|
||||
s.renderBackups(w, r, "Not restored: "+err.Error(), "")
|
||||
return
|
||||
}
|
||||
msg := "Restoring " + res.From + ". The current database is backed up as " + res.Backup + "."
|
||||
if s.Restart == nil {
|
||||
s.renderBackups(w, r, "", msg+" It's applied when Supermarket next starts.")
|
||||
return
|
||||
}
|
||||
s.Log.Info("backups: restoring; restarting", "from", res.From)
|
||||
// Reload once the app is back: Docker starts it again in a few seconds.
|
||||
w.Header().Set("Refresh", "8; url=/settings/backups")
|
||||
s.renderBackups(w, r, "", msg+" Supermarket is restarting to apply it; this page reloads by itself.")
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
s.Restart()
|
||||
}()
|
||||
}
|
||||
|
||||
// maxUpload is the biggest backup that can be uploaded.
|
||||
const maxUpload = 4 << 30
|
||||
|
||||
// backupUpload takes a backup file from the browser (one downloaded from
|
||||
// here, perhaps on another machine) and adds it to the list, checked;
|
||||
// restoring it is a second step.
|
||||
func (s *Server) backupUpload(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxUpload)
|
||||
f, hdr, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
s.renderBackups(w, r, "Choose a backup file to upload.", "")
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
if err := os.MkdirAll(s.backupDir(), 0o700); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
tmp, err := os.CreateTemp(s.backupDir(), ".upload-*.db")
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
defer os.Remove(tmp.Name())
|
||||
if _, err := io.Copy(tmp, f); err != nil {
|
||||
tmp.Close()
|
||||
s.renderBackups(w, r, "The upload didn't finish: "+err.Error(), "")
|
||||
return
|
||||
}
|
||||
tmp.Close()
|
||||
if err := backup.Check(tmp.Name()); err != nil {
|
||||
s.renderBackups(w, r, hdr.Filename+" isn't a backup Supermarket can use: "+err.Error(), "")
|
||||
return
|
||||
}
|
||||
note := "uploaded " + strings.TrimSuffix(filepath.Base(hdr.Filename), ".db")
|
||||
b, err := backup.Adopt(tmp.Name(), s.backupDir(), note, time.Now())
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
s.renderBackups(w, r, "", "Uploaded as "+b.Name+". Restore it from the list when you're ready.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user