Repository Public
1package main23import (4 "context"5 "flag"6 "fmt"7 "os"8 "os/exec"9 "path/filepath"10 "sort"11 "strings"12 "time"1314 "github.com/google/uuid"15 "github.com/jackc/pgx/v5"16 "github.com/jackc/pgx/v5/pgxpool"1718 "clustr.spboucher.ai/mcc/internal/backup"19 "clustr.spboucher.ai/mcc/internal/config"20)2122func init() {23 extraCommands["backup"] = cmdBackup24 extraCommands["backup-verify"] = cmdBackupVerify25 extraCommands["restore"] = cmdRestore26 extraCommands["restore-test"] = cmdRestoreTest27}2829func backupKey(cfg *config.Config) ([]byte, error) {30 path := cfg.SecretPath("backup.key")31 key, created, err := backup.LoadOrCreateKey(path)32 if err != nil {33 return nil, err34 }35 if created {36 fmt.Fprintf(os.Stderr, "\nA new backup key was created: %s\nCOPY IT OFF THIS HOST NOW (password manager or another machine). Without it, backups cannot be restored after a disk loss.\n\n", path)37 }38 return key, nil39}4041func cmdBackup(ctx context.Context, cfg *config.Config, pool *pgxpool.Pool, args []string) error {42 fs := flag.NewFlagSet("backup", flag.ExitOnError)43 dir := fs.String("dir", "", "directory receiving the backup files")44 keep := fs.Int("keep", 14, "number of local backups kept")45 copyTo := fs.String("copy-to", "", "rsync destination for an off-host copy (host:dir)")46 fs.Parse(args)47 if *dir == "" {48 return fmt.Errorf("--dir is required")49 }50 key, err := backupKey(cfg)51 if err != nil {52 return err53 }54 res, err := backup.Create(ctx, cfg.DatabaseURL, *dir, key)55 if err == nil {56 // A backup that cannot be read back is not a backup: verify before declaring success.57 _, err = backup.Verify(ctx, res.Path, key)58 }59 detail := ""60 if err == nil && *copyTo != "" {61 out, cerr := exec.CommandContext(ctx, "rsync", "-a", res.Path, *copyTo+"/").CombinedOutput()62 if cerr != nil {63 err = fmt.Errorf("off-host copy to %s failed: %v: %s", *copyTo, cerr, strings.TrimSpace(string(out)))64 } else {65 detail = "off-host copy: " + *copyTo66 }67 } else if err == nil {68 detail = "local only (no off-host copy configured)"69 }70 if rerr := backup.Record(ctx, pool, "postgres", "pg_dump + AES-256-GCM", res, err, detail); rerr != nil {71 fmt.Fprintln(os.Stderr, "warning: could not record the outcome:", rerr)72 }73 if err != nil {74 return err75 }76 removed, _ := backup.Prune(*dir, *keep)77 fmt.Printf("backup %s %d bytes sha256=%s %.1fs verified pruned=%d %s\n", filepath.Base(res.Path), res.Size, res.SHA256, res.Duration.Seconds(), len(removed), detail)78 return nil79}8081func cmdBackupVerify(ctx context.Context, cfg *config.Config, _ *pgxpool.Pool, args []string) error {82 fs := flag.NewFlagSet("backup-verify", flag.ExitOnError)83 file := fs.String("file", "", "backup file")84 fs.Parse(args)85 key, err := backupKey(cfg)86 if err != nil {87 return err88 }89 n, err := backup.Verify(ctx, *file, key)90 if err != nil {91 return err92 }93 fmt.Printf("authentic and readable: %d catalogue entries\n", n)94 return nil95}9697func cmdRestore(ctx context.Context, cfg *config.Config, _ *pgxpool.Pool, args []string) error {98 fs := flag.NewFlagSet("restore", flag.ExitOnError)99 file := fs.String("file", "", "backup file")100 to := fs.String("to", "", "URL of an EMPTY target database")101 fs.Parse(args)102 if *file == "" || *to == "" {103 return fmt.Errorf("--file and --to are required")104 }105 key, err := backupKey(cfg)106 if err != nil {107 return err108 }109 if err := backup.Restore(ctx, *file, key, *to); err != nil {110 return err111 }112 c, err := backup.Inspect(ctx, *to)113 if err != nil {114 return err115 }116 fmt.Printf("restored: migrations=%d users=%d nodes=%d audit_rows=%d audit_chain_ok=%v\n", c.Migrations, c.Users, c.Nodes, c.AuditRows, c.AuditOK)117 if !c.AuditOK {118 return fmt.Errorf("the restored audit chain does not verify")119 }120 return nil121}122123func latestBackup(dir string) (string, error) {124 m, err := filepath.Glob(filepath.Join(dir, "mcc-*.dump.enc"))125 if err != nil || len(m) == 0 {126 return "", fmt.Errorf("no backup found in %s", dir)127 }128 sort.Strings(m)129 return m[len(m)-1], nil130}131132// cmdRestoreTest proves a backup is restorable: it restores into a scratch133// database on the same server, verifies the audit chain and basic counts,134// then drops the scratch database. The live database is only read.135func cmdRestoreTest(ctx context.Context, cfg *config.Config, pool *pgxpool.Pool, args []string) error {136 fs := flag.NewFlagSet("restore-test", flag.ExitOnError)137 file := fs.String("file", "", "backup file (default: latest in --dir)")138 dir := fs.String("dir", "", "backup directory")139 fs.Parse(args)140 if *file == "" {141 if *dir == "" {142 return fmt.Errorf("--file or --dir is required")143 }144 var err error145 if *file, err = latestBackup(*dir); err != nil {146 return err147 }148 }149 key, err := backupKey(cfg)150 if err != nil {151 return err152 }153 base, err := pgx.ParseConfig(cfg.DatabaseURL)154 if err != nil {155 return err156 }157 scratch := "mcc_restore_test_" + strings.ReplaceAll(uuid.NewString()[:8], "-", "")158 if _, err := pool.Exec(ctx, "create database "+scratch); err != nil {159 return fmt.Errorf("create scratch database: %w", err)160 }161 defer func() {162 c, cancel := context.WithTimeout(context.Background(), 30*time.Second)163 defer cancel()164 pool.Exec(c, "drop database if exists "+scratch+" with (force)")165 }()166 target := strings.Replace(cfg.DatabaseURL, "/"+base.Database, "/"+scratch, 1)167 start := time.Now()168 if err := backup.Restore(ctx, *file, key, target); err != nil {169 backup.Record(ctx, pool, "postgres-restore-test", "restore into scratch database", nil, err, "")170 return err171 }172 c, err := backup.Inspect(ctx, target)173 if err == nil && !c.AuditOK {174 err = fmt.Errorf("restored audit chain does not verify")175 }176 if err == nil && c.Migrations == 0 {177 err = fmt.Errorf("restored database has no schema (migrations=%d)", c.Migrations)178 }179 st, _ := os.Stat(*file)180 res := &backup.Result{Path: *file, Duration: time.Since(start)}181 if st != nil {182 res.Size = st.Size()183 }184 backup.Record(ctx, pool, "postgres-restore-test", "restore into scratch database", res, err,185 fmt.Sprintf("%s: migrations=%d users=%d nodes=%d audit_rows=%d", filepath.Base(*file), c.Migrations, c.Users, c.Nodes, c.AuditRows))186 if err != nil {187 return err188 }189 fmt.Printf("restore test OK in %.1fs — %s: migrations=%d users=%d nodes=%d audit_rows=%d (chain verified)\n",190 time.Since(start).Seconds(), filepath.Base(*file), c.Migrations, c.Users, c.Nodes, c.AuditRows)191 return nil192}193