mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 08:26:00 +01:00
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:
parent
2e8bc3536f
commit
7b182ac04b
1 changed files with 20 additions and 2 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue