-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.go
More file actions
126 lines (94 loc) · 2.98 KB
/
backup.go
File metadata and controls
126 lines (94 loc) · 2.98 KB
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
package backr
import "time"
type BackupExecution interface {
Execute()
}
// Project represents a backup project executed by backr
type Project struct {
Name string
Backups []Backup
Dir string
Archiver Archiver
}
// Backup represents the state of a backup
type Backup struct {
BackupSpec
Checksum string
LastExecution time.Time
}
type UpdateReport struct {
Created int
Unchanged int
Deleted int
}
// NewProject returns a new Project.
// Use it to initialize a new backup project.
func NewProject(spec ProjectBackupSpec) Project {
project := Project{}
project.Name = spec.Name
project.Update(spec)
return project
}
// Update a Project from a spec and log the report
func (p *Project) Update(spec ProjectBackupSpec) UpdateReport {
// update Archiver
if spec.Archiver != nil {
p.Archiver = *spec.Archiver
} else {
p.Archiver = Archiver{Type: "pliz"}
}
report := UpdateReport{}
// this value will be decremented for each backup found
report.Deleted = len(p.Backups)
// map the checksums to each item
backupsByChecksum := map[string]Backup{}
for i := range p.Backups {
backupsByChecksum[p.Backups[i].Checksum] = p.Backups[i]
}
backups := []Backup{}
for _, backupSpec := range spec.Backups {
checksum := backupSpec.GetChecksum()
var backup Backup
// search if the item already exists
if existingBackup, ok := backupsByChecksum[checksum]; ok {
backup = existingBackup
report.Unchanged++
report.Deleted--
} else {
backup = Backup{
BackupSpec: backupSpec,
Checksum: checksum,
}
// setup the first
report.Created++
}
backups = append(backups, backup)
}
p.Backups = backups
return report
}
// GetNextBackupTime returns the time representing the moment where the backup should be executed,
// according to the last backup time
func (backup *Backup) GetNextBackupTime(timeSpec BackupTimeSpec, startupTime time.Time) time.Time {
// returns the date only if it's the first backup or the min age has been reached
// force the execution at a the specified start hour, to avoid performing backup at unwanted time
next := backup.LastExecution.Add(time.Duration(backup.MinAge) * time.Duration(backup.PeriodUnit) * time.Minute)
if !backup.IgnoreStartupTime && (backup.LastExecution.IsZero() || next.Before(startupTime)) {
date := time.Date(startupTime.Year(), startupTime.Month(), startupTime.Day(), timeSpec.Hour, timeSpec.Minute, 0, 0, time.Local)
// if the next date is before than the current time, then pick the next day at the same hour
if date.Before(startupTime) {
date = date.AddDate(0, 0, 1)
}
return date
}
return next
}
// GetHealth returns the health of a backup: true is everything is OK, false otherwise
func (backup *Backup) GetHealth(timeSpec BackupTimeSpec, startupTime time.Time) bool {
// add a tolerance of 1 hour (execution time...)
nowWithTolerance := time.Now().Add(-10 * time.Minute)
if backup.GetNextBackupTime(timeSpec, startupTime).Before(nowWithTolerance) {
return false
}
return true
}