Skip to content

Commit 042e6f3

Browse files
authored
Merge pull request #1227 from jcechace/PBM-1649-cleanup-profiles
PBM-1649 Support profiles in cleanup and delete-backup
2 parents 945b6da + b6fdb4b commit 042e6f3

File tree

12 files changed

+1169
-84
lines changed

12 files changed

+1169
-84
lines changed

cmd/pbm-agent/delete.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ func (a *Agent) Delete(ctx context.Context, d *ctrl.DeleteBackupCmd, opid ctrl.O
9595
return
9696
}
9797

98-
l.Info("deleting backups older than %v", t)
99-
err = backup.DeleteBackupBefore(ctx, a.leadConn, t, bcpType, nodeInfo.Me)
98+
stg, err := util.GetProfiledStorage(ctx, a.leadConn, d.Profile, nodeInfo.Me, l)
99+
if err != nil {
100+
l.Error("get storage: %v", err)
101+
return
102+
}
103+
l.Info("deleting backups (profile: %q) older than %v", d.Profile, t)
104+
err = backup.DeleteBackupBefore(ctx, a.leadConn, stg, d.Profile, bcpType, t)
100105
if err != nil {
101106
l.Error("deleting: %v", err)
102107
return
@@ -254,20 +259,22 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
254259
return
255260
}
256261

257-
cfg, err := config.GetConfig(ctx, a.leadConn)
262+
cfg, err := config.GetProfiledConfig(ctx, a.leadConn, d.Profile)
258263
if err != nil {
259264
l.Error("get config: %v", err)
265+
return
260266
}
261267

262268
stg, err := util.StorageFromConfig(&cfg.Storage, a.brief.Me, l)
263269
if err != nil {
264270
l.Error("get storage: " + err.Error())
271+
return
265272
}
266273

267274
eg := errgroup.Group{}
268275
eg.SetLimit(runtime.NumCPU())
269276

270-
cr, err := backup.MakeCleanupInfo(ctx, a.leadConn, d.OlderThan)
277+
cr, err := backup.MakeCleanupInfo(ctx, a.leadConn, d.OlderThan, d.Profile)
271278
if err != nil {
272279
l.Error("make cleanup report: " + err.Error())
273280
return
@@ -289,15 +296,18 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
289296
bcp := &cr.Backups[i]
290297

291298
eg.Go(func() error {
292-
err := backup.DeleteBackupFiles(stg, bcp.Name)
293-
return errors.Wrapf(err, "delete backup files %q", bcp.Name)
299+
err := backup.DeleteBackupData(ctx, a.leadConn, stg, bcp.Name)
300+
return errors.Wrapf(err, "delete backup %q", bcp.Name)
294301
})
295302
}
296303
if err := eg.Wait(); err != nil {
297304
l.Error(err.Error())
298305
}
299306

