Vacuum into database directory then move file if backup dir different (#6137)

If the backup directory is not the same directory as the database, then vacuum into the same directory then move it to its destination. This is to prevent issues vacuuming over a network share.
This commit is contained in:
WithoutPants 2025-10-15 16:30:06 +11:00 committed by GitHub
parent 2e8bc3536f
commit 7b182ac04b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -350,12 +350,30 @@ func (db *Database) Backup(backupPath string) (err error) {
defer thisDB.Close()
}
logger.Infof("Backing up database into: %s", backupPath)
_, err = thisDB.Exec(`VACUUM INTO "` + backupPath + `"`)
// if backup path is not in the same directory as the database,
// then backup to the same directory first, then move to the final location.
// This is to prevent errors if the backup directory is over a network share.
dbDir := filepath.Dir(db.dbPath)
moveAfter := filepath.Dir(backupPath) != dbDir
vacuumOut := backupPath
if moveAfter {
vacuumOut = filepath.Join(dbDir, filepath.Base(backupPath))
}
logger.Infof("Backing up database into: %s", vacuumOut)
_, err = thisDB.Exec(`VACUUM INTO "` + vacuumOut + `"`)
if err != nil {
return fmt.Errorf("vacuum failed: %w", err)
}
if moveAfter {
logger.Infof("Moving database backup to: %s", backupPath)
err = os.Rename(vacuumOut, backupPath)
if err != nil {
return fmt.Errorf("moving database backup failed: %w", err)
}
}
return nil
}