package main import ( "flag" "fmt" "path/filepath" "git.wntrmute.dev/kyle/mcias/internal/config" "git.wntrmute.dev/kyle/mcias/internal/db" ) // runSnapshot handles the "snapshot" command. // // It opens the database read-only (no master key derivation needed — VACUUM // INTO does not access encrypted columns) and writes a timestamped backup to // /srv/mcias/backups/ (or the directory adjacent to the configured DB path). // Backups older than --retain-days are pruned. func runSnapshot(configPath string, args []string) { fs := flag.NewFlagSet("snapshot", flag.ExitOnError) retainDays := fs.Int("retain-days", 30, "prune backups older than this many days (0 = keep all)") if err := fs.Parse(args); err != nil { fatalf("snapshot: %v", err) } cfg, err := config.Load(configPath) if err != nil { fatalf("snapshot: load config: %v", err) } database, err := db.Open(cfg.Database.Path) if err != nil { fatalf("snapshot: open database: %v", err) } defer func() { _ = database.Close() }() // Place backups in a "backups" directory adjacent to the database file. backupDir := filepath.Join(filepath.Dir(cfg.Database.Path), "backups") dest, err := database.SnapshotDir(backupDir, *retainDays) if err != nil { fatalf("snapshot: %v", err) } fmt.Printf("snapshot written: %s\n", dest) }