-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
268 lines (232 loc) · 6.24 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
package main
import (
"archive/zip"
"compress/gzip"
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"filippo.io/age"
_ "github.com/rclone/rclone/backend/all"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/config/configfile"
"github.com/rclone/rclone/fs/object"
"github.com/robfig/cron/v3"
"gopkg.in/yaml.v3"
)
// BackupService handles database and directory backups
type BackupService struct {
Config BackupConfig
Database DatabaseConnection
Cron *cron.Cron
CronEntry cron.EntryID
RClone fs.Fs
}
// Prepare for backup (creating directories, checking credentials, ...)
func (s *BackupService) Prepare() error {
err := os.MkdirAll(s.Config.Storage, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create backup dir %s: %w", s.Config.Storage, err)
}
if s.Config.RCloneConfig != "" {
err = config.SetConfigPath(s.Config.RCloneConfig)
if err != nil {
return fmt.Errorf("failed to load rclone config %s: %w", s.Config.RCloneConfig, err)
}
configfile.Install()
}
if s.Config.RClonePath != "" {
s.RClone, err = fs.NewFs(context.Background(), s.Config.RClonePath)
if err != nil {
return fmt.Errorf("failed create rclone FS %s: %w", s.Config.RClonePath, err)
}
}
return nil
}
// IsBackupEnabled returns true if any backup is enabled
func (s *BackupService) IsBackupEnabled() bool {
return s.Config.Database || s.Config.DataDirectories != ""
}
// StartSchedule of backup cron
func (s *BackupService) StartSchedule() error {
s.Cron = cron.New()
s.Cron.Start()
// only enable cron if any backup is enabled
if s.IsBackupEnabled() && s.Config.Schedule != "" {
var err error
s.CronEntry, err = s.Cron.AddFunc(s.Config.Schedule, func() {
err := s.Backup()
if err != nil {
log.Printf("backup failed: %v", err)
}
log.Printf("[Next Backup: %s]", s.Cron.Entry(s.CronEntry).Next)
})
if err != nil {
return fmt.Errorf("failed to create backup schedule: %w", err)
}
log.Printf("[Next Backup: %s]", s.Cron.Entry(s.CronEntry).Next)
}
return nil
}
// StopSchedule cron of backup
func (s *BackupService) StopSchedule(timeout time.Duration) {
if s.Cron != nil {
ctx := s.Cron.Stop()
select {
case <-ctx.Done():
case <-time.After(timeout):
}
}
}
// Backup database and data directories
func (s *BackupService) Backup() error {
if !s.IsBackupEnabled() {
log.Print("Nothing to backup")
return nil
}
recipients := s.Config.ageRecipients()
var filename string
if len(recipients) > 0 {
filename = fmt.Sprintf("backup_%s.zip.age", time.Now().Format(time.RFC3339))
} else {
filename = fmt.Sprintf("backup_%s.zip", time.Now().Format(time.RFC3339))
}
log.Printf("create backup %s ...", filename)
// open file
file, fileClose, err := s.createFile(filename)
if err != nil {
return err
}
defer fileClose()
encryptedFile, encryptClose, err := s.encryptFile(file)
if err != nil {
return err
}
defer encryptClose()
// create zip writer (without compression)
zipWriter := zip.NewWriter(encryptedFile)
defer zipWriter.Close()
meta := &BackupMeta{
Version: 1,
Date: time.Now(),
}
if err = s.backupDatabase(zipWriter, meta); err != nil {
return err
}
if err = s.backupDirectories(zipWriter, meta); err != nil {
return err
}
// write meta file
writer, err := zipWriter.CreateHeader(&zip.FileHeader{
Name: "backup.yml",
Modified: time.Now(),
})
if err != nil {
return fmt.Errorf("failed to create backup.yml: %w", err)
}
if err = yaml.NewEncoder(writer).Encode(&meta); err != nil {
return fmt.Errorf("failed to write backup.yml: %w", err)
}
log.Printf("backup finished")
return nil
}
// createFile on local file system or remote via rclone
func (s *BackupService) createFile(filename string) (io.Writer, func(), error) {
if s.RClone == nil {
file, err := os.Create(filepath.Join(s.Config.Storage, filename))
if err != nil {
return nil, nil, fmt.Errorf("failed to create backup file %s: %w", filename, err)
}
return file, func() {
file.Close()
}, nil
}
reader, writer := io.Pipe()
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, err := s.RClone.Put(context.Background(), reader,
object.NewStaticObjectInfo(
filename, time.Now(), -1, false, nil, nil))
if err != nil {
_ = reader.CloseWithError(err)
} else {
reader.Close()
}
wg.Done()
}()
return writer, func() {
writer.Close()
wg.Wait()
}, nil
}
// encryptFile if configured
func (s *BackupService) encryptFile(file io.Writer) (io.Writer, func(), error) {
recipients := s.Config.ageRecipients()
if len(recipients) > 0 {
encryptedFile, err := age.Encrypt(file, recipients...)
if err != nil {
return nil, nil, fmt.Errorf("failed age encryption: %w", err)
}
return encryptedFile, func() {
encryptedFile.Close()
}, nil
}
return file, func() {}, nil
}
func (s *BackupService) backupDatabase(zipWriter *zip.Writer, meta *BackupMeta) error {
if !s.Config.Database || s.Database == nil {
return nil
}
log.Printf("> dump database")
writer, err := zipWriter.CreateHeader(&zip.FileHeader{
Name: "database.sql.gz",
Modified: time.Now(),
})
if err != nil {
return fmt.Errorf("failed to create database.sql.gz: %w", err)
}
// backup database
gzipWriter := gzip.NewWriter(writer)
err = s.Database.Backup(gzipWriter)
gzipWriter.Close()
if err != nil {
return err
}
meta.DatabaseBackup = "database.sql.gz"
return nil
}
func (s *BackupService) backupDirectories(zipWriter *zip.Writer, meta *BackupMeta) error {
if s.Config.DataDirectories == "" {
return nil
}
log.Printf("> backup data directories")
dirsSplit := strings.Split(s.Config.DataDirectories, ",")
meta.Directories = make([]BackupMetaDirectory, len(dirsSplit))
for idx, dir := range dirsSplit {
log.Printf("-> %s", dir)
dirBackupFilename := fmt.Sprintf("data_%d.tar.gz", idx)
writer, err := zipWriter.CreateHeader(&zip.FileHeader{
Name: dirBackupFilename,
Modified: time.Now(),
})
if err != nil {
return fmt.Errorf("failed to create data_%d.tar.gz: %w", idx, err)
}
err = tarDir(writer, dir)
if err != nil {
return fmt.Errorf("failed to create data_%d.tar.gz: %w", idx, err)
}
meta.Directories[idx] = BackupMetaDirectory{
DirectoryPath: dir,
Filename: dirBackupFilename,
}
}
return nil
}