-
Notifications
You must be signed in to change notification settings - Fork 2
/
backup-executor.go
204 lines (175 loc) · 6.28 KB
/
backup-executor.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
package pkg
import (
"context"
"fmt"
"os"
"time"
"github.com/bborbe/errors"
libtime "github.com/bborbe/time"
"github.com/golang/glog"
v1 "github.com/bborbe/backup/k8s/apis/backup.benjamin-borbe.de/v1"
)
type BackupExectuor interface {
Backup(ctx context.Context, target v1.BackupSpec) error
}
func NewBackupExectuor(
currentTimeGetter libtime.CurrentTimeGetter,
rsyncExectuor RsyncExectuor,
backupRootDirectory Path,
sshPrivateKey SSHPrivateKey,
) BackupExectuor {
return &backupExectuor{
sshPrivateKey: sshPrivateKey,
currentTimeGetter: currentTimeGetter,
backupRootDirectory: backupRootDirectory,
rsyncExectuor: rsyncExectuor,
}
}
type backupExectuor struct {
currentTimeGetter libtime.CurrentTimeGetter
rsyncExectuor RsyncExectuor
backupRootDirectory Path
sshPrivateKey SSHPrivateKey
}
func (b *backupExectuor) Backup(ctx context.Context, backupSpec v1.BackupSpec) error {
if err := backupSpec.Validate(ctx); err != nil {
return errors.Wrapf(ctx, err, "valid backup faild")
}
backupPath := b.backupPath(backupSpec)
exists, err := backupPath.Exists(ctx)
if err != nil {
return errors.Wrapf(ctx, err, "exists failed")
}
if exists {
glog.V(2).Infof("backup %s already exists", backupPath)
return nil
}
if err := b.createIncompleteIfNotExists(ctx, backupSpec); err != nil {
return errors.Wrapf(ctx, err, "create incomplete if not exists failed")
}
if err := b.createCurrentIfNotExists(ctx, backupSpec); err != nil {
return errors.Wrapf(ctx, err, "create current if not exists failed")
}
if err := b.runRsync(ctx, backupSpec); err != nil {
return errors.Wrapf(ctx, err, "run rsync failed")
}
if err := b.renameIncomplete(ctx, backupSpec); err != nil {
return errors.Wrapf(ctx, err, "rename incomplete if not exists failed")
}
if err := b.updateCurrentSymlink(ctx, backupSpec); err != nil {
return errors.Wrapf(ctx, err, "update current symlink if not exists failed")
}
if err := b.removeEmpty(ctx, backupSpec); err != nil {
return errors.Wrapf(ctx, err, "remove empty failed")
}
return nil
}
func (b *backupExectuor) createIncompleteIfNotExists(ctx context.Context, backupSpec v1.BackupSpec) error {
incompletePath := b.incompletePath(backupSpec)
if err := os.MkdirAll(incompletePath.String(), os.ModePerm); err != nil {
return errors.Wrapf(ctx, err, "create incomplete directory failed")
}
glog.V(3).Infof("create incomplete directory completed")
return nil
}
func (b *backupExectuor) createCurrentIfNotExists(ctx context.Context, backupSpec v1.BackupSpec) error {
currentPath := b.currentPath(backupSpec)
emptyPath := b.emptyPath(backupSpec)
currentExists, err := currentPath.Exists(ctx)
if err != nil {
return errors.Wrapf(ctx, err, "check current exsits failed")
}
if currentExists {
glog.V(3).Infof("current directory already exists")
return nil
}
if err := os.MkdirAll(emptyPath.String(), os.ModePerm); err != nil {
return errors.Wrapf(ctx, err, "create incomplete directory failed")
}
glog.V(3).Infof("create empty directory completed")
if err := os.Symlink("empty", currentPath.String()); err != nil {
return errors.Wrapf(ctx, err, "create symlink from empty to current failed")
}
glog.V(3).Infof("create current directory completed")
return nil
}
func (b *backupExectuor) runRsync(ctx context.Context, backupSpec v1.BackupSpec) error {
glog.V(3).Infof("rsync started")
excludePath := b.excludePath(backupSpec)
if err := os.WriteFile(excludePath.String(), backupSpec.Excludes.Bytes(), 0644); err != nil {
return errors.Wrapf(ctx, err, "write exclude failed")
}
args := []string{
"-a",
"-m",
"--progress",
//"--compress",
"--whole-file",
"--numeric-ids",
"--delete",
"--delete-excluded",
"-e",
fmt.Sprintf("ssh -T -x -o StrictHostKeyChecking=no -p %d -i %s", backupSpec.Port, b.sshPrivateKey),
fmt.Sprintf("--exclude-from=%s", excludePath),
fmt.Sprintf("--port=%d", backupSpec.Port),
fmt.Sprintf("--link-dest=%s", b.currentPath(backupSpec)),
}
for _, dir := range backupSpec.Dirs {
args = append(args, fmt.Sprintf("%s@%s:%s", backupSpec.User, backupSpec.Host, dir))
}
args = append(args, b.incompletePath(backupSpec).String())
if err := b.rsyncExectuor.Rsync(ctx, args...); err != nil {
return errors.Wrapf(ctx, err, "rsync failed")
}
glog.V(3).Infof("rsync completed")
return nil
}
func (b *backupExectuor) renameIncomplete(ctx context.Context, backupSpec v1.BackupSpec) error {
incompletePath := b.incompletePath(backupSpec)
backupPath := b.backupPath(backupSpec)
if err := incompletePath.Rename(ctx, backupPath); err != nil {
return errors.Wrapf(ctx, err, "rename incomplete to backup failed")
}
return nil
}
func (b *backupExectuor) updateCurrentSymlink(ctx context.Context, backupSpec v1.BackupSpec) error {
currentPath := b.currentPath(backupSpec)
if err := currentPath.Remove(ctx); err != nil {
return errors.Wrapf(ctx, err, "remove current path failed")
}
if err := os.Symlink(b.currentTimeGetter.Now().Format(time.DateOnly), currentPath.String()); err != nil {
return errors.Wrapf(ctx, err, "create symlink from empty to current failed")
}
glog.V(3).Infof("create current directory completed")
return nil
}
func (b *backupExectuor) removeEmpty(ctx context.Context, backupSpec v1.BackupSpec) error {
emptyPath := b.emptyPath(backupSpec)
exists, err := emptyPath.Exists(ctx)
if err != nil {
return errors.Wrapf(ctx, err, "check empty exists failed")
}
if exists == false {
return nil
}
if err := emptyPath.Remove(ctx); err != nil {
return errors.Wrapf(ctx, err, "remove empty failed")
}
glog.V(3).Infof("remove empty completed")
return nil
}
func (b *backupExectuor) emptyPath(backupSpec v1.BackupSpec) Path {
return b.backupRootDirectory.Join(backupSpec.Host.String(), "empty")
}
func (b *backupExectuor) incompletePath(backupSpec v1.BackupSpec) Path {
return b.backupRootDirectory.Join(backupSpec.Host.String(), "incomplete")
}
func (b *backupExectuor) currentPath(backupSpec v1.BackupSpec) Path {
return b.backupRootDirectory.Join(backupSpec.Host.String(), "current")
}
func (b *backupExectuor) backupPath(backupSpec v1.BackupSpec) Path {
return b.backupRootDirectory.Join(backupSpec.Host.String(), b.currentTimeGetter.Now().Format(time.DateOnly))
}
func (b *backupExectuor) excludePath(spec v1.BackupSpec) Path {
return Path(fmt.Sprintf("/tmp/%s.excludes", spec.Host))
}