forked from percona/percona-backup-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
318 lines (269 loc) · 8.28 KB
/
delete.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package pbm
import (
"fmt"
"time"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/sync/errgroup"
"github.com/percona/percona-backup-mongodb/pbm/log"
"github.com/percona/percona-backup-mongodb/pbm/storage"
"github.com/percona/percona-backup-mongodb/version"
)
// DeleteBackup deletes backup with the given name from the current storage
// and pbm database
func (p *PBM) DeleteBackup(name string, l *log.Event) error {
meta, err := p.GetBackupMeta(name)
if err != nil {
return errors.Wrap(err, "get backup meta")
}
tlns, err := p.PITRTimelines()
if err != nil {
return errors.Wrap(err, "get PITR chunks")
}
err = p.probeDelete(meta, tlns)
if err != nil {
return err
}
stg, err := p.GetStorage(l)
if err != nil {
return errors.Wrap(err, "get storage")
}
err = p.DeleteBackupFiles(meta, stg)
if err != nil {
return errors.Wrap(err, "delete files from storage")
}
_, err = p.Conn.Database(DB).Collection(BcpCollection).DeleteOne(p.ctx, bson.M{"name": meta.Name})
if err != nil {
return errors.Wrap(err, "delete metadata from db")
}
return nil
}
func (p *PBM) probeDelete(backup *BackupMeta, tlns []Timeline) error {
// check if backup isn't running
switch backup.Status {
case StatusDone, StatusCancelled, StatusError:
default:
return errors.Errorf("unable to delete backup in %s state", backup.Status)
}
// if backup isn't a base for any PITR timeline
for _, t := range tlns {
if backup.LastWriteTS.T == t.Start {
return errors.Errorf("unable to delete: backup is a base for '%s'", t)
}
}
ispitr, err := p.IsPITR()
if err != nil {
return errors.Wrap(err, "unable check pitr state")
}
// if PITR is ON and there are no chunks yet we shouldn't delete the most recent back
if !ispitr || tlns != nil {
return nil
}
nxt, err := p.BackupGetNext(backup)
if err != nil {
return errors.Wrap(err, "check next backup")
}
if nxt == nil {
return errors.New("unable to delete the last backup while PITR is on")
}
return nil
}
// DeleteBackupFiles removes backup's artifacts from storage
func (p *PBM) DeleteBackupFiles(meta *BackupMeta, stg storage.Storage) (err error) {
switch meta.Type {
case PhysicalBackup, IncrementalBackup:
return p.deletePhysicalBackupFiles(meta, stg)
case LogicalBackup:
fallthrough
default:
if version.IsLegacyArchive(meta.PBMVersion) {
err = p.deleteLegacyLogicalBackupFiles(meta, stg)
} else {
err = p.deleteLogicalBackupFiles(meta, stg)
}
return err
}
}
// DeleteBackupFiles removes backup's artifacts from storage
func (p *PBM) deletePhysicalBackupFiles(meta *BackupMeta, stg storage.Storage) (err error) {
for _, r := range meta.Replsets {
for _, f := range r.Files {
fname := meta.Name + "/" + r.Name + "/" + f.Name + meta.Compression.Suffix()
if f.Len != 0 {
fname += fmt.Sprintf(".%d-%d", f.Off, f.Len)
}
err = stg.Delete(fname)
if err != nil && err != storage.ErrNotExist {
return errors.Wrapf(err, "delete %s", fname)
}
}
for _, f := range r.Journal {
fname := meta.Name + "/" + r.Name + "/" + f.Name + meta.Compression.Suffix()
if f.Len != 0 {
fname += fmt.Sprintf(".%d-%d", f.Off, f.Len)
}
err = stg.Delete(fname)
if err != nil && err != storage.ErrNotExist {
return errors.Wrapf(err, "delete %s", fname)
}
}
}
err = stg.Delete(meta.Name + MetadataFileSuffix)
if err == storage.ErrNotExist {
return nil
}
return errors.Wrap(err, "delete metadata file from storage")
}
// deleteLogicalBackupFiles removes backup's artifacts from storage
func (p *PBM) deleteLogicalBackupFiles(meta *BackupMeta, stg storage.Storage) error {
if stg.Type() == storage.Filesystem {
return p.deleteLogicalBackupFilesFromFS(stg, meta.Name)
}
prefix := meta.Name + "/"
files, err := stg.List(prefix, "")
if err != nil {
return errors.WithMessagef(err, "get file list: %q", prefix)
}
eg := errgroup.Group{}
for _, f := range files {
ns := prefix + f.Name
eg.Go(func() error {
return errors.WithMessagef(stg.Delete(ns), "delete %q", ns)
})
}
if err := eg.Wait(); err != nil {
return err
}
bcpMF := meta.Name + MetadataFileSuffix
return errors.WithMessagef(stg.Delete(bcpMF), "delete %q", bcpMF)
}
// deleteLogicalBackupFiles removes backup's artifacts from storage
func (p *PBM) deleteLogicalBackupFilesFromFS(stg storage.Storage, bcpName string) error {
if err := stg.Delete(bcpName); err != nil {
return errors.WithMessagef(err, "delete %q", bcpName)
}
bcpMetafile := bcpName + MetadataFileSuffix
return errors.WithMessagef(stg.Delete(bcpMetafile), "delete %q", bcpMetafile)
}
// deleteLegacyLogicalBackupFiles removes backup's artifacts from storage
func (p *PBM) deleteLegacyLogicalBackupFiles(meta *BackupMeta, stg storage.Storage) (err error) {
for _, r := range meta.Replsets {
err = stg.Delete(r.OplogName)
if err != nil && err != storage.ErrNotExist {
return errors.Wrapf(err, "delete oplog %s", r.OplogName)
}
err = stg.Delete(r.DumpName)
if err != nil && err != storage.ErrNotExist {
return errors.Wrapf(err, "delete dump %s", r.DumpName)
}
}
err = stg.Delete(meta.Name + MetadataFileSuffix)
if err == storage.ErrNotExist {
return nil
}
return errors.Wrap(err, "delete metadata file from storage")
}
// DeleteOlderThan deletes backups which older than given Time
func (p *PBM) DeleteOlderThan(t time.Time, l *log.Event) error {
stg, err := p.GetStorage(l)
if err != nil {
return errors.Wrap(err, "get storage")
}
tlns, err := p.PITRTimelines()
if err != nil {
return errors.Wrap(err, "get PITR chunks")
}
cur, err := p.Conn.Database(DB).Collection(BcpCollection).Find(
p.ctx,
bson.M{
"start_ts": bson.M{"$lt": t.Unix()},
},
)
if err != nil {
return errors.Wrap(err, "get backups list")
}
defer cur.Close(p.ctx)
for cur.Next(p.ctx) {
m := new(BackupMeta)
err := cur.Decode(m)
if err != nil {
return errors.Wrap(err, "decode backup meta")
}
err = p.probeDelete(m, tlns)
if err != nil {
l.Info("deleting %s: %v", m.Name, err)
continue
}
err = p.DeleteBackupFiles(m, stg)
if err != nil {
return errors.Wrap(err, "delete backup files from storage")
}
_, err = p.Conn.Database(DB).Collection(BcpCollection).DeleteOne(p.ctx, bson.M{"name": m.Name})
if err != nil {
return errors.Wrap(err, "delete backup meta from db")
}
}
if cur.Err() != nil {
return errors.Wrap(cur.Err(), "cursor")
}
return nil
}
// DeletePITR deletes backups which older than given `until` Time. It will round `until` down
// to the last write of the closest preceding backup in order not to make gaps. So usually it
// gonna leave some extra chunks. E.g. if `until = 13` and the last write of the closest preceding
// backup is `10` it will leave `11` and `12` chunks as well since `13` won't be restorable
// without `11` and `12` (contiguous timeline from the backup).
// It deletes all chunks if `until` is nil.
func (p *PBM) DeletePITR(until *time.Time, l *log.Event) error {
stg, err := p.GetStorage(l)
if err != nil {
return errors.Wrap(err, "get storage")
}
var zerots primitive.Timestamp
if until == nil {
return p.deleteChunks(zerots, zerots, stg, l)
}
t := primitive.Timestamp{T: uint32(until.Unix()), I: 0}
bcp, err := p.GetLastBackup(&t)
if errors.Is(err, ErrNotFound) {
return p.deleteChunks(zerots, t, stg, l)
}
if err != nil {
return errors.Wrap(err, "get recent backup")
}
return p.deleteChunks(zerots, bcp.LastWriteTS, stg, l)
}
func (p *PBM) deleteChunks(start, until primitive.Timestamp, stg storage.Storage, l *log.Event) (err error) {
var chunks []OplogChunk
if until.T > 0 {
chunks, err = p.PITRGetChunksSliceUntil("", until)
} else {
chunks, err = p.PITRGetChunksSlice("", start, until)
}
if err != nil {
return errors.Wrap(err, "get pitr chunks")
}
if len(chunks) == 0 {
l.Debug("nothing to delete")
}
for _, chnk := range chunks {
err = stg.Delete(chnk.FName)
if err != nil && err != storage.ErrNotExist {
return errors.Wrapf(err, "delete pitr chunk '%s' (%v) from storage", chnk.FName, chnk)
}
_, err = p.Conn.Database(DB).Collection(PITRChunksCollection).DeleteOne(
p.ctx,
bson.D{
{"rs", chnk.RS},
{"start_ts", chnk.StartTS},
{"end_ts", chnk.EndTS},
},
)
if err != nil {
return errors.Wrap(err, "delete pitr chunk metadata")
}
l.Debug("deleted %s", chnk.FName)
}
return nil
}