Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: retention policy for backup storage backend s3 (#2497)

Yilong Lieeaeb401f7b0
diff --git a/plugin/storage/s3/s3.go b/plugin/storage/s3/s3.go
index 3f2475236e..f070c326ff 100644
--- a/plugin/storage/s3/s3.go
+++ b/plugin/storage/s3/s3.go
@@ -82,11 +82,16 @@ func (c *Client) UploadObject(ctx context.Context, path string, body io.Reader)
})
}
-// DeleteObject deletes the object with path.
-func (c *Client) DeleteObject(ctx context.Context, path string) (*s3.DeleteObjectOutput, error) {
- return c.c.DeleteObject(ctx, &s3.DeleteObjectInput{
+// DeleteObjects deletes the objects with path.
+func (c *Client) DeleteObjects(ctx context.Context, pathList ...string) (*s3.DeleteObjectsOutput, error) {
+ var oidList []types.ObjectIdentifier
+ for _, path := range pathList {
+ path := path // create a new 'path'.
+ oidList = append(oidList, types.ObjectIdentifier{Key: &path})
+ }
+ return c.c.DeleteObjects(ctx, &s3.DeleteObjectsInput{
Bucket: &c.bucket,
- Key: &path,
+ Delete: &types.Delete{Objects: oidList},
})
}
diff --git a/plugin/storage/s3/s3_test.go b/plugin/storage/s3/s3_test.go
index 0947dc8850..9e6fbbc10a 100644
--- a/plugin/storage/s3/s3_test.go
+++ b/plugin/storage/s3/s3_test.go
@@ -59,7 +59,7 @@ func TestS3Operations(t *testing.T) {
})
t.Run("DeleteObjects", func(t *testing.T) {
- resp, err := client.DeleteObject(ctx, "backup/test/blob")
+ resp, err := client.DeleteObjects(ctx, "backup/test/blob")
a.NoError(err)
log.Info("Deleted", zap.Any("meta", resp.ResultMetadata))
})
diff --git a/server/backup_runner.go b/server/backup_runner.go
index d33a45f38a..7546e809d1 100644
--- a/server/backup_runner.go
+++ b/server/backup_runner.go
@@ -121,7 +121,7 @@ func (r *BackupRunner) purgeExpiredBackupData(ctx context.Context) {
if maxRetentionPeriodTs == math.MaxInt {
continue
}
- if err := r.purgeBinlogFiles(instance.ID, maxRetentionPeriodTs); err != nil {
+ if err := r.purgeBinlogFiles(ctx, instance.ID, maxRetentionPeriodTs); err != nil {
log.Error("Failed to purge binlog files for instance", zap.String("instance", instance.Name), zap.Int("retentionPeriodTs", maxRetentionPeriodTs), zap.Error(err))
}
}
@@ -142,17 +142,47 @@ func (r *BackupRunner) getMaxRetentionPeriodTsForMySQLInstance(ctx context.Conte
return maxRetentionPeriodTs, nil
}
-// TODO(dragonly): Remove metadata as well.
-func (r *BackupRunner) purgeBinlogFiles(instanceID, retentionPeriodTs int) error {
+func (r *BackupRunner) purgeBinlogFiles(ctx context.Context, instanceID, retentionPeriodTs int) error {
binlogDir := getBinlogAbsDir(r.server.profile.DataDir, instanceID)
+ switch r.server.profile.BackupStorageBackend {
+ case api.BackupStorageBackendLocal:
+ return r.purgeBinlogFilesLocal(binlogDir, retentionPeriodTs)
+ case api.BackupStorageBackendS3:
+ return r.purgeBinlogFilesOnCloud(ctx, binlogDir, retentionPeriodTs)
+ default:
+ return errors.Errorf("purge binlog files not implemented for storage backend %s", r.server.profile.BackupStorageBackend)
+ }
+}
+
+func (r *BackupRunner) purgeBinlogFilesOnCloud(ctx context.Context, binlogDir string, retentionPeriodTs int) error {
+ binlogDirOnCloud := common.GetBinlogRelativeDir(binlogDir)
+ listOutput, err := r.server.s3Client.ListObjects(ctx, binlogDirOnCloud)
+ if err != nil {
+ return errors.Wrapf(err, "failed to list binlog dir %q in the cloud storage", binlogDirOnCloud)
+ }
+ var purgeBinlogPathList []string
+ for _, item := range listOutput.Contents {
+ expireTime := item.LastModified.Add(time.Duration(retentionPeriodTs) * time.Second)
+ if time.Now().After(expireTime) {
+ purgeBinlogPathList = append(purgeBinlogPathList, *item.Key)
+ }
+ }
+ if len(purgeBinlogPathList) > 0 {
+ log.Debug(fmt.Sprintf("Deleting %d expired binlog files from the cloud storage.", len(purgeBinlogPathList)))
+ if _, err := r.server.s3Client.DeleteObjects(ctx, purgeBinlogPathList...); err != nil {
+ return errors.Wrapf(err, "failed to delete %d expired binlog files from the cloud storage", len(purgeBinlogPathList))
+ }
+ }
+ return nil
+}
+
+// TODO(dragonly): Remove metadata as well.
+func (*BackupRunner) purgeBinlogFilesLocal(binlogDir string, retentionPeriodTs int) error {
binlogFileInfoList, err := os.ReadDir(binlogDir)
if err != nil {
return errors.Wrapf(err, "failed to read backup directory %q", binlogDir)
}
for _, binlogFileInfo := range binlogFileInfoList {
- if _, err := mysql.GetBinlogNameSeq(binlogFileInfo.Name()); err != nil {
- continue // next binlog file
- }
// We use modification time of local binlog files which is later than the modification time of that on the MySQL server,
// which in turn is later than the last event timestamp of the binlog file.
// This is not accurate and gives about 10 minutes (backup runner interval) more retention time to the binlog files, which is acceptable.
@@ -164,7 +194,7 @@ func (r *BackupRunner) purgeBinlogFiles(instanceID, retentionPeriodTs int) error
expireTime := fileInfo.ModTime().Add(time.Duration(retentionPeriodTs) * time.Second)
if time.Now().After(expireTime) {
binlogFilePath := path.Join(binlogDir, binlogFileInfo.Name())
- log.Debug("Deleting expired binlog file for MySQL instance.", zap.String("path", binlogFilePath))
+ log.Debug("Deleting expired local binlog file for MySQL instance.", zap.String("path", binlogFilePath))
if err := os.Remove(binlogFilePath); err != nil {
log.Warn("Failed to remove an expired binlog file.", zap.String("path", binlogFilePath), zap.Error(err))
continue
@@ -183,16 +213,23 @@ func (r *BackupRunner) purgeBackup(ctx context.Context, backup *api.Backup) erro
RowStatus: &archive,
}
if _, err := r.server.store.PatchBackup(ctx, &backupPatch); err != nil {
- log.Error("Failed to update status for deleted backup.", zap.String("name", backup.Name), zap.Int("databaseId", backup.DatabaseID))
return errors.Wrapf(err, "failed to update status for deleted backup %q for database with ID %d", backup.Name, backup.DatabaseID)
}
- backupFilePath := getBackupAbsFilePath(r.server.profile.DataDir, backup.DatabaseID, backup.Name)
- if err := os.Remove(backupFilePath); err != nil {
- log.Error("Failed to delete an expired backup file.", zap.String("path", backupFilePath), zap.Error(err))
- return errors.Wrapf(err, "failed to delete an expired backup file %q", backupFilePath)
+ switch backup.StorageBackend {
+ case api.BackupStorageBackendLocal:
+ backupFilePath := getBackupAbsFilePath(r.server.profile.DataDir, backup.DatabaseID, backup.Name)
+ if err := os.Remove(backupFilePath); err != nil {
+ return errors.Wrapf(err, "failed to delete an expired backup file %q", backupFilePath)
+ }
+ log.Info(fmt.Sprintf("Deleted expired local backup file %s", backupFilePath))
+ case api.BackupStorageBackendS3:
+ backupFilePath := getBackupRelativeFilePath(backup.DatabaseID, backup.Name)
+ if _, err := r.server.s3Client.DeleteObjects(ctx, backupFilePath); err != nil {
+ return errors.Wrapf(err, "failed to delete backup file %s in the cloud storage", backupFilePath)
+ }
+ log.Info(fmt.Sprintf("Deleted expired backup file %s in the cloud storage", backupFilePath))
}
- log.Info("Deleted expired backup file.", zap.String("path", backupFilePath))
return nil
}