300-
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me, false)
307+
if d.Profile == "" {
308+
err = resync.Resync(ctx, a.leadConn, &cfg.Storage, a.brief.Me, false)
309+
}
310+
301311
if err != nil {
302312
l.Error("storage resync: " + err.Error())
303313
}
@@ -306,7 +316,7 @@ func (a *Agent) Cleanup(ctx context.Context, d *ctrl.CleanupCmd, opid ctrl.OPID,
306316
func (a *Agent) deletePITRImpl(ctx context.Context, ts primitive.Timestamp) error {
307317
l := log.LogEventFromContext(ctx)
308318

309-
r, err := backup.MakeCleanupInfo(ctx, a.leadConn, ts)
319+
r, err := backup.MakeCleanupInfo(ctx, a.leadConn, ts, "")
310320
if err != nil {
311321
return errors.Wrap(err, "get pitr chunks")
312322
}

cmd/pbm/delete.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.mongodb.org/mongo-driver/bson/primitive"
1313

1414
"github.com/percona/percona-backup-mongodb/pbm/backup"
15+
"github.com/percona/percona-backup-mongodb/pbm/config"
1516
"github.com/percona/percona-backup-mongodb/pbm/connect"
1617
"github.com/percona/percona-backup-mongodb/pbm/ctrl"
1718
"github.com/percona/percona-backup-mongodb/pbm/defs"
@@ -25,6 +26,7 @@ type deleteBcpOpts struct {
2526
name string
2627
olderThan string
2728
bcpType string
29+
profile string
2830
dryRun bool
2931
yes bool
3032
}
@@ -36,14 +38,26 @@ func deleteBackup(
3638
d *deleteBcpOpts,
3739
) (fmt.Stringer, error) {
3840
if d.name == "" && d.olderThan == "" {
39-
return nil, errors.New("either --name or --older-than should be set")
41+
return nil, errors.New("either [name] or --older-than should be set")
4042
}
4143
if d.name != "" && d.olderThan != "" {
42-
return nil, errors.New("cannot use --name and --older-than at the same command")
44+
return nil, errors.New("cannot use [name] and --older-than at the same command")
45+
}
46+
if d.name != "" && d.profile != "" {
47+
return nil, errors.New("cannot use [name] with --profile")
4348
}
4449
if d.bcpType != "" && d.olderThan == "" {
4550
return nil, errors.New("cannot use --type without --older-than")
4651
}
52+
if d.profile != "" {
53+
_, err := config.GetProfile(ctx, conn, d.profile)
54+
if err != nil {
55+
if errors.Is(err, config.ErrMissedConfigProfile) {
56+
return nil, errors.Errorf("profile %q is not found", d.profile)
57+
}
58+
return nil, errors.Wrap(err, "get config")
59+
}
60+
}
4761
if !d.dryRun {
4862
err := checkForAnotherOperation(ctx, pbm)
4963
if err != nil {
@@ -135,7 +149,7 @@ func deleteManyBackup(ctx context.Context, pbm *sdk.Client, d *deleteBcpOpts) (s
135149
if err != nil {
136150
return sdk.NoOpID, errors.Wrap(err, "parse --type")
137151
}
138-
backups, err := sdk.ListDeleteBackupBefore(ctx, pbm, ts, bcpType)
152+
backups, err := sdk.ListDeleteBackupBefore(ctx, pbm, ts, bcpType, d.profile)
139153
if err != nil {
140154
return sdk.NoOpID, errors.Wrap(err, "fetch backup list")
141155
}
@@ -151,7 +165,7 @@ func deleteManyBackup(ctx context.Context, pbm *sdk.Client, d *deleteBcpOpts) (s
151165
}
152166
}
153167

154-
cid, err := pbm.DeleteBackupBefore(ctx, ts, sdk.DeleteBackupBeforeOptions{Type: bcpType})
168+
cid, err := pbm.DeleteBackupBefore(ctx, ts, sdk.DeleteBackupBeforeOptions{Type: bcpType, Profile: d.profile})
155169
return cid, errors.Wrap(err, "schedule delete")
156170
}
157171

@@ -255,6 +269,7 @@ type cleanupOptions struct {
255269
wait bool
256270
waitTime time.Duration
257271
dryRun bool
272+
profile string
258273
}
259274

260275
func doCleanup(ctx context.Context, conn connect.Client, pbm *sdk.Client, d *cleanupOptions) (fmt.Stringer, error) {
@@ -267,14 +282,23 @@ func doCleanup(ctx context.Context, conn connect.Client, pbm *sdk.Client, d *cle
267282
realTime := n.Format(time.RFC3339)
268283
return nil, errors.Errorf("--older-than %q is after now %q", providedTime, realTime)
269284
}
285+
if d.profile != "" {
286+
_, err := config.GetProfile(ctx, conn, d.profile)
287+
if err != nil {
288+
if errors.Is(err, config.ErrMissedConfigProfile) {
289+
return nil, errors.Errorf("profile %q is not found", d.profile)
290+
}
291+
return nil, errors.Wrap(err, "get config")
292+
}
293+
}
270294
if !d.dryRun {
271295
err := checkForAnotherOperation(ctx, pbm)
272296
if err != nil {
273297
return nil, err
274298
}
275299
}
276300

277-
info, err := pbm.CleanupReport(ctx, ts)
301+
info, err := pbm.CleanupReport(ctx, ts, d.profile)
278302
if err != nil {
279303
return nil, errors.Wrap(err, "make cleanup report")
280304
}
@@ -296,7 +320,7 @@ func doCleanup(ctx context.Context, conn connect.Client, pbm *sdk.Client, d *cle
296320
}
297321
}
298322

299-
cid, err := pbm.RunCleanup(ctx, ts)
323+
cid, err := pbm.RunCleanup(ctx, ts, d.profile)
300324
if err != nil {
301325
return nil, errors.Wrap(err, "send command")
302326
}

cmd/pbm/main.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,32 +335,36 @@ func (app *pbmApp) buildCancelBackupCmd() *cobra.Command {
335335
func (app *pbmApp) buildCleanupCmd() *cobra.Command {
336336
cleanupOpts := cleanupOptions{}
337337

338-
deletePitrCmd := &cobra.Command{
338+
cleanupCmd := &cobra.Command{
339339
Use: "cleanup",
340340
Short: "Delete Backups and PITR chunks",
341341
RunE: app.wrapRunE(func(cmd *cobra.Command, args []string) (fmt.Stringer, error) {
342342
return doCleanup(app.ctx, app.conn, app.pbm, &cleanupOpts)
343343
}),
344344
}
345345

346-
deletePitrCmd.Flags().StringVar(
346+
cleanupCmd.Flags().StringVar(
347347
&cleanupOpts.olderThan, "older-than", "",
348348
fmt.Sprintf("Delete backups older than date/time in format %s or %s", datetimeFormat, dateFormat),
349349
)
350-
deletePitrCmd.Flags().BoolVarP(
350+
cleanupCmd.Flags().BoolVarP(
351351
&cleanupOpts.yes, "yes", "y", false, "Don't ask for confirmation",
352352
)
353-
deletePitrCmd.Flags().BoolVarP(
353+
cleanupCmd.Flags().BoolVarP(
354354
&cleanupOpts.wait, "wait", "w", false, "Wait for deletion done",
355355
)
356-
deletePitrCmd.Flags().DurationVar(
356+
cleanupCmd.Flags().DurationVar(
357357
&cleanupOpts.waitTime, "wait-time", 0, "Maximum wait time",
358358
)
359-
deletePitrCmd.Flags().BoolVar(
359+
cleanupCmd.Flags().BoolVar(
360360
&cleanupOpts.dryRun, "dry-run", false, "Report but do not delete",
361361
)
362+
cleanupCmd.Flags().StringVar(
363+
&cleanupOpts.profile, "profile", "",
364+
"Name of the PBM profile to use for cleanup. Uses the default profile if omitted.",
365+
)
362366

363-
return deletePitrCmd
367+
return cleanupCmd
364368
}
365369

366370
func (app *pbmApp) buildConfigCmd() *cobra.Command {
@@ -560,6 +564,10 @@ func (app *pbmApp) buildDeleteBackupCmd() *cobra.Command {
560564
deleteBcpCmd.Flags().BoolVar(
561565
&deleteBcpOptions.dryRun, "dry-run", false, "Report but do not delete",
562566
)
567+
deleteBcpCmd.Flags().StringVar(
568+
&deleteBcpOptions.profile, "profile", "",
569+
"Name of the PBM profile to use for delete. Uses the default profile if omitted.",
570+
)
563571

564572
return deleteBcpCmd
565573
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
github.com/pkg/errors v0.9.1
3131
github.com/spf13/cobra v1.10.1
3232
github.com/spf13/viper v1.21.0
33+
github.com/stretchr/testify v1.11.1
3334
github.com/testcontainers/testcontainers-go v0.39.0
3435
github.com/testcontainers/testcontainers-go/modules/minio v0.34.0
3536
github.com/testcontainers/testcontainers-go/modules/mongodb v0.39.0
@@ -126,7 +127,6 @@ require (
126127
github.com/spf13/cast v1.10.0 // indirect
127128
github.com/spf13/pflag v1.0.10 // indirect
128129
github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect
129-
github.com/stretchr/testify v1.11.1 // indirect
130130
github.com/subosito/gotenv v1.6.0 // indirect
131131
github.com/tinylib/msgp v1.4.0 // indirect
132132
github.com/tklauser/go-sysconf v0.3.12 // indirect

0 commit comments

Comments
 (0)