forked from r3-team/r3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.go
283 lines (232 loc) · 7.3 KB
/
backup.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package backup
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"r3/compress"
"r3/config"
"r3/db/embedded"
"r3/log"
"r3/tools"
"strconv"
"sync"
)
type backupDef struct {
AppBuild int `json:"appBuild"`
JobName string `json:"jobName"`
Timestamp int64 `json:"timestamp"`
}
type tocFileType struct {
Backups []backupDef `json:"backups"`
}
var (
access_mx sync.Mutex
backupDir string // working directory for backups
tocFilePath string // path to table of content file for backups
tocFile tocFileType // in-memory table of content file
subPathConfig = "config.json" // path within backup dir for config file
subPathDb = "database" // path within backup dir for database dump
subPathCerts = "certificates.zip" // path within backup dir for certificate files
subPathFiles = "files.zip" // path within backup dir for attribute files
subPathTransfer = "transfer.zip" // path within backup dir for transfer files
)
func Run() error {
access_mx.Lock()
defer access_mx.Unlock()
// check if anything is to be done
if config.GetUint64("backupDaily") == 0 &&
config.GetUint64("backupWeekly") == 0 &&
config.GetUint64("backupMonthly") == 0 {
log.Info("backup", "no backup jobs active, do nothing")
return nil
}
// initialize state
backupDir = config.GetString("backupDir")
tocFilePath = filepath.Join(backupDir, "backups_toc.json")
if backupDir == "" {
err := errors.New("backup directory not defined")
log.Error("backup", "could start", err)
return err
}
// check for table of contents backup file
exists, err := tools.Exists(tocFilePath)
if err != nil {
log.Error("backup", "could not check existence of TOC file", err)
return err
}
if !exists {
// create new TOC file
if err := tocFileWrite(); err != nil {
log.Error("backup", "could not write TOC file", err)
return err
}
} else {
// read existing
if err := tocFileRead(); err != nil {
log.Error("backup", "could not read TOC file", err)
return err
}
}
// clean up old backups then create new backups
now := tools.GetTimeUnix()
jobRan := false // limit to one job per run
var runOne = func(jobName string, keepVersions uint64, interval int64) error {
log.Info("backup", fmt.Sprintf("is considering job '%s' for execution", jobName))
if getLatestTimestamp(jobName) > (now - interval) {
log.Info("backup", fmt.Sprintf("does not need to execute '%s', latest backup is still valid", jobName))
return nil
}
if err := cleanup(jobName, keepVersions); err != nil {
log.Error("backup", fmt.Sprintf("could not delete old versions of job '%s'", jobName), err)
return err
}
if err := exec(jobName); err != nil {
log.Error("backup", fmt.Sprintf("could not execute job '%s'", jobName), err)
return err
}
jobRan = true
return nil
}
if !jobRan && config.GetUint64("backupMonthly") == 1 {
if err := runOne("monthly", config.GetUint64("backupCountMonthly"), 2592000); err != nil {
return err
}
}
if !jobRan && config.GetUint64("backupWeekly") == 1 {
if err := runOne("weekly", config.GetUint64("backupCountWeekly"), 604800); err != nil {
return err
}
}
if !jobRan && config.GetUint64("backupDaily") == 1 {
if err := runOne("daily", config.GetUint64("backupCountDaily"), 86400); err != nil {
return err
}
}
return nil
}
func cleanup(jobName string, countKeep uint64) error {
log.Info("backup", fmt.Sprintf("starting cleanup for job '%s', keep %d versions",
jobName, countKeep))
defer log.Info("backup", fmt.Sprintf("finished cleanup for job '%s'", jobName))
// get current count
var countCurrent int
for _, backup := range tocFile.Backups {
if backup.JobName == jobName {
countCurrent++
}
}
log.Info("backup", fmt.Sprintf("found %d versions for job '%s'", countCurrent, jobName))
// delete not-kept versions
// if 3 are to be kept, delete all but 2 (to make room for next backup)
for countDelete := countCurrent - int(countKeep) + 1; countDelete > 0; countDelete-- {
timestampToDelete, timestampIndex := getOldestTimestamp(jobName)
pathToDelete := getBackupJobDir(timestampToDelete, jobName)
log.Info("backup", fmt.Sprintf("is attempting to delete '%s'", pathToDelete))
exists, err := tools.Exists(pathToDelete)
if err != nil {
return err
}
if exists {
// physically delete backup
if err := os.RemoveAll(pathToDelete); err != nil {
return err
}
}
// update TOC file in any case
tocFile.Backups[timestampIndex] = tocFile.Backups[len(tocFile.Backups)-1]
tocFile.Backups = tocFile.Backups[:len(tocFile.Backups)-1]
if err := tocFileWrite(); err != nil {
return err
}
log.Info("backup", fmt.Sprintf("has successfully deleted '%s'", pathToDelete))
}
return nil
}
func exec(jobName string) error {
log.Info("backup", fmt.Sprintf("started for job '%s'", jobName))
newTimestamp := tools.GetTimeUnix()
_, _, appBuild, _ := config.GetAppVersions()
appBuildInt, err := strconv.Atoi(appBuild)
if err != nil {
return err
}
// create database backup
dbPath := filepath.Join(getBackupJobDir(newTimestamp, jobName), subPathDb)
if err := os.MkdirAll(dbPath, 0600); err != nil {
return err
}
if err := embedded.Backup(dbPath); err != nil {
return err
}
// create certificates backup
target := filepath.Join(getBackupJobDir(newTimestamp, jobName), subPathCerts)
if err := compress.Path(target, config.File.Paths.Certificates); err != nil {
return err
}
// create config backup
target = filepath.Join(getBackupJobDir(newTimestamp, jobName), subPathConfig)
if err := tools.FileCopy(config.GetConfigFilepath(), target, false); err != nil {
return err
}
// create files backup
target = filepath.Join(getBackupJobDir(newTimestamp, jobName), subPathFiles)
if err := compress.Path(target, config.File.Paths.Files); err != nil {
return err
}
// create transfer backup
target = filepath.Join(getBackupJobDir(newTimestamp, jobName), subPathTransfer)
if err := compress.Path(target, config.File.Paths.Transfer); err != nil {
return err
}
// update TOC file
tocFile.Backups = append(tocFile.Backups, backupDef{
AppBuild: appBuildInt,
JobName: jobName,
Timestamp: newTimestamp,
})
if err := tocFileWrite(); err != nil {
return err
}
log.Info("backup", fmt.Sprintf("successfully completed job '%s'", jobName))
return nil
}
func tocFileRead() error {
jsonFile, err := os.ReadFile(tocFilePath)
if err != nil {
return err
}
jsonFile = tools.RemoveUtf8Bom(jsonFile)
return json.Unmarshal(jsonFile, &tocFile)
}
func tocFileWrite() error {
jsonFile, err := json.MarshalIndent(tocFile, "", "\t")
if err != nil {
return err
}
return os.WriteFile(tocFilePath, jsonFile, 0644)
}
func getLatestTimestamp(jobName string) int64 {
var timestamp int64
for _, backup := range tocFile.Backups {
if backup.JobName == jobName && backup.Timestamp > timestamp {
timestamp = backup.Timestamp
}
}
return timestamp
}
func getOldestTimestamp(jobName string) (int64, int) {
var timestamp int64
var timestampIndex int
for i, backup := range tocFile.Backups {
if backup.JobName == jobName && (timestamp == 0 || backup.Timestamp < timestamp) {
timestamp = backup.Timestamp
timestampIndex = i
}
}
return timestamp, timestampIndex
}
func getBackupJobDir(timestamp int64, jobName string) string {
return filepath.Join(backupDir, fmt.Sprintf("%d_%s", timestamp, jobName))
}