// Package backup implementiert BAK-01: automatisierte, inkrementelle // Sicherung der PostgreSQL-Datenbank per pg_basebackup (PostgreSQL 17s // natives inkrementelles Backup über WAL-Summarization, siehe // `summarize_wal`), mit Verifikation jeder Sicherung und // generationsbasierter Rotation. Kein pg_dump-basierter Ansatz, weil // pg_dump ausschließlich logische Vollsicherungen kennt — "inkrementell" // im Sinne des Tickets erfordert das physische, WAL-summary-gestützte // Verfahren aus PostgreSQL 17. package backup import ( "context" "fmt" "os" "os/exec" "path/filepath" "time" ) // Config enthält die Verbindungsdaten für pg_basebackup — ausschließlich // über Umgebungsvariablen befüllt, nie im Code (siehe Ticket-Abschluss- // Regel). type Config struct { Host string Port string User string Password string BackupDir string PgBaseBackupPath string // Default "pg_basebackup", überschreibbar für Tests } func (c Config) binary() string { if c.PgBaseBackupPath != "" { return c.PgBaseBackupPath } return "pg_basebackup" } // FullBackupDirName/IncrementalDirName sind die festen Unterverzeichnis- // namen je Generation. const ( FullBackupDirName = "full" IncrementalSubdir = "incremental" BackupManifestFile = "backup_manifest" BaseTarGzFile = "base.tar.gz" ) // NewGenerationID liefert eine sortierbare, eindeutige Generation-Kennung // (RFC3339-artig, dateisystemtauglich) — Generationen werden anhand dieser // Kennung chronologisch sortiert (Rotate, ListGenerations). func NewGenerationID(t time.Time) string { return t.UTC().Format("20060102T150405Z") } // FullBackup erstellt eine neue Vollsicherung (Akzeptanzkriterium 1) als // eigene Generation. Liefert den Pfad zum backup_manifest, das spätere // IncrementalBackup-Aufrufe als Referenz brauchen. func FullBackup(ctx context.Context, cfg Config, generationID string) (manifestPath string, err error) { dir := filepath.Join(cfg.BackupDir, generationID, FullBackupDirName) if err := os.MkdirAll(filepath.Dir(dir), 0o750); err != nil { return "", fmt.Errorf("backup: generationsverzeichnis anlegen: %w", err) } args := []string{ "-h", cfg.Host, "-p", cfg.Port, "-U", cfg.User, "-D", dir, "-Ft", "-z", "--checkpoint=fast", "--no-password", } if err := runPgBaseBackup(ctx, cfg, args); err != nil { return "", fmt.Errorf("backup: vollsicherung: %w", err) } return filepath.Join(dir, BackupManifestFile), nil } // IncrementalBackup erstellt eine inkrementelle Sicherung gegen die zuletzt // bekannte Vollsicherung ODER die letzte Inkrement-Sicherung (priorManifestPath // zeigt jeweils auf das backup_manifest der Referenz). func IncrementalBackup(ctx context.Context, cfg Config, generationID, incrementID, priorManifestPath string) (manifestPath string, err error) { dir := filepath.Join(cfg.BackupDir, generationID, IncrementalSubdir, incrementID) if err := os.MkdirAll(filepath.Dir(dir), 0o750); err != nil { return "", fmt.Errorf("backup: inkrement-verzeichnis anlegen: %w", err) } args := []string{ "-h", cfg.Host, "-p", cfg.Port, "-U", cfg.User, "-D", dir, "-Ft", "-z", "--checkpoint=fast", "--no-password", "--incremental=" + priorManifestPath, } if err := runPgBaseBackup(ctx, cfg, args); err != nil { return "", fmt.Errorf("backup: inkrementelle sicherung: %w", err) } return filepath.Join(dir, BackupManifestFile), nil } func runPgBaseBackup(ctx context.Context, cfg Config, args []string) error { cmd := exec.CommandContext(ctx, cfg.binary(), args...) cmd.Env = append(os.Environ(), "PGPASSWORD="+cfg.Password) output, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("%s fehlgeschlagen: %w (ausgabe: %s)", cfg.binary(), err, string(output)) } return nil }