Skip to content

Commit

Permalink
Move where we delete the oldest backup for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed Jun 18, 2024
1 parent 7a623ad commit e75bd61
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ee/agent/storage/bbolt/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,23 @@ 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
if _, err := os.Stat(currentBackupPath); err != nil && os.IsNotExist(err) {
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.<n> to launcher.db.bak.<n+1>
olderBackupPath := fmt.Sprintf("%s.%d", baseBackupPath, i+1)
if err := os.Rename(currentBackupPath, olderBackupPath); err != nil {
Expand Down

0 comments on commit e75bd61

Please sign in to comment.