Backup & Restore: Refactor commands not to run into size limits #3140

See also #3301, #3311, and #3298.

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2023-04-01 16:23:25 +02:00
parent b6378a5c1f
commit c426a184c5
4 changed files with 63 additions and 52 deletions

View File

@@ -19,7 +19,7 @@ import (
"github.com/photoprism/photoprism/pkg/fs"
)
const backupDescription = "A user-defined SQL dump FILENAME or - for stdout can be passed as the first argument. " +
const backupDescription = "A user-defined 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" +
" The index backup and album file paths are automatically detected if not specified explicitly."
@@ -28,8 +28,8 @@ const backupDescription = "A user-defined SQL dump FILENAME or - for stdout can
var BackupCommand = cli.Command{
Name: "backup",
Description: backupDescription,
Usage: "Creates an index SQL dump and optionally album YAML files organized by type",
ArgsUsage: "[filename.sql | -]",
Usage: "Creates an index backup and optionally album YAML files organized by type",
ArgsUsage: "[filename]",
Flags: backupFlags,
Action: backupAction,
}
@@ -49,7 +49,7 @@ var backupFlags = []cli.Flag{
},
cli.BoolFlag{
Name: "index, i",
Usage: "create index SQL dump",
Usage: "create index backup",
},
cli.StringFlag{
Name: "index-path",
@@ -103,9 +103,9 @@ func backupAction(ctx *cli.Context) error {
if indexFileName != "-" {
if _, err := os.Stat(indexFileName); err == nil && !ctx.Bool("force") {
return fmt.Errorf("SQL dump already exists: %s", indexFileName)
return fmt.Errorf("%s already exists", clean.Log(indexFileName))
} else if err == nil {
log.Warnf("replacing existing SQL dump")
log.Warnf("replacing existing backup")
}
// Create backup directory if not exists.
@@ -114,8 +114,6 @@ func backupAction(ctx *cli.Context) error {
return err
}
}
log.Infof("writing SQL dump to %s", clean.Log(indexFileName))
}
var cmd *exec.Cmd
@@ -134,18 +132,28 @@ func backupAction(ctx *cli.Context) error {
case config.SQLite3:
cmd = exec.Command(
conf.SqliteBin(),
conf.DatabaseDsn(),
conf.DatabaseFile(),
".dump",
)
default:
return fmt.Errorf("unsupported database type: %s", conf.DatabaseDriver())
}
// Fetch command output.
var out bytes.Buffer
// 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()
}
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.Stdout = f
// Log exact command for debugging in trace mode.
log.Trace(cmd.String())
@@ -156,16 +164,6 @@ func backupAction(ctx *cli.Context) error {
return errors.New(stderr.String())
}
}
if indexFileName == "-" {
// Return output via stdout.
fmt.Println(out.String())
} else {
// Write output to file.
if err := os.WriteFile(indexFileName, []byte(out.String()), fs.ModeFile); err != nil {
return err
}
}
}
if backupAlbums {