Skip to content

Commit

Permalink
feat: add lifecycle days configuration for backups
Browse files Browse the repository at this point in the history
- Add a new flag for setting lifecycle days in the database backup configuration
- Import a new storage library in the main application file
- Implement logic to set lifecycle policies based on the configured days and path in the backup function
- Update the configuration structure to include a field for lifecycle days

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Aug 17, 2024
1 parent a2cdbf5 commit 9ac24f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/docker-backup-database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ func settingsFlags(cfg *config.Config) []cli.Flag {
},
Destination: &cfg.Storage.SkipVerify,
},
&cli.IntFlag{
Name: "storage.days",
Usage: "Set lifecycle Days",
EnvVars: []string{"PLUGIN_STORAGE_DAYS", "INPUT_STORAGE_DAYS", "STORAGE_DAYS"},
Destination: &cfg.Storage.Days,
},

// SCHEDULE
&cli.StringFlag{
Expand Down
11 changes: 11 additions & 0 deletions cmd/docker-backup-database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/appleboy/docker-backup-database/pkg/dbdump"

"github.com/appleboy/go-storage"
"github.com/appleboy/go-storage/core"
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -118,6 +119,16 @@ func backupDB(ctx context.Context, cfg *config.Config) error {
}
}

// Set lifecycle on bucket or an object prefix.
if cfg.Storage.Days > 0 && cfg.Storage.Path != "" {
if err := s3.SetLifeCycle(ctx, cfg.Storage.Bucket, &core.LifecycleConfig{
Days: cfg.Storage.Days,
Prefix: cfg.Storage.Path,
}); err != nil {
return errors.New("can't set bucket lifecycle: " + err.Error())
}
}

if err := backup.Exec(ctx); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
Driver string
DumpName string
SkipVerify bool
Days int
}

// Server provides the server configuration.
Expand Down

0 comments on commit 9ac24f2

Please sign in to comment.