-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
221 lines (190 loc) · 5.33 KB
/
schedule.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"fmt"
"math"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/hyperjumptech/jiffy"
)
// return true if we are currently in the scheduled backup window
func ScheduleIsActive() bool {
debugReturn := DebugCall()
// we already validated these when we validated the config
start, err := time.Parse("3:04 PM", Config.Schedule.StartTime)
if err != nil {
panic(err)
}
end, err := time.Parse("3:04 PM", Config.Schedule.EndTime)
if err != nil {
panic(err)
}
// subtract 1 minute from the start time just in case whatever launches
// this program is a little off
start = start.Add(-1 * time.Minute)
// current time without date or timezone
now := time.Now()
nowTime := time.Date(
0, 1, 1,
now.Hour(), now.Minute(), now.Second(), now.Nanosecond(),
time.UTC,
)
if start.Before(end) {
// if the schedule does not cross midnight
ret := nowTime.After(start) && nowTime.Before(end)
debugReturn(ret)
return ret
} else {
// if the schedule does cross midnight
ret := nowTime.After(start) || nowTime.Before(end)
debugReturn(ret)
return ret
}
}
// take a VM name and return a folder name in the format
// "2006-01-02_15-04-05 My-VM-Name"
func DateTimePrefix(t time.Time, name string) string {
debugReturn := DebugCall(t, name)
ret := t.Format("2006-01-02_15-04-05 ") + name
debugReturn(ret)
return ret
}
// given a folder name in the format "2006-01-02_15-04-05 My-VM-Name"
// return the time and the name
func parseDateTime(fullName string) (time.Time, string, error) {
prefix, name, _ := strings.Cut(fullName, " ")
t, err := time.ParseInLocation("2006-01-02_15-04-05", prefix, time.Local)
return t, name, err
}
// search local path for backups, listing each one for each VM
func Backups() (map[string][]time.Time, error) {
debugReturn := DebugCall()
entries, err := os.ReadDir(Config.SMB.LocalPath)
if err != nil {
debugReturn(nil, err)
return nil, err
}
backups := make(map[string][]time.Time)
for _, entry := range entries {
if !entry.IsDir() {
continue
}
t, name, err := parseDateTime(entry.Name())
if err != nil {
continue
}
backups[name] = append(backups[name], t)
}
// sort the backups for each VM (newest first)
for _, times := range backups {
sort.Slice(times, func(i, j int) bool {
return times[i].After(times[j])
})
}
debugReturn(backups, nil)
return backups, nil
}
// list VMs that need to be backed up, sorted by priority
func BackupQueue(interval time.Duration) ([]string, error) {
debugReturn := DebugCall(interval)
// get a list of all VMs
vms, err := VMs(Config.Schedule.Tag)
if err != nil {
debugReturn(nil, err)
return nil, err
}
// get a list of all backups
backups, err := Backups()
if err != nil {
debugReturn(nil, err)
return nil, err
}
// get backup ages for all VMs (MaxInt64 if never backed up)
backupAges := make(map[string]time.Duration)
for name := range vms {
// if the VM has never been backed up
if _, exists := backups[name]; !exists {
backupAges[name] = time.Duration(math.MaxInt64)
continue
}
lastBackup := backups[name][0]
backupAges[name] = time.Since(lastBackup)
}
// list VMs to backup
type vmToBackup struct {
name string
age time.Duration
}
var vmsToBackup []vmToBackup
for name, age := range backupAges {
if age > interval {
vmsToBackup = append(vmsToBackup, vmToBackup{name, age})
}
}
// sort VMs to backup by age (oldest first)
sort.Slice(vmsToBackup, func(i, j int) bool {
return vmsToBackup[j].age < vmsToBackup[i].age
})
// return a list of VM names
var vmNames []string
for _, vm := range vmsToBackup {
vmNames = append(vmNames, vm.name)
}
debugReturn(vmNames, nil)
return vmNames, nil
}
// delete old backups
func Cleanup() error {
debugReturn := DebugCall()
backups, err := Backups()
if err != nil {
debugReturn(err)
return err
}
maxAge := time.Duration(math.MaxInt64)
if Config.Schedule.MaxAge != "" {
// already validated from when we validated the config
maxAge, err = jiffy.DurationOf(Config.Schedule.MaxAge)
if err != nil {
panic(err)
}
}
maxBackups := math.MaxInt64
if Config.Schedule.MaxBackups != 0 {
maxBackups = Config.Schedule.MaxBackups
}
var backupsToDelete []string
for vmName, backupTimes := range backups {
for i, backupTime := range backupTimes {
// if backup is too old
if time.Since(backupTime) > maxAge {
folderName := DateTimePrefix(backupTime, vmName)
backupsToDelete = append(backupsToDelete, folderName)
}
// if there are too many backups
if i >= maxBackups {
folderName := DateTimePrefix(backupTime, vmName)
backupsToDelete = append(backupsToDelete, folderName)
}
}
}
// sanity check that we are deleting less than 50% of backups
deletionPercentage := 100 * float64(len(backupsToDelete)) / float64(len(backups))
if deletionPercentage > 50 {
err := fmt.Errorf("refusing to delete %.0f%% of backups", deletionPercentage)
debugReturn(err)
return err
}
// delete backups
for _, folderName := range backupsToDelete {
err := os.RemoveAll(filepath.Join(Config.SMB.LocalPath, folderName))
if err != nil {
debugReturn(err)
return err
}
}
debugReturn(nil)
return nil
}