Skip to content

Commit

Permalink
Add auto clean up for old backups.
Browse files Browse the repository at this point in the history
Introduced `shouldCleanUp` and `maxBackups` configuration options with corresponding default values.
  • Loading branch information
anytimesoon committed Jan 4, 2025
1 parent ad8e769 commit 3364371
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
45 changes: 45 additions & 0 deletions cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"log"
"os"
"path/filepath"
"sort"
)

func cleanUp() {
files, err := os.ReadDir(pathToBackups)
if err != nil {
log.Println("Failed to read backup directory for clean up")
return
}

if len(files) > maxBackups {
sort.Slice(files, func(i, j int) bool {
info1, err1 := files[i].Info()
info2, err2 := files[j].Info()
if err1 != nil || err2 != nil {
log.Printf("Error retrieving file info: %v, %v", err1, err2)
return false
}

return info1.ModTime().Before(info2.ModTime())
})

// The oldest file will now be the first element in the sorted list
oldestFile := files[0]
oldestFilePath := filepath.Join(pathToBackups, oldestFile.Name())

// Delete the oldest file
log.Printf("Deleting oldest file: %s\n", oldestFilePath)
err = os.Remove(oldestFilePath)
if err != nil {
log.Fatalf("Failed to delete file %s: %v", oldestFilePath, err)
}

log.Printf("Oldest file %s has been deleted successfully.\n", oldestFile.Name())
return
}

log.Println("Not enough backups to require clean up")
}
7 changes: 6 additions & 1 deletion example/main_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ backupOptions:
shouldRestore: True

# Only run the backup functions (optional, default True)
shouldBackup: True
shouldBackup: True

# Clean up will delete old backup files (optional, default True)
shouldCleanUp: True
# Maximum number of backups (optional, default 10)
maxBackups: 10
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.22.0
require (
github.com/habx/pg-commands v0.6.1
github.com/lib/pq v1.10.9
github.com/spf13/viper v1.18.2
github.com/theckman/yacspin v0.13.12
)

require (
Expand All @@ -24,9 +26,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/theckman/yacspin v0.13.12 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
Expand Down
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var (
backupOptions []string
restoreOptions []string

shouldCleanUp bool
maxBackups int

spinner *yacspin.Spinner

restoreFile string
Expand Down Expand Up @@ -83,8 +86,11 @@ func init() {
restoreOptions = viper.GetStringSlice("restoreOptions")

viper.SetDefault("shouldRestore", false)
shouldRestore = viper.GetBool("shouldRestore")
viper.SetDefault("shouldBackup", true)
viper.SetDefault("shouldCleanUp", true)
viper.SetDefault("maxBackups", 10)

shouldRestore = viper.GetBool("shouldRestore")
shouldBackup = viper.GetBool("shouldBackup")

args := flag.Args()
Expand All @@ -97,6 +103,9 @@ func init() {
}
}

shouldCleanUp = viper.GetBool("shouldCleanUp")
maxBackups = viper.GetInt("maxBackups")

spinner, err = yacspin.New(cfg)
if err != nil {
log.Println("Failed to start the spinner")
Expand Down Expand Up @@ -124,6 +133,10 @@ func main() {
}
restore(restoreFile)
}

if shouldCleanUp {
cleanUp()
}
}

func findExecutables() {
Expand Down

0 comments on commit 3364371

Please sign in to comment.