-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
88 lines (73 loc) · 1.69 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
package main
import (
"bytes"
"fmt"
"io"
"os"
"time"
"github.com/ulikunitz/xz"
bolt "go.etcd.io/bbolt"
)
var BackupInterval = time.Hour * 24
type BackupSaver interface {
/*
Interface that recieves backups of the database
to save in a variety of ways
*/
Save(io.Reader)
/*
The struct is responsible for determing the name
of the backup (such as the date).
The body will be compressed using the Lempel–Ziv–
Markov chain algorithm (.xz), this should be
reflected in the filename that is saved.
*/
}
type FileSaver struct {
Prefix string
}
func (f FileSaver) Save(in io.Reader) {
filename := fmt.Sprintf("%s%s.xz", f.Prefix, time.Now().Format(time.RFC3339))
file, err := os.Create(filename)
if err != nil {
fmt.Printf("Error during creating backup file: %s\n", err.Error())
return
}
defer file.Close()
io.Copy(file, in)
}
var fileSaver FileSaver
func runBackup() {
/*
Write database to xz reader
Give this to each BackupSaver
*/
fileSaverBuffer := new(bytes.Buffer)
compressedOut := io.MultiWriter(fileSaverBuffer)
xzWriter, err := xz.NewWriter(compressedOut)
if err != nil {
fmt.Printf("Error while opening xzWriter: %s\n", err.Error())
return
}
if err := db.View(func(tx *bolt.Tx) error {
_, err := tx.WriteTo(xzWriter)
return err
}); err != nil {
fmt.Printf("Error while reading database for backup: %s\n", err.Error())
return
}
if err := xzWriter.Close(); err != nil {
fmt.Printf("Error while closing xzWriter: %s\n", err.Error())
return
}
if Configuration.Backup.File.Enabled {
fileSaver.Save(fileSaverBuffer)
}
}
/*
This makes sure that the above objects all
satisfy the BackupSaver interface.
*/
var _ = []BackupSaver{
FileSaver{},
}