feat: cleanup old backups
This commit is contained in:
parent
3fc3b8cb18
commit
6929a86485
35
cleanup.go
Normal file
35
cleanup.go
Normal file
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func cleanupOldBackups() error {
|
||||
files, err := ioutil.ReadDir(config.Folder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(files) < config.MaxBackups {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].ModTime().Unix() < files[j].ModTime().Unix()
|
||||
})
|
||||
|
||||
oldest := files[:len(files)-config.MaxBackups]
|
||||
|
||||
for _, file := range oldest {
|
||||
log.Printf("Removing old backup folder %s...\n", file.Name())
|
||||
err = os.RemoveAll(config.Folder + file.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -16,7 +16,7 @@ type conf struct {
|
||||
Folder string // Backup folder _with_ trailing slash
|
||||
fullCurrentBackupPath string
|
||||
Interval time.Duration
|
||||
MaxBackups int64
|
||||
MaxBackups int
|
||||
}
|
||||
|
||||
var (
|
||||
@ -34,7 +34,7 @@ func init() {
|
||||
config = &conf{
|
||||
Folder: "/backups/",
|
||||
Interval: time.Hour * 6,
|
||||
MaxBackups: 24,
|
||||
MaxBackups: 10,
|
||||
}
|
||||
|
||||
folder, has := os.LookupEnv(envBackupFolder)
|
||||
@ -58,10 +58,11 @@ func init() {
|
||||
|
||||
max, has := os.LookupEnv(envMax)
|
||||
if has {
|
||||
config.MaxBackups, err = strconv.ParseInt(max, 10, 64)
|
||||
maxBackups, err := strconv.ParseInt(max, 10, 32)
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid max: %s\n", err)
|
||||
}
|
||||
config.MaxBackups = int(maxBackups)
|
||||
}
|
||||
|
||||
updateFullBackupPath()
|
||||
|
7
main.go
7
main.go
@ -19,12 +19,15 @@ func main() {
|
||||
|
||||
storeContainers(c, containers)
|
||||
|
||||
err = cleanupOldBackups()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not clean old backups: %s", err)
|
||||
}
|
||||
|
||||
err = dumpAllDatabases(c)
|
||||
if err != nil {
|
||||
// TODO: Only log errors while dumping dbs
|
||||
log.Fatalf("Could not dump databases: %s", err)
|
||||
}
|
||||
|
||||
// TODO: Cron
|
||||
// TODO: Cleanup old
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user