feat: add dry-run mode, GFS retention policies, and notifications

- Add --dry-run/-n flag for backup commands with comprehensive preflight checks
  - Database connectivity validation
  - Required tools availability check
  - Storage target and permissions verification
  - Backup size estimation
  - Encryption and cloud storage configuration validation

- Implement GFS (Grandfather-Father-Son) retention policies
  - Daily/Weekly/Monthly/Yearly tier classification
  - Configurable retention counts per tier
  - Custom weekly day and monthly day settings
  - ISO week handling for proper week boundaries

- Add notification system with SMTP and webhook support
  - SMTP email notifications with TLS/STARTTLS
  - Webhook HTTP notifications with HMAC-SHA256 signing
  - Slack-compatible webhook payload format
  - Event types: backup/restore started/completed/failed, cleanup, verify, PITR
  - Configurable severity levels and retry logic

- Update README.md with documentation for all new features
This commit is contained in:
2025-12-13 19:00:54 +01:00
parent 2becde8077
commit d0d83b61ef
15 changed files with 3080 additions and 5 deletions

View File

@@ -48,6 +48,7 @@ var (
encryptBackupFlag bool
encryptionKeyFile string
encryptionKeyEnv string
backupDryRun bool
)
var singleCmd = &cobra.Command{
@@ -123,6 +124,11 @@ func init() {
cmd.Flags().StringVar(&encryptionKeyEnv, "encryption-key-env", "DBBACKUP_ENCRYPTION_KEY", "Environment variable containing encryption key/passphrase")
}
// Dry-run flag for all backup commands
for _, cmd := range []*cobra.Command{clusterCmd, singleCmd, sampleCmd} {
cmd.Flags().BoolVarP(&backupDryRun, "dry-run", "n", false, "Validate configuration without executing backup")
}
// Cloud storage flags for all backup commands
for _, cmd := range []*cobra.Command{clusterCmd, singleCmd, sampleCmd} {
cmd.Flags().String("cloud", "", "Cloud storage URI (e.g., s3://bucket/path) - takes precedence over individual flags")

View File

@@ -9,6 +9,7 @@ import (
"time"
"dbbackup/internal/backup"
"dbbackup/internal/checks"
"dbbackup/internal/config"
"dbbackup/internal/database"
"dbbackup/internal/security"
@@ -28,6 +29,11 @@ func runClusterBackup(ctx context.Context) error {
return fmt.Errorf("configuration error: %w", err)
}
// Handle dry-run mode
if backupDryRun {
return runBackupPreflight(ctx, "")
}
// Check privileges
privChecker := security.NewPrivilegeChecker(log)
if err := privChecker.CheckAndWarn(cfg.AllowRoot); err != nil {
@@ -124,6 +130,16 @@ func runSingleBackup(ctx context.Context, databaseName string) error {
// Update config from environment
cfg.UpdateFromEnvironment()
// Validate configuration
if err := cfg.Validate(); err != nil {
return fmt.Errorf("configuration error: %w", err)
}
// Handle dry-run mode
if backupDryRun {
return runBackupPreflight(ctx, databaseName)
}
// Get backup type and base backup from command line flags (set via global vars in PreRunE)
// These are populated by cobra flag binding in cmd/backup.go
backupType := "full" // Default to full backup if not specified
@@ -148,11 +164,6 @@ func runSingleBackup(ctx context.Context, databaseName string) error {
}
}
// Validate configuration
if err := cfg.Validate(); err != nil {
return fmt.Errorf("configuration error: %w", err)
}
// Check privileges
privChecker := security.NewPrivilegeChecker(log)
if err := privChecker.CheckAndWarn(cfg.AllowRoot); err != nil {
@@ -306,6 +317,11 @@ func runSampleBackup(ctx context.Context, databaseName string) error {
return fmt.Errorf("configuration error: %w", err)
}
// Handle dry-run mode
if backupDryRun {
return runBackupPreflight(ctx, databaseName)
}
// Check privileges
privChecker := security.NewPrivilegeChecker(log)
if err := privChecker.CheckAndWarn(cfg.AllowRoot); err != nil {
@@ -536,3 +552,25 @@ func findLatestClusterBackup(backupDir string) (string, error) {
return latestPath, nil
}
// runBackupPreflight runs preflight checks without executing backup
func runBackupPreflight(ctx context.Context, databaseName string) error {
checker := checks.NewPreflightChecker(cfg, log)
defer checker.Close()
result, err := checker.RunAllChecks(ctx, databaseName)
if err != nil {
return fmt.Errorf("preflight check error: %w", err)
}
// Format and print report
report := checks.FormatPreflightReport(result, databaseName, true)
fmt.Print(report)
// Return appropriate exit code
if !result.AllPassed {
return fmt.Errorf("preflight checks failed")
}
return nil
}

View File

@@ -25,6 +25,13 @@ The retention policy ensures:
2. At least --min-backups most recent backups are always kept
3. Both conditions must be met for deletion
GFS (Grandfather-Father-Son) Mode:
When --gfs flag is enabled, a tiered retention policy is applied:
- Yearly: Keep one backup per year on the first eligible day
- Monthly: Keep one backup per month on the specified day
- Weekly: Keep one backup per week on the specified weekday
- Daily: Keep most recent daily backups
Examples:
# Clean up backups older than 30 days (keep at least 5)
dbbackup cleanup /backups --retention-days 30 --min-backups 5
@@ -35,6 +42,12 @@ Examples:
# Clean up specific database backups only
dbbackup cleanup /backups --pattern "mydb_*.dump"
# GFS retention: 7 daily, 4 weekly, 12 monthly, 3 yearly
dbbackup cleanup /backups --gfs --gfs-daily 7 --gfs-weekly 4 --gfs-monthly 12 --gfs-yearly 3
# GFS with custom weekly day (Saturday) and monthly day (15th)
dbbackup cleanup /backups --gfs --gfs-weekly-day Saturday --gfs-monthly-day 15
# Aggressive cleanup (keep only 3 most recent)
dbbackup cleanup /backups --retention-days 1 --min-backups 3`,
Args: cobra.ExactArgs(1),
@@ -46,6 +59,15 @@ var (
minBackups int
dryRun bool
cleanupPattern string
// GFS retention policy flags
gfsEnabled bool
gfsDaily int
gfsWeekly int
gfsMonthly int
gfsYearly int
gfsWeeklyDay string
gfsMonthlyDay int
)
func init() {
@@ -54,6 +76,15 @@ func init() {
cleanupCmd.Flags().IntVar(&minBackups, "min-backups", 5, "Always keep at least this many backups")
cleanupCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Show what would be deleted without actually deleting")
cleanupCmd.Flags().StringVar(&cleanupPattern, "pattern", "", "Only clean up backups matching this pattern (e.g., 'mydb_*.dump')")
// GFS retention policy flags
cleanupCmd.Flags().BoolVar(&gfsEnabled, "gfs", false, "Enable GFS (Grandfather-Father-Son) retention policy")
cleanupCmd.Flags().IntVar(&gfsDaily, "gfs-daily", 7, "Number of daily backups to keep (GFS mode)")
cleanupCmd.Flags().IntVar(&gfsWeekly, "gfs-weekly", 4, "Number of weekly backups to keep (GFS mode)")
cleanupCmd.Flags().IntVar(&gfsMonthly, "gfs-monthly", 12, "Number of monthly backups to keep (GFS mode)")
cleanupCmd.Flags().IntVar(&gfsYearly, "gfs-yearly", 3, "Number of yearly backups to keep (GFS mode)")
cleanupCmd.Flags().StringVar(&gfsWeeklyDay, "gfs-weekly-day", "Sunday", "Day of week for weekly backups (e.g., 'Sunday')")
cleanupCmd.Flags().IntVar(&gfsMonthlyDay, "gfs-monthly-day", 1, "Day of month for monthly backups (1-28)")
}
func runCleanup(cmd *cobra.Command, args []string) error {
@@ -72,6 +103,11 @@ func runCleanup(cmd *cobra.Command, args []string) error {
return fmt.Errorf("backup directory does not exist: %s", backupDir)
}
// Check if GFS mode is enabled
if gfsEnabled {
return runGFSCleanup(backupDir)
}
// Create retention policy
policy := retention.Policy{
RetentionDays: retentionDays,
@@ -333,3 +369,112 @@ func formatBackupAge(t time.Time) string {
return fmt.Sprintf("%d years", years)
}
}
// runGFSCleanup applies GFS (Grandfather-Father-Son) retention policy
func runGFSCleanup(backupDir string) error {
// Create GFS policy
policy := retention.GFSPolicy{
Enabled: true,
Daily: gfsDaily,
Weekly: gfsWeekly,
Monthly: gfsMonthly,
Yearly: gfsYearly,
WeeklyDay: retention.ParseWeekday(gfsWeeklyDay),
MonthlyDay: gfsMonthlyDay,
DryRun: dryRun,
}
fmt.Printf("📅 GFS Retention Policy:\n")
fmt.Printf(" Directory: %s\n", backupDir)
fmt.Printf(" Daily: %d backups\n", policy.Daily)
fmt.Printf(" Weekly: %d backups (on %s)\n", policy.Weekly, gfsWeeklyDay)
fmt.Printf(" Monthly: %d backups (day %d)\n", policy.Monthly, policy.MonthlyDay)
fmt.Printf(" Yearly: %d backups\n", policy.Yearly)
if cleanupPattern != "" {
fmt.Printf(" Pattern: %s\n", cleanupPattern)
}
if dryRun {
fmt.Printf(" Mode: DRY RUN (no files will be deleted)\n")
}
fmt.Println()
// Apply GFS policy
result, err := retention.ApplyGFSPolicy(backupDir, policy)
if err != nil {
return fmt.Errorf("GFS cleanup failed: %w", err)
}
// Display tier breakdown
fmt.Printf("📊 Backup Classification:\n")
fmt.Printf(" Yearly: %d\n", result.YearlyKept)
fmt.Printf(" Monthly: %d\n", result.MonthlyKept)
fmt.Printf(" Weekly: %d\n", result.WeeklyKept)
fmt.Printf(" Daily: %d\n", result.DailyKept)
fmt.Printf(" Total kept: %d\n", result.TotalKept)
fmt.Println()
// Display deletions
if len(result.Deleted) > 0 {
if dryRun {
fmt.Printf("🔍 Would delete %d backup(s):\n", len(result.Deleted))
} else {
fmt.Printf("✅ Deleted %d backup(s):\n", len(result.Deleted))
}
for _, file := range result.Deleted {
fmt.Printf(" - %s\n", filepath.Base(file))
}
}
// Display kept backups (limited display)
if len(result.Kept) > 0 && len(result.Kept) <= 15 {
fmt.Printf("\n📦 Kept %d backup(s):\n", len(result.Kept))
for _, file := range result.Kept {
// Show tier classification
info, _ := os.Stat(file)
if info != nil {
tiers := retention.ClassifyBackup(info.ModTime(), policy)
tierStr := formatTiers(tiers)
fmt.Printf(" - %s [%s]\n", filepath.Base(file), tierStr)
} else {
fmt.Printf(" - %s\n", filepath.Base(file))
}
}
} else if len(result.Kept) > 15 {
fmt.Printf("\n📦 Kept %d backup(s)\n", len(result.Kept))
}
if !dryRun && result.SpaceFreed > 0 {
fmt.Printf("\n💾 Space freed: %s\n", metadata.FormatSize(result.SpaceFreed))
}
if len(result.Errors) > 0 {
fmt.Printf("\n⚠ Errors:\n")
for _, err := range result.Errors {
fmt.Printf(" - %v\n", err)
}
}
fmt.Println(strings.Repeat("─", 50))
if dryRun {
fmt.Println("✅ GFS dry run completed (no files were deleted)")
} else if len(result.Deleted) > 0 {
fmt.Println("✅ GFS cleanup completed successfully")
} else {
fmt.Println(" No backups eligible for deletion under GFS policy")
}
return nil
}
// formatTiers formats a list of tiers as a comma-separated string
func formatTiers(tiers []retention.Tier) string {
if len(tiers) == 0 {
return "none"
}
parts := make([]string, len(tiers))
for i, t := range tiers {
parts[i] = t.String()
}
return strings.Join(parts, ",")
}