feat: cleanup old backups
This commit is contained in:
parent
3fc3b8cb18
commit
6929a86485
|
@ -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
|
Folder string // Backup folder _with_ trailing slash
|
||||||
fullCurrentBackupPath string
|
fullCurrentBackupPath string
|
||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
MaxBackups int64
|
MaxBackups int
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -34,7 +34,7 @@ func init() {
|
||||||
config = &conf{
|
config = &conf{
|
||||||
Folder: "/backups/",
|
Folder: "/backups/",
|
||||||
Interval: time.Hour * 6,
|
Interval: time.Hour * 6,
|
||||||
MaxBackups: 24,
|
MaxBackups: 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
folder, has := os.LookupEnv(envBackupFolder)
|
folder, has := os.LookupEnv(envBackupFolder)
|
||||||
|
@ -58,10 +58,11 @@ func init() {
|
||||||
|
|
||||||
max, has := os.LookupEnv(envMax)
|
max, has := os.LookupEnv(envMax)
|
||||||
if has {
|
if has {
|
||||||
config.MaxBackups, err = strconv.ParseInt(max, 10, 64)
|
maxBackups, err := strconv.ParseInt(max, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Invalid max: %s\n", err)
|
log.Fatalf("Invalid max: %s\n", err)
|
||||||
}
|
}
|
||||||
|
config.MaxBackups = int(maxBackups)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateFullBackupPath()
|
updateFullBackupPath()
|
||||||
|
|
7
main.go
7
main.go
|
@ -19,12 +19,15 @@ func main() {
|
||||||
|
|
||||||
storeContainers(c, containers)
|
storeContainers(c, containers)
|
||||||
|
|
||||||
|
err = cleanupOldBackups()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not clean old backups: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
err = dumpAllDatabases(c)
|
err = dumpAllDatabases(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: Only log errors while dumping dbs
|
|
||||||
log.Fatalf("Could not dump databases: %s", err)
|
log.Fatalf("Could not dump databases: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Cron
|
// TODO: Cron
|
||||||
// TODO: Cleanup old
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue