2020-12-11 12:46:28 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2021-10-07 13:00:32 +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-17 18:24:55 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-12-11 12:46:28 +01:00
|
|
|
)
|
|
|
|
|
2023-04-01 16:23:25 +02:00
|
|
|
const backupDescription = "A user-defined filename or - for stdout 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" +
|
|
|
|
" Make sure to run the command with exec -T when using Docker to prevent log messages from being sent to stdout.\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
|
|
|
// BackupCommand configures the command name, flags, and action.
|
2020-12-11 12:46:28 +01:00
|
|
|
var BackupCommand = cli.Command{
|
2022-01-05 11:40:44 +01:00
|
|
|
Name: "backup",
|
|
|
|
Description: backupDescription,
|
2023-04-01 16:23:25 +02:00
|
|
|
Usage: "Creates an index backup and optionally album YAML files organized by type",
|
|
|
|
ArgsUsage: "[filename]",
|
2022-01-05 11:40:44 +01:00
|
|
|
Flags: backupFlags,
|
|
|
|
Action: backupAction,
|
2020-12-11 12:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var backupFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
2022-01-05 11:40:44 +01:00
|
|
|
Usage: "replace existing files",
|
2020-12-11 12:46:28 +01:00
|
|
|
},
|
2020-12-17 18:24:55 +01:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "albums, a",
|
2022-01-06 13:21:42 +01:00
|
|
|
Usage: "create album YAML files organized by type",
|
2020-12-17 18:24:55 +01:00
|
|
|
},
|
2021-04-19 13:48:46 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "albums-path",
|
2022-01-06 13:21:42 +01:00
|
|
|
Usage: "custom album files `PATH`",
|
2021-04-19 13:48:46 +02:00
|
|
|
},
|
2020-12-17 18:24:55 +01:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "index, i",
|
2023-04-01 16:23:25 +02:00
|
|
|
Usage: "create index backup",
|
2021-04-19 13:48:46 +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 12:46:28 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 13:52:34 +01:00
|
|
|
// backupAction creates a database backup.
|
2020-12-11 12:46:28 +01:00
|
|
|
func backupAction(ctx *cli.Context) error {
|
2021-04-19 13:48:46 +02:00
|
|
|
// 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")
|
|
|
|
|
|
|
|
backupAlbums := ctx.Bool("albums") || albumsPath != ""
|
|
|
|
|
|
|
|
if !backupIndex && !backupAlbums {
|
2022-01-05 11:40:44 +01:00
|
|
|
return cli.ShowSubcommandHelp(ctx)
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:46:28 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
2020-12-11 12:46:28 +01:00
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if err != nil {
|
2020-12-11 12:46:28 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
conf.RegisterDb()
|
|
|
|
defer conf.Shutdown()
|
|
|
|
|
2021-04-19 13:48:46 +02:00
|
|
|
if backupIndex {
|
2020-12-17 18:24:55 +01:00
|
|
|
// If empty, use default backup file name.
|
2021-04-19 13:48:46 +02:00
|
|
|
if indexFileName == "" {
|
|
|
|
if !fs.PathWritable(indexPath) {
|
|
|
|
if indexPath != "" {
|
|
|
|
log.Warnf("custom index backup path not writable, using default")
|
|
|
|
}
|
|
|
|
|
|
|
|
indexPath = filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
backupFile := time.Now().UTC().Format("2006-01-02") + ".sql"
|
2021-04-19 13:48:46 +02:00
|
|
|
indexFileName = filepath.Join(indexPath, backupFile)
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
|
2021-04-19 13:48:46 +02:00
|
|
|
if indexFileName != "-" {
|
|
|
|
if _, err := os.Stat(indexFileName); err == nil && !ctx.Bool("force") {
|
2023-04-01 16:23:25 +02:00
|
|
|
return fmt.Errorf("%s already exists", clean.Log(indexFileName))
|
2021-04-19 13:48:46 +02:00
|
|
|
} else if err == nil {
|
2023-04-01 16:23:25 +02:00
|
|
|
log.Warnf("replacing existing backup")
|
2021-04-19 13:48:46 +02:00
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
|
2021-04-19 13:48:46 +02:00
|
|
|
// Create backup directory if not exists.
|
|
|
|
if dir := filepath.Dir(indexFileName); dir != "." {
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(dir, fs.ModeDir); err != nil {
|
2021-04-19 13:48:46 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
2021-04-19 13:48:46 +02:00
|
|
|
}
|
2020-12-17 18:24:55 +01:00
|
|
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
|
|
|
switch conf.DatabaseDriver() {
|
|
|
|
case config.MySQL, config.MariaDB:
|
|
|
|
cmd = exec.Command(
|
|
|
|
conf.MysqldumpBin(),
|
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(),
|
|
|
|
conf.DatabaseName(),
|
|
|
|
)
|
2021-12-09 07:47:23 +01:00
|
|
|
case config.SQLite3:
|
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
|
|
|
".dump",
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported database type: %s", conf.DatabaseDriver())
|
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
|
2023-04-01 16:23:25 +02:00
|
|
|
// Write to stdout or file.
|
|
|
|
var f *os.File
|
|
|
|
if indexFileName == "-" {
|
|
|
|
log.Infof("writing backup to stdout")
|
|
|
|
f = os.Stdout
|
|
|
|
} else if f, err = os.OpenFile(indexFileName, os.O_TRUNC|os.O_RDWR|os.O_CREATE, fs.ModeFile); err != nil {
|
|
|
|
return fmt.Errorf("failed to create %s: %s", clean.Log(indexFileName), err)
|
|
|
|
} else {
|
|
|
|
log.Infof("writing backup to %s", clean.Log(indexFileName))
|
|
|
|
defer f.Close()
|
|
|
|
}
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
2023-04-01 16:23:25 +02:00
|
|
|
cmd.Stdout = f
|
2020-12-17 18:24:55 +01:00
|
|
|
|
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() != "" {
|
|
|
|
return errors.New(stderr.String())
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
}
|
|
|
|
|
2021-04-19 13:48:46 +02:00
|
|
|
if backupAlbums {
|
|
|
|
if !fs.PathWritable(albumsPath) {
|
|
|
|
if albumsPath != "" {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Warnf("album files path not writable, using default")
|
2021-04-19 13:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
albumsPath = conf.AlbumsPath()
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("saving albums in %s", clean.Log(albumsPath))
|
2021-04-20 08:40:39 +02:00
|
|
|
|
2021-04-19 13:48:46 +02:00
|
|
|
if count, err := photoprism.BackupAlbums(albumsPath, true); err != nil {
|
2020-12-17 18:24:55 +01:00
|
|
|
return err
|
|
|
|
} else {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("created %s", english.Plural(count, "YAML album file", "YAML album files"))
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2020-12-11 12:46:28 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|