Phase 6 part 1: database backups
Backups with SQLite's VACUUM INTO into <data>/backups: on request (Settings → Backups, supermarket backup [note]), before every schema migration, and daily, keeping 7 dailies and 4 weeklies. Listed with size and schema; downloadable and deletable. The old app.db.pre-* copies are moved in, with their -wal files folded in first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/backup"
|
||||
"supermarket/internal/db"
|
||||
)
|
||||
|
||||
func (s *Server) backupDir() string { return backup.Dir(s.DataDir) }
|
||||
|
||||
// RunBackups makes the daily backup, and prunes old dailies, while the app
|
||||
// runs.
|
||||
func (s *Server) RunBackups(ctx context.Context) {
|
||||
tick := time.NewTicker(time.Hour)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
s.dailyBackup(time.Now())
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-tick.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) dailyBackup(now time.Time) {
|
||||
due, err := backup.DueDaily(s.backupDir(), now)
|
||||
if err != nil {
|
||||
s.Log.Warn("backups: listing", "err", err)
|
||||
return
|
||||
}
|
||||
if !due {
|
||||
return
|
||||
}
|
||||
if _, err := backup.Make(s.DB.DB, s.backupDir(), backup.Daily, "", now); err != nil {
|
||||
s.Log.Error("backups: the daily backup failed", "err", err)
|
||||
return
|
||||
}
|
||||
if n, err := backup.Prune(s.backupDir()); err != nil {
|
||||
s.Log.Warn("backups: pruning", "err", err)
|
||||
} else if n > 0 {
|
||||
s.Log.Info("backups: removed old daily backups", "count", n)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) backupsPage(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderBackups(w, r, "", "")
|
||||
}
|
||||
|
||||
func (s *Server) renderBackups(w http.ResponseWriter, r *http.Request, errMsg, flash string) {
|
||||
bs, err := backup.List(s.backupDir())
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
var total int64
|
||||
for _, b := range bs {
|
||||
total += b.Size
|
||||
}
|
||||
keep := backup.Keep(bs)
|
||||
hostDir := s.backupDir()
|
||||
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,
|
||||
}})
|
||||
}
|
||||
|
||||
func (s *Server) backupsAction(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
switch r.PathValue("action") {
|
||||
case "now":
|
||||
b, err := backup.Make(s.DB.DB, s.backupDir(), backup.Manual, r.PostFormValue("note"), time.Now())
|
||||
if err != nil {
|
||||
s.renderBackups(w, r, err.Error(), "")
|
||||
return
|
||||
}
|
||||
s.renderBackups(w, r, "", "Backed up to "+b.Name+".")
|
||||
case "delete":
|
||||
name := r.PostFormValue("name")
|
||||
if err := backup.Delete(s.backupDir(), name); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
s.renderBackups(w, r, "There's no backup called "+name+".", "")
|
||||
return
|
||||
}
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
s.renderBackups(w, r, "", "Deleted "+name+".")
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// backupDownload sends a backup file. It holds contacts and the app's
|
||||
// data (not its secrets), so it's served to this machine only, like
|
||||
// everything else here.
|
||||
func (s *Server) backupDownload(w http.ResponseWriter, r *http.Request) {
|
||||
b, err := backup.Find(s.backupDir(), r.PathValue("name"))
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/vnd.sqlite3")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="`+b.Name+`"`)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
http.ServeFile(w, r, b.Path)
|
||||
}
|
||||
Reference in New Issue
Block a user