2020-12-11 13:52:34 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-10-06 07:10:50 +02:00
|
|
|
"os"
|
2020-12-11 13:52:34 +01:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
2021-10-07 13:17:16 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
2020-12-17 18:24:55 +01:00
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-12-11 13:52:34 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-12-11 13:52:34 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
2023-04-01 16:23:25 +02:00
|
|
|
const restoreDescription = "A user-defined filename or - for stdin can be passed as the first argument. " +
|
2022-01-05 11:40:44 +01:00
|
|
|
"The -i parameter can be omitted in this case.\n" +
|
2022-01-06 13:21:42 +01:00
|
|
|
" The index backup and album file paths are automatically detected if not specified explicitly."
|
2022-01-05 11:40:44 +01:00
|
|
|
|
2023-02-14 11:37:22 +01:00
|
|
|
// RestoreCommand configures the command name, flags, and action.
|
2020-12-11 13:52:34 +01:00
|
|
|
var RestoreCommand = cli.Command{
|
2022-01-05 11:40:44 +01:00
|
|
|
Name: "restore",
|
|
|
|
Description: restoreDescription,
|
2023-04-01 16:23:25 +02:00
|
|
|
Usage: "Restores the index from a backup and optionally albums from YAML files",
|
|
|
|
ArgsUsage: "[filename]",
|
2022-01-05 11:40:44 +01:00
|
|
|
Flags: restoreFlags,
|
|
|
|
Action: restoreAction,
|
2020-12-11 13:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var restoreFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2021-10-07 13:00:32 +02:00
|
|
|
Usage: "replace existing index",
|
2020-12-11 13:52:34 +01:00
|
|
|
},
|
2020-12-17 18:24:55 +01:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "albums, a",
|
2022-01-06 13:21:42 +01:00
|
|
|
Usage: "restore albums from YAML files",
|
2020-12-17 18:24:55 +01:00
|
|
|
},
|
2021-04-20 08:40:39 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "albums-path",
|
2022-01-06 13:21:42 +01:00
|
|
|
Usage: "custom album files `PATH`",
|
2021-04-20 08:40:39 +02:00
|
|
|
},
|
2020-12-17 18:24:55 +01:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "index, i",
|
2023-04-01 16:23:25 +02:00
|
|
|
Usage: "restore index from backup",
|
2021-04-20 08:40:39 +02:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "index-path",
|
2022-01-05 11:40:44 +01:00
|
|
|
Usage: "custom index backup `PATH`",
|
2020-12-17 18:24:55 +01:00
|
|
|
},
|
2020-12-11 13:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// restoreAction restores a database backup.
|
|
|
|
func restoreAction(ctx *cli.Context) error {
|
2021-04-20 08:40:39 +02:00
|
|
|
// 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 {
|
2022-01-05 11:40:44 +01:00
|
|
|
return cli.ShowSubcommandHelp(ctx)
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 13:52:34 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
2020-12-11 13:52:34 +01:00
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if err != nil {
|
2020-12-11 13:52:34 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
conf.RegisterDb()
|
|
|
|
defer conf.Shutdown()
|
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
if restoreIndex {
|
2020-12-17 18:24:55 +01:00
|
|
|
// If empty, use default backup file name.
|
2021-04-20 08:40:39 +02:00
|
|
|
if indexFileName == "" {
|
|
|
|
if indexPath == "" {
|
|
|
|
indexPath = filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
|
|
|
|
}
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
matches, err := filepath.Glob(filepath.Join(regexp.QuoteMeta(indexPath), "*.sql"))
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
2023-04-01 16:23:25 +02:00
|
|
|
log.Errorf("no backup files found in %s", indexPath)
|
2020-12-17 18:24:55 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
indexFileName = matches[len(matches)-1]
|
2020-12-11 13:52:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
counts := struct{ Photos int }{}
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
conf.Db().Unscoped().Table("photos").
|
|
|
|
Select("COUNT(*) AS photos").
|
|
|
|
Take(&counts)
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if counts.Photos == 0 {
|
|
|
|
// Do nothing;
|
|
|
|
} else if !ctx.Bool("force") {
|
2023-04-01 16:23:25 +02:00
|
|
|
return fmt.Errorf("found exisisting index with %d pictures, use --force to replace it", counts.Photos)
|
2020-12-17 18:24:55 +01:00
|
|
|
} else {
|
2023-04-01 16:23:25 +02:00
|
|
|
log.Warnf("replacing existing index with %d pictures", counts.Photos)
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
tables := entity.Entities
|
|
|
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
|
|
|
switch conf.DatabaseDriver() {
|
|
|
|
case config.MySQL, config.MariaDB:
|
|
|
|
cmd = exec.Command(
|
|
|
|
conf.MysqlBin(),
|
2021-02-16 08:59:57 +01:00
|
|
|
"--protocol", "tcp",
|
2020-12-17 18:24:55 +01:00
|
|
|
"-h", conf.DatabaseHost(),
|
|
|
|
"-P", conf.DatabasePortString(),
|
|
|
|
"-u", conf.DatabaseUser(),
|
|
|
|
"-p"+conf.DatabasePassword(),
|
|
|
|
"-f",
|
|
|
|
conf.DatabaseName(),
|
|
|
|
)
|
2021-12-09 07:47:23 +01:00
|
|
|
case config.SQLite3:
|
2020-12-17 18:24:55 +01:00
|
|
|
log.Infoln("dropping existing tables")
|
2021-11-28 13:52:27 +01:00
|
|
|
tables.Drop(conf.Db())
|
2020-12-17 18:24:55 +01:00
|
|
|
cmd = exec.Command(
|
|
|
|
conf.SqliteBin(),
|
2023-04-01 16:23:25 +02:00
|
|
|
conf.DatabaseFile(),
|
2020-12-17 18:24:55 +01:00
|
|
|
)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported database type: %s", conf.DatabaseDriver())
|
|
|
|
}
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2023-04-01 16:23:25 +02:00
|
|
|
// Read from stdin or file.
|
|
|
|
var f *os.File
|
|
|
|
if indexFileName == "-" {
|
|
|
|
log.Infof("restoring index from stdin")
|
|
|
|
f = os.Stdin
|
|
|
|
} else if f, err = os.OpenFile(indexFileName, os.O_RDONLY, 0); err != nil {
|
|
|
|
return fmt.Errorf("failed to open %s: %s", clean.Log(indexFileName), err)
|
|
|
|
} else {
|
|
|
|
log.Infof("restoring index from %s", clean.Log(indexFileName))
|
|
|
|
defer f.Close()
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
var stderr bytes.Buffer
|
2023-04-01 16:23:25 +02:00
|
|
|
var stdin io.WriteCloser
|
2020-12-17 18:24:55 +01:00
|
|
|
cmd.Stderr = &stderr
|
2023-04-01 16:23:25 +02:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
stdin, err = cmd.StdinPipe()
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2020-12-11 13:52:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
go func() {
|
|
|
|
defer stdin.Close()
|
2023-04-01 16:23:25 +02:00
|
|
|
if _, err = io.Copy(stdin, f); err != nil {
|
2020-12-17 18:24:55 +01:00
|
|
|
log.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-03-02 12:20:02 +01:00
|
|
|
// Log exact command for debugging in trace mode.
|
|
|
|
log.Trace(cmd.String())
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 13:17:16 +02:00
|
|
|
log.Infoln("migrating index database schema")
|
2020-12-11 13:52:34 +01:00
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
if restoreAlbums {
|
2022-10-15 21:54:11 +02:00
|
|
|
get.SetConfig(conf)
|
2020-12-17 18:24:55 +01:00
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
if albumsPath == "" {
|
|
|
|
albumsPath = conf.AlbumsPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fs.PathExists(albumsPath) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("album files path %s not found", clean.Log(albumsPath))
|
2020-12-17 18:24:55 +01:00
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("restoring albums from %s", clean.Log(albumsPath))
|
2021-04-20 08:40:39 +02:00
|
|
|
|
|
|
|
if count, err := photoprism.RestoreAlbums(albumsPath, true); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("restored %s from YAML files", english.Plural(count, "album", "albums"))
|
2021-04-20 08:40:39 +02:00
|
|
|
}
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:52:34 +01:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2022-01-05 11:40:44 +01:00
|
|
|
log.Infof("restored in %s", elapsed)
|
2020-12-11 13:52:34 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|