Skip to content

Commit

Permalink
Backups: Improve handling of custom backup file and path names #4243
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Mayer <[email protected]>
  • Loading branch information
lastzero committed May 15, 2024
1 parent 7735d8b commit 6a1c92a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/photoprism/restore_albums.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func RestoreAlbums(backupPath string, force bool) (count int, result error) {
}

if len(existing) > 0 && !force {
log.Debugf("restore: found existing albums, backups will not be restored")
log.Debugf("restore: found existing albums, backup not restored")
return count, nil
}

Expand Down
60 changes: 38 additions & 22 deletions internal/photoprism/restore_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,43 @@ func RestoreDatabase(backupPath, fileName string, fromStdIn, force bool) (err er
c := Config()

// If empty, use default backup file name.
if !fromStdIn && fileName == "" {
if backupPath == "" {
backupPath = c.BackupDatabasePath()
}

files, globErr := filepath.Glob(filepath.Join(regexp.QuoteMeta(backupPath), SqlBackupFileNamePattern))

if globErr != nil {
return globErr
}

if len(files) == 0 {
return fmt.Errorf("found no database backup files in %s", backupPath)
}

sort.Strings(files)

fileName = files[len(files)-1]

if !fs.FileExistsNotEmpty(fileName) {
return fmt.Errorf("no database backup found in %s", filepath.Base(fileName))
if !fromStdIn {
if fileName == "" {
if backupPath == "" {
backupPath = c.BackupDatabasePath()
}

files, globErr := filepath.Glob(filepath.Join(regexp.QuoteMeta(backupPath), SqlBackupFileNamePattern))

if globErr != nil {
return globErr
}

if len(files) == 0 {
return fmt.Errorf("found no database backup files in %s", backupPath)
}

sort.Strings(files)

fileName = files[len(files)-1]

if !fs.FileExistsNotEmpty(fileName) {
return fmt.Errorf("no database backup found in %s", filepath.Base(fileName))
}
} else if backupPath == "" {
if absName, absErr := filepath.Abs(fileName); absErr == nil && fs.FileExists(absName) {
fileName = absName
} else if dir := filepath.Dir(fileName); dir != "" && dir != "." {
return fmt.Errorf("file %s not found", clean.Log(fileName))
} else if absName = filepath.Join(c.BackupDatabasePath(), fileName); !fs.FileExists(absName) {
return fmt.Errorf("file %s not found in %s backup path", clean.Log(fileName), clean.Log(filepath.Base(c.BackupDatabasePath())))
} else {
fileName = absName
}
} else if absName, absErr := filepath.Abs(filepath.Join(backupPath, fileName)); absErr == nil && fs.FileExists(absName) {
fileName = absName
} else {
return fmt.Errorf("file %s not found in %s", clean.Log(filepath.Base(fileName)), clean.Log(backupPath))
}
}

Expand All @@ -62,7 +78,7 @@ func RestoreDatabase(backupPath, fileName string, fromStdIn, force bool) (err er
if counts.Photos == 0 {
// Do nothing;
} else if !force {
return fmt.Errorf("found an existing index with %d pictures, backup will not be restored", counts.Photos)
return fmt.Errorf("found an existing index with %d pictures, backup not restored", counts.Photos)
} else {
log.Warnf("restore: existing index with %d pictures will be replaced", counts.Photos)
}
Expand Down

0 comments on commit 6a1c92a

Please sign in to comment.