-
Notifications
You must be signed in to change notification settings - Fork 4
/
backup.go
231 lines (201 loc) · 7.15 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
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package restic
import (
"sync"
"time"
api_v1alpha1 "stash.appscode.dev/apimachinery/apis/stash/v1alpha1"
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
"gomodules.xyz/pointer"
"k8s.io/apimachinery/pkg/util/errors"
)
// RunBackup takes backup, cleanup old snapshots, check repository integrity etc.
// It extract valuable information from respective restic command it runs and return them for further use.
func (w *ResticWrapper) RunBackup(backupOption BackupOptions, targetRef api_v1beta1.TargetRef) (*BackupOutput, error) {
// Start clock to measure total session duration
startTime := time.Now()
backupOutput := &BackupOutput{
BackupTargetStatus: api_v1beta1.BackupTargetStatus{
Ref: targetRef,
},
}
// Run backup
hostStats, err := w.runBackup(backupOption)
if err != nil {
return nil, err
}
backupOutput.BackupTargetStatus.Stats = []api_v1beta1.HostBackupStats{hostStats}
for idx, hostStats := range backupOutput.BackupTargetStatus.Stats {
if hostStats.Hostname == backupOption.Host {
backupOutput.BackupTargetStatus.Stats[idx].Duration = time.Since(startTime).String()
backupOutput.BackupTargetStatus.Stats[idx].Phase = api_v1beta1.HostBackupSucceeded
}
}
return backupOutput, nil
}
// RunParallelBackup runs multiple backup in parallel.
// Host must be different for each backup.
func (w *ResticWrapper) RunParallelBackup(backupOptions []BackupOptions, targetRef api_v1beta1.TargetRef, maxConcurrency int) (*BackupOutput, error) {
// WaitGroup to wait until all go routine finishes
wg := sync.WaitGroup{}
// concurrencyLimiter channel is used to limit maximum number simultaneous go routine
concurrencyLimiter := make(chan bool, maxConcurrency)
defer close(concurrencyLimiter)
var (
backupErrs []error
mu sync.Mutex // use lock to avoid racing condition
)
backupOutput := &BackupOutput{
BackupTargetStatus: api_v1beta1.BackupTargetStatus{
Ref: targetRef,
},
}
for i := range backupOptions {
// try to send message in concurrencyLimiter channel.
// if maximum allowed concurrent backup is already running, program control will stuck here.
concurrencyLimiter <- true
// starting new go routine. add it to WaitGroup
wg.Add(1)
go func(opt BackupOptions, startTime time.Time) {
// when this go routine completes it task, release a slot from the concurrencyLimiter channel
// so that another go routine can start. Also, tell the WaitGroup that it is done with its task.
defer func() {
<-concurrencyLimiter
wg.Done()
}()
// sh field in ResticWrapper is a pointer. we must not use same w in multiple go routine.
// otherwise they might enter in racing condition.
nw := w.Copy()
hostStats, err := nw.runBackup(opt)
hostStats.Duration = time.Since(startTime).String()
if err != nil {
hostStats.Phase = api_v1beta1.HostBackupFailed
hostStats.Error = err.Error()
mu.Lock()
backupErrs = append(backupErrs, err)
mu.Unlock()
} else {
hostStats.Phase = api_v1beta1.HostBackupSucceeded
}
// add hostStats to backupOutput. use lock to avoid racing condition.
mu.Lock()
backupOutput.upsertHostBackupStats(hostStats)
mu.Unlock()
}(backupOptions[i], time.Now())
}
// wait for all the go routines to complete
wg.Wait()
return backupOutput, errors.NewAggregate(backupErrs)
}
func (w *ResticWrapper) runBackup(backupOption BackupOptions) (api_v1beta1.HostBackupStats, error) {
hostStats := api_v1beta1.HostBackupStats{
Hostname: backupOption.Host,
}
// fmt.Println("shell: ",w)
// Backup from stdin
if len(backupOption.StdinPipeCommands) != 0 {
out, err := w.backupFromStdin(backupOption)
if err != nil {
return hostStats, err
}
// Extract information from the output of backup command
snapStats, err := extractBackupInfo(out, backupOption.StdinFileName)
if err != nil {
return hostStats, err
}
hostStats.Snapshots = []api_v1beta1.SnapshotStats{snapStats}
return hostStats, nil
}
// Backup all target paths
for _, path := range backupOption.BackupPaths {
params := backupParams{
path: path,
host: backupOption.Host,
excludes: backupOption.Exclude,
args: backupOption.Args,
}
out, err := w.backup(params)
if err != nil {
return hostStats, err
}
// Extract information from the output of backup command
stats, err := extractBackupInfo(out, path)
if err != nil {
return hostStats, err
}
hostStats = upsertSnapshotStats(hostStats, stats)
}
return hostStats, nil
}
func upsertSnapshotStats(hostStats api_v1beta1.HostBackupStats, snapStats api_v1beta1.SnapshotStats) api_v1beta1.HostBackupStats {
for i, s := range hostStats.Snapshots {
// if there is already an entry for this snapshot, then update it
if s.Name == snapStats.Name {
hostStats.Snapshots[i] = snapStats
return hostStats
}
}
// no entry for this snapshot. add a new entry
hostStats.Snapshots = append(hostStats.Snapshots, snapStats)
return hostStats
}
func (backupOutput *BackupOutput) upsertHostBackupStats(hostStats api_v1beta1.HostBackupStats) {
// check if a entry already exist for this host in backupOutput. If exist then update it.
for i, v := range backupOutput.BackupTargetStatus.Stats {
if v.Hostname == hostStats.Hostname {
backupOutput.BackupTargetStatus.Stats[i] = hostStats
return
}
}
// no entry for this host. add a new entry
backupOutput.BackupTargetStatus.Stats = append(backupOutput.BackupTargetStatus.Stats, hostStats)
}
func (w *ResticWrapper) RepositoryAlreadyExist() bool {
return w.repositoryExist()
}
func (w *ResticWrapper) InitializeRepository() error {
return w.initRepository()
}
func (w *ResticWrapper) ApplyRetentionPolicies(retentionPolicy api_v1alpha1.RetentionPolicy) (*RepositoryStats, error) {
// Cleanup old snapshots according to retention policy
out, err := w.cleanup(retentionPolicy, "")
if err != nil {
return nil, err
}
// Extract information from output of cleanup command
kept, removed, err := extractCleanupInfo(out)
if err != nil {
return nil, err
}
return &RepositoryStats{SnapshotCount: kept, SnapshotsRemovedOnLastCleanup: removed}, nil
}
func (w *ResticWrapper) VerifyRepositoryIntegrity() (*RepositoryStats, error) {
// Check repository integrity
out, err := w.check()
if err != nil {
return nil, err
}
// Extract information from output of "check" command
integrity := extractCheckInfo(out)
// Read repository statics after cleanup
out, err = w.stats("")
if err != nil {
return nil, err
}
// Extract information from output of "stats" command
repoSize, err := extractStatsInfo(out)
if err != nil {
return nil, err
}
return &RepositoryStats{Integrity: pointer.BoolP(integrity), Size: repoSize}, nil
}