Skip to content

Backup file not using atomic write #4816

@e-gineer

Description

@e-gineer

Description

The takeBackup() function writes directly to the final backup file path using pg_dump's --file parameter. If the backup fails midway (disk full, process killed, etc.), a partial backup file may be left behind. This is similar to bug #4718 which was fixed for export files by using atomic writes (write to temp file, then rename).

Severity

HIGH - File corruption / partial backups

Reproduction

  1. Start backup operation
  2. Kill process or fill disk during backup
  3. Partial .dump file remains in backup directory
  4. Subsequent operations may attempt to use corrupt backup file

Expected Behavior

Backup should use atomic write pattern: write to temp file, verify success, then rename to final location.

Test Reference

Test: TestTakeBackup_AtomicWrite in pkg/db/db_local/backup_test.go:150 (skipped)

Suggested Fix

// Write to temp file first
tempFile := filepath.Join(os.TempDir(), fmt.Sprintf("steampipe-backup-%d.tmp", time.Now().Unix()))
cmd := pgDumpCmd(ctx,
    fmt.Sprintf("--file=%s", tempFile),
    // ... other args
)
if err := cmd.Run(); err != nil {
    os.Remove(tempFile)
    return err
}
// Move to final location atomically
return os.Rename(tempFile, filepaths.DatabaseBackupFilePath())

Related Code

  • pkg/db/db_local/backup.go:127-149

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleNo recent activity has been detected on this issue/PR and it will be closed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions