From e75bd6134395052178c25f5cfc8af764c5fac964 Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Tue, 18 Jun 2024 15:34:57 -0400 Subject: [PATCH] Move where we delete the oldest backup for clarity --- ee/agent/storage/bbolt/backup.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ee/agent/storage/bbolt/backup.go b/ee/agent/storage/bbolt/backup.go index 55593de22..f26315c6a 100644 --- a/ee/agent/storage/bbolt/backup.go +++ b/ee/agent/storage/bbolt/backup.go @@ -112,7 +112,16 @@ func (d *databaseBackupSaver) backupDb() error { func (d *databaseBackupSaver) rotate() error { baseBackupPath := BackupLauncherDbLocation(d.knapsack.RootDirectory()) - for i := numberOfOldBackupsToRetain; i > 0; i -= 1 { + // Delete the oldest backup, if it exists + oldestBackupPath := fmt.Sprintf("%s.%d", baseBackupPath, numberOfOldBackupsToRetain) + if _, err := os.Stat(oldestBackupPath); err == nil { + if err := os.Remove(oldestBackupPath); err != nil { + return fmt.Errorf("removing oldest backup %s during rotation: %w", oldestBackupPath, err) + } + } + + // Rename all other backups + for i := numberOfOldBackupsToRetain - 1; i > 0; i -= 1 { currentBackupPath := fmt.Sprintf("%s.%d", baseBackupPath, i) // This backup doesn't exist yet -- skip it @@ -120,14 +129,6 @@ func (d *databaseBackupSaver) rotate() error { continue } - // If is the oldest backup, delete it so we can rotate a new one into its place - if i == numberOfOldBackupsToRetain { - if err := os.Remove(currentBackupPath); err != nil { - return fmt.Errorf("removing oldest backup %s during rotation: %w", currentBackupPath, err) - } - continue - } - // Rename from launcher.db.bak. to launcher.db.bak. olderBackupPath := fmt.Sprintf("%s.%d", baseBackupPath, i+1) if err := os.Rename(currentBackupPath, olderBackupPath); err != nil {