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"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2020-12-11 12:46:28 +01:00
|
|
|
)
|
|
|
|
|
2022-01-05 11:40:44 +01:00
|
|
|
const backupDescription = "A user-defined SQL dump FILENAME or - for stdout can be passed as the first argument. " +
|
|
|
|
"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
|
|
|
|
2020-12-11 12:46:28 +01:00
|
|
|
// BackupCommand configures the backup cli command.
|
|
|
|
var BackupCommand = cli.Command{
|
2022-01-05 11:40:44 +01:00
|
|
|
Name: "backup",
|
|
|
|
Description: backupDescription,
|
2022-01-06 13:21:42 +01:00
|
|
|
Usage: "Creates an index SQL dump and optionally album YAML files organized by type",
|
2022-01-05 11:40:44 +01:00
|
|
|
ArgsUsage: "[FILENAME | -]",
|
|
|
|
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",
|
2022-01-05 11:40:44 +01:00
|
|
|
Usage: "create index SQL dump",
|
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()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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") {
|
2022-01-05 11:40:44 +01:00
|
|
|
return fmt.Errorf("SQL dump already exists: %s", indexFileName)
|
2021-04-19 13:48:46 +02:00
|
|
|
} else if err == nil {
|
2022-01-05 11:40:44 +01:00
|
|
|
log.Warnf("replacing existing SQL dump")
|
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 != "." {
|
|
|
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-17 18:24:55 +01:00
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
|
2022-01-05 11:40:44 +01:00
|
|
|
log.Infof("writing SQL dump to %s", sanitize.Log(indexFileName))
|
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(),
|
|
|
|
conf.DatabaseDsn(),
|
|
|
|
".dump",
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported database type: %s", conf.DatabaseDriver())
|
|
|
|
}
|
2020-12-11 12:46:28 +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
|
|
|
|
|
|
|
|
// 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 indexFileName == "-" {
|
|
|
|
// Return output via stdout.
|
|
|
|
fmt.Println(out.String())
|
|
|
|
} else {
|
|
|
|
// Write output to file.
|
2021-10-06 07:10:50 +02:00
|
|
|
if err := os.WriteFile(indexFileName, []byte(out.String()), os.ModePerm); err != nil {
|
2021-04-19 13:48:46 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-12-11 12:46:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:48:46 +02:00
|
|
|
if backupAlbums {
|
2020-12-17 18:24:55 +01:00
|
|
|
service.SetConfig(conf)
|
|
|
|
conf.InitDb()
|
|
|
|
|
2021-04-19 13:48:46 +02:00
|
|
|
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-01-06 13:21:42 +01:00
|
|
|
log.Infof("saving albums in %s", sanitize.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)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("backup completed in %s", elapsed)
|
2020-12-11 12:46:28 +01:00
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|