-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
104 lines (92 loc) · 2.4 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
// Copyright © 2019 Valentin Slyusarev <va.slyusarev@gmail.com>
package cmd
import (
"compress/gzip"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/va-slyusarev/lgr"
"github.com/va-slyusarev/pinky/app/config"
"github.com/va-slyusarev/pinky/app/store"
)
type Backup struct {
Cfg *config.BackupConfig
DB store.Storer
}
// Serv. Run backup service.
func (b *Backup) Serv(ctx context.Context) {
if b.Cfg.Duration <= 0 {
lgr.Warn("backup: service is not used: duration is %s", b.Cfg.Duration)
return
}
if err := os.MkdirAll(b.Cfg.Path, os.ModePerm); err != nil {
lgr.Error("backup: service is not used: path: %v", err)
return
}
lgr.Info("backup: service is activated: path %q, duration %s", b.Cfg.Path, b.Cfg.Duration)
tick := time.NewTicker(b.Cfg.Duration)
lgr.Info("backup: first backup at %s", time.Now().Add(b.Cfg.Duration).Format("2006/01/02 15:04:05"))
for {
select {
case <-tick.C:
if err := b.create(); err != nil {
lgr.Error("backup: %s", err)
continue
}
b.remove()
lgr.Info("backup: next backup at %s", time.Now().Add(b.Cfg.Duration).Format("2006/01/02 15:04:05"))
case <-ctx.Done():
lgr.Info("backup: service terminated")
return
}
}
}
func (b *Backup) create() error {
fileName := fmt.Sprintf("%s/backup-%s.gz", b.Cfg.Path, time.Now().Format("20060102T150405"))
f, err := os.Create(fileName)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
gz := gzip.NewWriter(f)
defer func() {
_ = gz.Close()
}()
if err := b.DB.Backup(gz); err != nil {
return err
}
lgr.Info("backup: create file %q", fileName)
return nil
}
func (b *Backup) remove() {
filesDir, err := ioutil.ReadDir(b.Cfg.Path)
if err != nil {
lgr.Error("remove: can't read files in path: %q: %v", b.Cfg.Path, err)
return
}
var files []os.FileInfo
for _, file := range filesDir {
if strings.HasPrefix(file.Name(), "backup-") {
files = append(files, file)
}
}
// old -> new
sort.Slice(files, func(i int, j int) bool { return files[i].Name() < files[j].Name() })
if len(files) > b.Cfg.MaxCopies {
for i := 0; i < len(files)-b.Cfg.MaxCopies; i++ {
file := filepath.Join(b.Cfg.Path, files[i].Name())
if err := os.Remove(file); err != nil {
lgr.Error("backup: remove: can't delete file, skip: %q: %v", file, err)
continue
}
lgr.Info("backup: remove: copy is outdated and has been deleted: %q", file)
}
}
}