photoprism/internal/commands/restore.go

226 lines
5 KiB
Go
Raw Normal View History

2020-12-11 13:52:34 +01:00
package commands
import (
"bytes"
"context"
"fmt"
"io"
"os"
2020-12-11 13:52:34 +01:00
"os/exec"
"path/filepath"
"regexp"
"time"
"github.com/dustin/go-humanize/english"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
2020-12-11 13:52:34 +01:00
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/get"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/pkg/clean"
2020-12-11 13:52:34 +01:00
"github.com/photoprism/photoprism/pkg/fs"
)
const restoreDescription = "A user-defined SQL dump FILENAME can be passed as the first argument. " +
"The -i parameter can be omitted in this case.\n" +
" The index backup and album file paths are automatically detected if not specified explicitly."
2020-12-11 13:52:34 +01:00
// RestoreCommand configures the backup cli command.
var RestoreCommand = cli.Command{
Name: "restore",
Description: restoreDescription,
Usage: "Restores the index from an SQL dump and optionally albums from YAML files",
ArgsUsage: "filename.sql",
Flags: restoreFlags,
Action: restoreAction,
2020-12-11 13:52:34 +01:00
}
var restoreFlags = []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "replace existing index",
2020-12-11 13:52:34 +01:00
},
cli.BoolFlag{
Name: "albums, a",
Usage: "restore albums from YAML files",
},
cli.StringFlag{
Name: "albums-path",
Usage: "custom album files `PATH`",
},
cli.BoolFlag{
Name: "index, i",
Usage: "restore index from SQL dump",
},
cli.StringFlag{
Name: "index-path",
Usage: "custom index backup `PATH`",
},
2020-12-11 13:52:34 +01:00
}
// restoreAction restores a database backup.
func restoreAction(ctx *cli.Context) error {
// 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 {
return cli.ShowSubcommandHelp(ctx)
}
2020-12-11 13:52:34 +01:00
start := time.Now()
conf, err := InitConfig(ctx)
2020-12-11 13:52:34 +01:00
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err != nil {
2020-12-11 13:52:34 +01:00
return err
}
conf.RegisterDb()
defer conf.Shutdown()
if restoreIndex {
// If empty, use default backup file name.
if indexFileName == "" {
if indexPath == "" {
indexPath = filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
}
2020-12-11 13:52:34 +01:00
matches, err := filepath.Glob(filepath.Join(regexp.QuoteMeta(indexPath), "*.sql"))
2020-12-11 13:52:34 +01:00
if err != nil {
return err
}
if len(matches) == 0 {
log.Errorf("no SQL dumps found in %s", indexPath)
return nil
}
indexFileName = matches[len(matches)-1]
2020-12-11 13:52:34 +01:00
}
if !fs.FileExists(indexFileName) {
log.Errorf("SQL dump not found: %s", indexFileName)
2020-12-11 13:52:34 +01:00
return nil
}
counts := struct{ Photos int }{}
2020-12-11 13:52:34 +01:00
conf.Db().Unscoped().Table("photos").
Select("COUNT(*) AS photos").
Take(&counts)
2020-12-11 13:52:34 +01:00
if counts.Photos == 0 {
// Do nothing;
} else if !ctx.Bool("force") {
return fmt.Errorf("use --force to replace exisisting index with %d photos", counts.Photos)
} else {
log.Warnf("replacing existing index with %d photos", counts.Photos)
}
2020-12-11 13:52:34 +01:00
log.Infof("restoring index from %s", clean.Log(indexFileName))
2020-12-11 13:52:34 +01:00
sqlBackup, err := os.ReadFile(indexFileName)
2020-12-11 13:52:34 +01:00
if err != nil {
return err
}
2020-12-11 13:52:34 +01:00
tables := entity.Entities
var cmd *exec.Cmd
switch conf.DatabaseDriver() {
case config.MySQL, config.MariaDB:
cmd = exec.Command(
conf.MysqlBin(),
"--protocol", "tcp",
"-h", conf.DatabaseHost(),
"-P", conf.DatabasePortString(),
"-u", conf.DatabaseUser(),
"-p"+conf.DatabasePassword(),
"-f",
conf.DatabaseName(),
)
case config.SQLite3:
log.Infoln("dropping existing tables")
tables.Drop(conf.Db())
cmd = exec.Command(
conf.SqliteBin(),
conf.DatabaseDsn(),
)
default:
return fmt.Errorf("unsupported database type: %s", conf.DatabaseDriver())
}
2020-12-11 13:52:34 +01:00
// Fetch command output.
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
2020-12-11 13:52:34 +01:00
stdin, err := cmd.StdinPipe()
2020-12-11 13:52:34 +01:00
if err != nil {
log.Fatal(err)
2020-12-11 13:52:34 +01:00
}
go func() {
defer stdin.Close()
if _, err := io.WriteString(stdin, string(sqlBackup)); err != nil {
log.Errorf(err.Error())
}
}()
// Log exact command for debugging in trace mode.
log.Trace(cmd.String())
// Run backup command.
if err := cmd.Run(); err != nil {
if stderr.String() != "" {
log.Debugln(stderr.String())
log.Warnf("index could not be restored completely")
}
2020-12-11 13:52:34 +01:00
}
}
log.Infoln("migrating index database schema")
2020-12-11 13:52:34 +01:00
conf.InitDb()
if restoreAlbums {
get.SetConfig(conf)
if albumsPath == "" {
albumsPath = conf.AlbumsPath()
}
if !fs.PathExists(albumsPath) {
log.Warnf("album files path %s not found", clean.Log(albumsPath))
} else {
log.Infof("restoring albums from %s", clean.Log(albumsPath))
if count, err := photoprism.RestoreAlbums(albumsPath, true); err != nil {
return err
} else {
log.Infof("restored %s from YAML files", english.Plural(count, "album", "albums"))
}
}
}
2020-12-11 13:52:34 +01:00
elapsed := time.Since(start)
log.Infof("restored in %s", elapsed)
2020-12-11 13:52:34 +01:00
return nil
}