Restore: Improve command flags and usage docs #1190 #1193

This commit is contained in:
Michael Mayer
2021-04-20 08:40:39 +02:00
parent de5a71f1a2
commit 55d502bbca
4 changed files with 59 additions and 28 deletions

View File

@@ -27,7 +27,7 @@ import (
var BackupCommand = cli.Command{
Name: "backup",
Usage: "Creates album and index backups",
UsageText: `A custom index sql backup FILENAME may be passed as first argument. Use - for stdout.`,
UsageText: `A custom index sql backup FILENAME may be passed as first argument. Use - for stdout. By default, the backup path is searched.`,
Flags: backupFlags,
Action: backupAction,
}
@@ -60,7 +60,6 @@ func backupAction(ctx *cli.Context) error {
// Use command argument as backup file name.
indexFileName := ctx.Args().First()
indexPath := ctx.String("index-path")
backupIndex := ctx.Bool("index") || indexFileName != "" || indexPath != ""
albumsPath := ctx.String("albums-path")
@@ -179,6 +178,8 @@ func backupAction(ctx *cli.Context) error {
albumsPath = conf.AlbumsPath()
}
log.Infof("backing up albums to %s", txt.Quote(albumsPath))
if count, err := photoprism.BackupAlbums(albumsPath, true); err != nil {
return err
} else {

View File

@@ -27,6 +27,7 @@ import (
var RestoreCommand = cli.Command{
Name: "restore",
Usage: "Restores album and index backups",
UsageText: `A custom index sql backup FILENAME may be passed as first argument. By default, the backup path is searched.`,
Flags: restoreFlags,
Action: restoreAction,
}
@@ -40,15 +41,31 @@ var restoreFlags = []cli.Flag{
Name: "albums, a",
Usage: "restore album yaml file backups",
},
cli.StringFlag{
Name: "albums-path",
Usage: "custom album yaml file backup `PATH`",
},
cli.BoolFlag{
Name: "index, i",
Usage: "restore index database backup",
Usage: "restore index sql database backup",
},
cli.StringFlag{
Name: "index-path",
Usage: "custom index sql database backup `PATH`",
},
}
// restoreAction restores a database backup.
func restoreAction(ctx *cli.Context) error {
if !ctx.Bool("index") && !ctx.Bool("albums") {
// Use command argument as backup file name.
indexFileName := ctx.Args().First()
indexPath := ctx.String("index-path")
restoreIndex := ctx.Bool("index") || indexFileName != "" || indexPath != ""
albumsPath := ctx.String("albums-path")
restoreAlbums := ctx.Bool("albums") || albumsPath != ""
if !restoreIndex && !restoreAlbums {
for _, flag := range restoreFlags {
fmt.Println(flag.String())
}
@@ -67,30 +84,29 @@ func restoreAction(ctx *cli.Context) error {
return err
}
if ctx.Bool("index") {
// Use command argument as backup file name.
fileName := ctx.Args().First()
if restoreIndex {
// If empty, use default backup file name.
if fileName == "" {
backupPath := filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
if indexFileName == "" {
if indexPath == "" {
indexPath = filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
}
matches, err := filepath.Glob(filepath.Join(regexp.QuoteMeta(backupPath), "*.sql"))
matches, err := filepath.Glob(filepath.Join(regexp.QuoteMeta(indexPath), "*.sql"))
if err != nil {
return err
}
if len(matches) == 0 {
log.Errorf("no backup files found in %s", backupPath)
log.Errorf("no backup files found in %s", indexPath)
return nil
}
fileName = matches[len(matches)-1]
indexFileName = matches[len(matches)-1]
}
if !fs.FileExists(fileName) {
log.Errorf("backup file not found: %s", fileName)
if !fs.FileExists(indexFileName) {
log.Errorf("backup file not found: %s", indexFileName)
return nil
}
@@ -108,9 +124,9 @@ func restoreAction(ctx *cli.Context) error {
log.Warnf("replacing existing index with %d photos", counts.Photos)
}
log.Infof("restoring index from %s", txt.Quote(fileName))
log.Infof("restoring index from %s", txt.Quote(indexFileName))
sqlBackup, err := ioutil.ReadFile(fileName)
sqlBackup, err := ioutil.ReadFile(indexFileName)
if err != nil {
return err
@@ -176,15 +192,25 @@ func restoreAction(ctx *cli.Context) error {
conf.InitDb()
if ctx.Bool("albums") {
if restoreAlbums {
service.SetConfig(conf)
if count, err := photoprism.RestoreAlbums(true); err != nil {
if albumsPath == "" {
albumsPath = conf.AlbumsPath()
}
if !fs.PathExists(albumsPath) {
log.Warnf("albums path %s not found", txt.Quote(albumsPath))
} else {
log.Infof("restoring albums from %s", txt.Quote(albumsPath))
if count, err := photoprism.RestoreAlbums(albumsPath, true); err != nil {
return err
} else {
log.Infof("%d albums restored from yaml files", count)
}
}
}
elapsed := time.Since(start)

View File

@@ -111,7 +111,7 @@ func startAction(ctx *cli.Context) error {
// start web server
go server.Start(cctx, conf)
if count, err := photoprism.RestoreAlbums(false); err != nil {
if count, err := photoprism.RestoreAlbums(conf.AlbumsPath(), false); err != nil {
log.Errorf("restore: %s", err)
} else if count > 0 {
log.Infof("%d albums restored", count)

View File

@@ -46,7 +46,7 @@ func BackupAlbums(backupPath string, force bool) (count int, result error) {
}
// RestoreAlbums restores all album YAML file backups.
func RestoreAlbums(force bool) (count int, result error) {
func RestoreAlbums(backupPath string, force bool) (count int, result error) {
c := Config()
if !c.BackupYaml() && !force {
@@ -65,7 +65,11 @@ func RestoreAlbums(force bool) (count int, result error) {
return count, nil
}
albums, err := filepath.Glob(regexp.QuoteMeta(c.AlbumsPath()) + "/**/*.yml")
if !fs.PathExists(backupPath) {
backupPath = c.AlbumsPath()
}
albums, err := filepath.Glob(regexp.QuoteMeta(backupPath) + "/**/*.yml")
if oAlbums, oErr := filepath.Glob(regexp.QuoteMeta(c.OriginalsAlbumsPath()) + "/**/*.yml"); oErr == nil {
err = nil