2020-12-11 13:52:34 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
|
2020-12-11 13:52:34 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RestoreCommand configures the backup cli command.
|
|
|
|
var RestoreCommand = cli.Command{
|
2021-04-20 08:40:39 +02:00
|
|
|
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,
|
2020-12-11 13:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var restoreFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
|
|
|
Usage: "overwrite existing index",
|
|
|
|
},
|
2020-12-17 18:24:55 +01:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "albums, a",
|
|
|
|
Usage: "restore album yaml file backups",
|
|
|
|
},
|
2021-04-20 08:40:39 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "albums-path",
|
|
|
|
Usage: "custom album yaml file backup `PATH`",
|
|
|
|
},
|
2020-12-17 18:24:55 +01:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "index, i",
|
2021-04-20 08:40:39 +02:00
|
|
|
Usage: "restore index sql database backup",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "index-path",
|
|
|
|
Usage: "custom index sql database 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 {
|
2020-12-17 18:24:55 +01:00
|
|
|
for _, flag := range restoreFlags {
|
|
|
|
fmt.Println(flag.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:52:34 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-04-20 08:40:39 +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
|
|
|
}
|
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
if !fs.FileExists(indexFileName) {
|
|
|
|
log.Errorf("backup file not found: %s", indexFileName)
|
2020-12-11 13:52:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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") {
|
|
|
|
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
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
log.Infof("restoring index from %s", txt.Quote(indexFileName))
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
sqlBackup, err := ioutil.ReadFile(indexFileName)
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-11 13:52:34 +01:00
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
entity.SetDbProvider(conf)
|
|
|
|
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(),
|
|
|
|
)
|
|
|
|
case config.SQLite:
|
|
|
|
log.Infoln("dropping existing tables")
|
|
|
|
tables.Drop()
|
|
|
|
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
|
|
|
|
2020-12-17 18:24:55 +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
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
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()
|
|
|
|
if _, err := io.WriteString(stdin, string(sqlBackup)); err != nil {
|
|
|
|
log.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// 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 database")
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
if restoreAlbums {
|
2020-12-17 18:24:55 +01:00
|
|
|
service.SetConfig(conf)
|
|
|
|
|
2021-04-20 08:40:39 +02:00
|
|
|
if albumsPath == "" {
|
|
|
|
albumsPath = conf.AlbumsPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fs.PathExists(albumsPath) {
|
|
|
|
log.Warnf("albums path %s not found", txt.Quote(albumsPath))
|
2020-12-17 18:24:55 +01:00
|
|
|
} else {
|
2021-04-20 08:40:39 +02:00
|
|
|
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)
|
|
|
|
}
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:52:34 +01:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
log.Infof("backup restored in %s", elapsed)
|
2020-12-11 13:52:34 +01:00
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|