Skip to content

Commit

Permalink
Backup every 6 hours, fix error message, only rename if present
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed Jun 18, 2024
1 parent e75bd61 commit 8c03963
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ee/agent/storage/bbolt/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

const (
backupInitialDelay = 10 * time.Minute
backupInterval = 1 * time.Hour
backupInterval = 6 * time.Hour
numberOfOldBackupsToRetain = 3
)

Expand Down Expand Up @@ -82,7 +82,7 @@ func (d *databaseBackupSaver) Interrupt(_ error) {
func (d *databaseBackupSaver) backupDb() error {
// Perform rotation of older backups to prepare for newer backup
if err := d.rotate(); err != nil {
return fmt.Errorf("backup succeeded, but rotation did not: %w", err)
return fmt.Errorf("rotation did not succeed: %w", err)
}

// Take backup
Expand Down Expand Up @@ -136,8 +136,11 @@ func (d *databaseBackupSaver) rotate() error {
}
}

if err := os.Rename(baseBackupPath, fmt.Sprintf("%s.1", baseBackupPath)); err != nil {
return fmt.Errorf("rotating %s: %w", baseBackupPath, err)
// Rename launcher.db.bak, if present
if _, err := os.Stat(baseBackupPath); err == nil {
if err := os.Rename(baseBackupPath, fmt.Sprintf("%s.1", baseBackupPath)); err != nil {
return fmt.Errorf("rotating %s: %w", baseBackupPath, err)
}
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions ee/agent/storage/bbolt/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func Test_rotate(t *testing.T) {
}
}
}

// Test rotation when backup db does not exist
_ = os.Remove(backupDbFileLocation)
require.NoError(t, d.rotate(), "must be able to rotate even when launcher.db.bak does not exist")
}

func TestInterrupt_Multiple(t *testing.T) {
Expand Down

0 comments on commit 8c03963

Please sign in to comment.