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:
@@ -15,6 +15,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -28,6 +29,7 @@ import (
|
||||
"time"
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/backup"
|
||||
"supermarket/internal/content"
|
||||
"supermarket/internal/core"
|
||||
"supermarket/internal/crm"
|
||||
@@ -47,6 +49,13 @@ func main() {
|
||||
fmt.Println(version)
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 && (os.Args[1] == "backup" || os.Args[1] == "backups") {
|
||||
if err := backupCommand(os.Args[1], os.Args[2:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
log := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
if err := run(log); err != nil {
|
||||
log.Error("stopped", "err", err)
|
||||
@@ -76,6 +85,19 @@ func run(log *slog.Logger) error {
|
||||
if err := os.MkdirAll(dataDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
// Old hand-made copies (app.db.pre-*) join the backups.
|
||||
if n := backup.AdoptLegacy(dataDir, func(name string, err error) {
|
||||
log.Warn("backups: an old copy was left where it is", "file", name, "err", err)
|
||||
}); n > 0 {
|
||||
log.Info("backups: moved old copies into the backups folder", "count", n)
|
||||
}
|
||||
db.BeforeMigrate = func(d *sql.DB, from, to int) error {
|
||||
b, err := backup.Make(d, backup.Dir(dataDir), backup.Migration, fmt.Sprintf("before-schema-%d", to), time.Now())
|
||||
if err == nil {
|
||||
log.Info("backups: backed up before migrating", "from", from, "to", to, "file", b.Name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
database, err := db.Open(filepath.Join(dataDir, "app.db"))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -162,6 +184,7 @@ func run(log *slog.Logger) error {
|
||||
go srv.RunTracking(ctx)
|
||||
go srv.RunSEO(ctx)
|
||||
go srv.RunMetrics(ctx)
|
||||
go srv.RunBackups(ctx)
|
||||
// A sync falls due, and a claim times out, without anything being
|
||||
// written: refresh the state file each minute.
|
||||
go func() {
|
||||
@@ -220,3 +243,45 @@ func mcpToken(path string) (string, error) {
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// backupCommand runs `supermarket backup [note]` and `supermarket backups`
|
||||
// (inside the container: the host's command runs them with docker exec).
|
||||
// It reads the database beside the running app, which WAL allows; it
|
||||
// never migrates it.
|
||||
func backupCommand(cmd string, args []string) error {
|
||||
dataDir := env("SUPERMARKET_DATA", "/data")
|
||||
dir := backup.Dir(dataDir)
|
||||
if cmd == "backups" {
|
||||
bs, err := backup.List(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(bs) == 0 {
|
||||
fmt.Println("No backups yet.")
|
||||
return nil
|
||||
}
|
||||
for _, b := range bs {
|
||||
schema := fmt.Sprintf("schema %d", b.Schema)
|
||||
if b.Schema < 0 {
|
||||
schema = "unreadable"
|
||||
}
|
||||
fmt.Printf("%-60s %8.1f MB %s\n", b.Name, float64(b.Size)/(1<<20), schema)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
path := filepath.Join(dataDir, "app.db")
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return fmt.Errorf("no database at %s", path)
|
||||
}
|
||||
d, err := sql.Open("sqlite", "file:"+path+"?_pragma=busy_timeout(10000)")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer d.Close()
|
||||
b, err := backup.Make(d, dir, backup.Manual, strings.Join(args, " "), time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Backed up to %s (%.1f MB)\n", b.Name, float64(b.Size)/(1<<20))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user