forked from r3-team/r3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.go
266 lines (230 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
package backup
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"r3/config"
"r3/log"
"r3/tools"
"r3/tools/compress"
"r3/types"
"strconv"
"sync"
)
var (
access_mx sync.Mutex
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
if config.GetString("backupDir") == "" {
err := errors.New("backup directory not defined")
log.Error("backup", "could not start", err)
return err
}
// read table of contents backup file
tocFile, err := TocFileReadCreate()
if err != nil {
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))
var timestampLatest int64
for _, backup := range tocFile.Backups {
if backup.JobName == jobName && backup.Timestamp > timestampLatest {
timestampLatest = backup.Timestamp
}
}
if timestampLatest > (now - interval) {
log.Info("backup", fmt.Sprintf("does not need to execute '%s', latest backup is still valid", jobName))
return nil
}
if err := jobCleanup(&tocFile, jobName, keepVersions); err != nil {
log.Error("backup", fmt.Sprintf("could not delete old versions of job '%s'", jobName), err)
return err
}
if err := jobBackup(&tocFile, 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 jobCleanup(tocFile *types.BackupTocFile, 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-- {
var timestampToDelete int64
var timestampToDeleteIndex int
for i, backup := range tocFile.Backups {
if backup.JobName == jobName && (timestampToDelete == 0 || backup.Timestamp < timestampToDelete) {
timestampToDelete = backup.Timestamp
timestampToDeleteIndex = i
}
}
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[timestampToDeleteIndex] = tocFile.Backups[len(tocFile.Backups)-1]
tocFile.Backups = tocFile.Backups[:len(tocFile.Backups)-1]
if err := tocFileWrite(*tocFile); err != nil {
return err
}
log.Info("backup", fmt.Sprintf("has successfully deleted '%s'", pathToDelete))
}
return nil
}
func jobBackup(tocFile *types.BackupTocFile, jobName string) error {
log.Info("backup", fmt.Sprintf("started for job '%s'", jobName))
newTimestamp := tools.GetTimeUnix()
jobDir := getBackupJobDir(newTimestamp, jobName)
// database backup
dbPath := filepath.Join(jobDir, subPathDb)
if err := os.MkdirAll(dbPath, 0755); err != nil {
return err
}
if err := dumpDb(dbPath); err != nil {
return err
}
// certificates backup
target := filepath.Join(jobDir, subPathCerts)
if err := compress.Path(target, config.File.Paths.Certificates); err != nil {
return err
}
// config backup
target = filepath.Join(jobDir, subPathConfig)
if err := tools.FileCopy(config.GetConfigFilepath(), target, false); err != nil {
return err
}
// files backup
target = filepath.Join(jobDir, subPathFiles)
if err := compress.Path(target, config.File.Paths.Files); err != nil {
return err
}
// transfer backup
target = filepath.Join(jobDir, subPathTransfer)
if err := compress.Path(target, config.File.Paths.Transfer); err != nil {
return err
}
// update TOC file
_, _, appBuild, _ := config.GetAppVersions()
appBuildInt, err := strconv.Atoi(appBuild)
if err != nil {
return err
}
tocFile.Backups = append(tocFile.Backups, types.BackupDef{
AppBuild: appBuildInt,
JobName: jobName,
Timestamp: newTimestamp,
})
if err := tocFileWrite(*tocFile); err != nil {
return err
}
log.Info("backup", fmt.Sprintf("successfully completed job '%s'", jobName))
return nil
}
// helpers
func dumpDb(path string) error {
args := []string{
"-h", config.File.Db.Host,
"-p", fmt.Sprintf("%d", config.File.Db.Port),
"-d", config.File.Db.Name,
"-U", config.File.Db.User,
"-j", "4", // number of parallel jobs
"-Fd", // custom format, to file directory
"-f", path,
}
cmd := exec.Command(getPgDumpPath(), args...)
tools.CmdAddSysProgAttrs(cmd)
cmd.Env = append(cmd.Env, fmt.Sprintf("LC_MESSAGES=%s", "en_US"))
cmd.Env = append(cmd.Env, fmt.Sprintf("PGPASSWORD=%s", config.File.Db.Pass))
return cmd.Run()
}
func TocFileReadCreate() (types.BackupTocFile, error) {
var tocFile = types.BackupTocFile{}
var path = getTocFilePath()
exists, err := tools.Exists(path)
if err != nil {
log.Error("backup", "could not check existence of TOC file", err)
return tocFile, err
}
if !exists {
tocFile.Backups = make([]types.BackupDef, 0)
return tocFile, tocFileWrite(tocFile)
}
jsonFile, err := os.ReadFile(path)
if err != nil {
return tocFile, err
}
return tocFile, json.Unmarshal(tools.RemoveUtf8Bom(jsonFile), &tocFile)
}
func tocFileWrite(tocFile types.BackupTocFile) error {
jsonFile, err := json.MarshalIndent(tocFile, "", "\t")
if err != nil {
return err
}
return os.WriteFile(getTocFilePath(), jsonFile, 0644)
}
func getTocFilePath() string {
return filepath.Join(config.GetString("backupDir"), "backups_toc.json")
}
func getBackupJobDir(timestamp int64, jobName string) string {
return filepath.Join(config.GetString("backupDir"), fmt.Sprintf("%d_%s", timestamp, jobName))
}