diff --git a/ee/agent/storage/bbolt/backup.go b/ee/agent/storage/bbolt/backup.go index f26315c6a..76ac5c28b 100644 --- a/ee/agent/storage/bbolt/backup.go +++ b/ee/agent/storage/bbolt/backup.go @@ -14,7 +14,7 @@ import ( const ( backupInitialDelay = 10 * time.Minute - backupInterval = 1 * time.Hour + backupInterval = 6 * time.Hour numberOfOldBackupsToRetain = 3 ) @@ -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 @@ -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 diff --git a/ee/agent/storage/bbolt/backup_test.go b/ee/agent/storage/bbolt/backup_test.go index 657b5c3de..c92cdd74f 100644 --- a/ee/agent/storage/bbolt/backup_test.go +++ b/ee/agent/storage/bbolt/backup_test.go @@ -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) {