/
backup-cleanup.go
98 lines (83 loc) · 2.3 KB
/
backup-cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"time"
"runtime"
"context"
"github.com/bborbe/backup-cleanup/backup_cleaner"
"github.com/bborbe/cron"
flag "github.com/bborbe/flagenv"
"github.com/bborbe/lock"
"github.com/golang/glog"
)
const (
defaultKeepAmount = 5
lockName = "/var/run/backup-cleanup.lock"
parameterKeepAmount = "keep"
parameterDirectory = "dir"
parameterMatch = "match"
parameterWait = "wait"
parameterOneTime = "one-time"
parameterLock = "lock"
)
var (
targetDirPtr = flag.String(parameterDirectory, "", "target directory")
matchPtr = flag.String(parameterMatch, "", "match")
keepAmountPtr = flag.Int(parameterKeepAmount, defaultKeepAmount, "keep amount")
waitPtr = flag.Duration(parameterWait, time.Minute*60, "wait")
oneTimePtr = flag.Bool(parameterOneTime, false, "exit after first backup")
lockPtr = flag.String(parameterLock, lockName, "lock")
)
type CleanupBackup func(directory string, match string, keepAmount int) error
func main() {
defer glog.Flush()
glog.CopyStandardLogTo("info")
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
if err := do(); err != nil {
glog.Exit(err)
}
}
func do() error {
dir := *targetDirPtr
match := *matchPtr
keepAmount := *keepAmountPtr
wait := *waitPtr
oneTime := *oneTimePtr
lockName := *lockPtr
l := lock.NewLock(lockName)
if err := l.Lock(); err != nil {
return err
}
defer func() {
if err := l.Unlock(); err != nil {
glog.Warningf("unlock failed: %v", err)
}
}()
glog.V(0).Info("backup cleanup cron started")
defer glog.V(0).Info("backup cleanup cron finished")
if len(dir) == 0 {
return fmt.Errorf("parameter %s missing", parameterDirectory)
}
if len(match) == 0 {
return fmt.Errorf("parameter %s missing", parameterMatch)
}
if keepAmount <= 0 {
return fmt.Errorf("parameter %s missing", parameterKeepAmount)
}
backupCleaner := backup_cleaner.New()
glog.V(1).Infof("dir: %s, match: %s, keepAmount %d, wait: %v, oneTime: %v, lockName: %s", dir, match, keepAmount, wait, oneTime, lockName)
action := func(ctx context.Context) error {
return backupCleaner.CleanupBackup(dir, match, keepAmount)
}
var c cron.Cron
if *oneTimePtr {
c = cron.NewOneTimeCron(action)
} else {
c = cron.NewWaitCron(
*waitPtr,
action,
)
}
return c.Run(context.Background())
}