mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
In the next step, this worker can be configured to automatically create index and/or album backups at regular intervals. Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -1,20 +1,14 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize/english"
|
"github.com/dustin/go-humanize/english"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
|
||||||
"github.com/photoprism/photoprism/internal/config"
|
|
||||||
"github.com/photoprism/photoprism/internal/photoprism"
|
"github.com/photoprism/photoprism/internal/photoprism"
|
||||||
"github.com/photoprism/photoprism/pkg/clean"
|
"github.com/photoprism/photoprism/pkg/clean"
|
||||||
"github.com/photoprism/photoprism/pkg/fs"
|
"github.com/photoprism/photoprism/pkg/fs"
|
||||||
@@ -61,13 +55,12 @@ var backupFlags = []cli.Flag{
|
|||||||
// backupAction creates a database backup.
|
// backupAction creates a database backup.
|
||||||
func backupAction(ctx *cli.Context) error {
|
func backupAction(ctx *cli.Context) error {
|
||||||
// Use command argument as backup file name.
|
// Use command argument as backup file name.
|
||||||
indexFileName := ctx.Args().First()
|
fileName := ctx.Args().First()
|
||||||
indexPath := ctx.String("index-path")
|
backupPath := ctx.String("index-path")
|
||||||
backupIndex := ctx.Bool("index") || indexFileName != "" || indexPath != ""
|
backupIndex := ctx.Bool("index") || fileName != "" || backupPath != ""
|
||||||
|
|
||||||
albumsPath := ctx.String("albums-path")
|
albumsPath := ctx.String("albums-path")
|
||||||
|
|
||||||
backupAlbums := ctx.Bool("albums") || albumsPath != ""
|
backupAlbums := ctx.Bool("albums") || albumsPath != ""
|
||||||
|
force := ctx.Bool("force")
|
||||||
|
|
||||||
if !backupIndex && !backupAlbums {
|
if !backupIndex && !backupAlbums {
|
||||||
return cli.ShowSubcommandHelp(ctx)
|
return cli.ShowSubcommandHelp(ctx)
|
||||||
@@ -89,83 +82,21 @@ func backupAction(ctx *cli.Context) error {
|
|||||||
|
|
||||||
if backupIndex {
|
if backupIndex {
|
||||||
// If empty, use default backup file name.
|
// If empty, use default backup file name.
|
||||||
if indexFileName == "" {
|
if fileName == "" {
|
||||||
if !fs.PathWritable(indexPath) {
|
if !fs.PathWritable(backupPath) {
|
||||||
if indexPath != "" {
|
if backupPath != "" {
|
||||||
log.Warnf("custom index backup path not writable, using default")
|
log.Warnf("custom index backup path not writable, using default")
|
||||||
}
|
}
|
||||||
|
|
||||||
indexPath = filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
|
backupPath = filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
|
||||||
}
|
}
|
||||||
|
|
||||||
backupFile := time.Now().UTC().Format("2006-01-02") + ".sql"
|
backupFile := time.Now().UTC().Format("2006-01-02") + ".sql"
|
||||||
indexFileName = filepath.Join(indexPath, backupFile)
|
fileName = filepath.Join(backupPath, backupFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
if indexFileName != "-" {
|
if err = photoprism.BackupIndex(backupPath, fileName, fileName == "-", force); err != nil {
|
||||||
if _, err := os.Stat(indexFileName); err == nil && !ctx.Bool("force") {
|
return fmt.Errorf("failed to create %s: %w", clean.Log(fileName), err)
|
||||||
return fmt.Errorf("%s already exists", clean.Log(indexFileName))
|
|
||||||
} else if err == nil {
|
|
||||||
log.Warnf("replacing existing backup")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create backup directory if not exists.
|
|
||||||
if dir := filepath.Dir(indexFileName); dir != "." {
|
|
||||||
if err = fs.MkdirAll(dir); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var cmd *exec.Cmd
|
|
||||||
|
|
||||||
switch conf.DatabaseDriver() {
|
|
||||||
case config.MySQL, config.MariaDB:
|
|
||||||
cmd = exec.Command(
|
|
||||||
conf.MariadbDumpBin(),
|
|
||||||
"--protocol", "tcp",
|
|
||||||
"-h", conf.DatabaseHost(),
|
|
||||||
"-P", conf.DatabasePortString(),
|
|
||||||
"-u", conf.DatabaseUser(),
|
|
||||||
"-p"+conf.DatabasePassword(),
|
|
||||||
conf.DatabaseName(),
|
|
||||||
)
|
|
||||||
case config.SQLite3:
|
|
||||||
cmd = exec.Command(
|
|
||||||
conf.SqliteBin(),
|
|
||||||
conf.DatabaseFile(),
|
|
||||||
".dump",
|
|
||||||
)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unsupported database type: %s", conf.DatabaseDriver())
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.Stderr = &stderr
|
|
||||||
cmd.Stdout = f
|
|
||||||
|
|
||||||
// Log exact command for debugging in trace mode.
|
|
||||||
log.Trace(cmd.String())
|
|
||||||
|
|
||||||
// Run backup command.
|
|
||||||
if cmdErr := cmd.Run(); cmdErr != nil {
|
|
||||||
if errStr := strings.TrimSpace(stderr.String()); errStr != "" {
|
|
||||||
return errors.New(errStr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmdErr
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,3 +19,8 @@ func (c *Config) ExifToolJson() bool {
|
|||||||
func (c *Config) BackupYaml() bool {
|
func (c *Config) BackupYaml() bool {
|
||||||
return !c.DisableBackups()
|
return !c.DisableBackups()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BackupIndex checks if a regular index backup should be created.
|
||||||
|
func (c *Config) BackupIndex() bool {
|
||||||
|
return !c.DisableBackups()
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package mutex
|
|||||||
var (
|
var (
|
||||||
MainWorker = Activity{}
|
MainWorker = Activity{}
|
||||||
SyncWorker = Activity{}
|
SyncWorker = Activity{}
|
||||||
|
BackupWorker = Activity{}
|
||||||
ShareWorker = Activity{}
|
ShareWorker = Activity{}
|
||||||
MetaWorker = Activity{}
|
MetaWorker = Activity{}
|
||||||
FacesWorker = Activity{}
|
FacesWorker = Activity{}
|
||||||
|
|||||||
108
internal/photoprism/backup_index.go
Normal file
108
internal/photoprism/backup_index.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
package photoprism
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/photoprism/photoprism/internal/config"
|
||||||
|
"github.com/photoprism/photoprism/pkg/clean"
|
||||||
|
"github.com/photoprism/photoprism/pkg/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BackupIndex creates an SQL backup dump with the specified file and path name.
|
||||||
|
func BackupIndex(backupPath, fileName string, stdOut, force bool) (err error) {
|
||||||
|
c := Config()
|
||||||
|
|
||||||
|
if !stdOut {
|
||||||
|
if backupPath == "" {
|
||||||
|
backupPath = filepath.Join(c.BackupPath(), c.DatabaseDriver())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the backup path if it does not already exist.
|
||||||
|
if err = fs.MkdirAll(backupPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the backup path is writable.
|
||||||
|
if !fs.PathWritable(backupPath) {
|
||||||
|
return fmt.Errorf("backup: path is not writable")
|
||||||
|
}
|
||||||
|
|
||||||
|
if fileName == "" {
|
||||||
|
backupFile := time.Now().UTC().Format("2006-01-02") + ".sql"
|
||||||
|
fileName = filepath.Join(backupPath, backupFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = os.Stat(fileName); err == nil && !force {
|
||||||
|
return fmt.Errorf("%s already exists", clean.Log(fileName))
|
||||||
|
} else if err == nil {
|
||||||
|
log.Warnf("replacing existing backup")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create backup path if not exists.
|
||||||
|
if dir := filepath.Dir(fileName); dir != "." {
|
||||||
|
if err = fs.MkdirAll(dir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmd *exec.Cmd
|
||||||
|
|
||||||
|
switch c.DatabaseDriver() {
|
||||||
|
case config.MySQL, config.MariaDB:
|
||||||
|
cmd = exec.Command(
|
||||||
|
c.MariadbDumpBin(),
|
||||||
|
"--protocol", "tcp",
|
||||||
|
"-h", c.DatabaseHost(),
|
||||||
|
"-P", c.DatabasePortString(),
|
||||||
|
"-u", c.DatabaseUser(),
|
||||||
|
"-p"+c.DatabasePassword(),
|
||||||
|
c.DatabaseName(),
|
||||||
|
)
|
||||||
|
case config.SQLite3:
|
||||||
|
cmd = exec.Command(
|
||||||
|
c.SqliteBin(),
|
||||||
|
c.DatabaseFile(),
|
||||||
|
".dump",
|
||||||
|
)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported database type: %s", c.DatabaseDriver())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to stdout or file.
|
||||||
|
var f *os.File
|
||||||
|
if fileName == "-" {
|
||||||
|
log.Infof("writing backup to stdout")
|
||||||
|
f = os.Stdout
|
||||||
|
} else if f, err = os.OpenFile(fileName, os.O_TRUNC|os.O_RDWR|os.O_CREATE, fs.ModeFile); err != nil {
|
||||||
|
return fmt.Errorf("failed to create %s: %s", clean.Log(fileName), err)
|
||||||
|
} else {
|
||||||
|
log.Infof("writing backup to %s", clean.Log(fileName))
|
||||||
|
defer f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
cmd.Stdout = f
|
||||||
|
|
||||||
|
// Log exact command for debugging in trace mode.
|
||||||
|
log.Trace(cmd.String())
|
||||||
|
|
||||||
|
// Run backup command.
|
||||||
|
if cmdErr := cmd.Run(); cmdErr != nil {
|
||||||
|
if errStr := strings.TrimSpace(stderr.String()); errStr != "" {
|
||||||
|
return errors.New(errStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
89
internal/workers/backup.go
Normal file
89
internal/workers/backup.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package workers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime/debug"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/photoprism/photoprism/internal/config"
|
||||||
|
"github.com/photoprism/photoprism/internal/entity"
|
||||||
|
"github.com/photoprism/photoprism/internal/mutex"
|
||||||
|
"github.com/photoprism/photoprism/internal/photoprism"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Backup represents a background backup worker.
|
||||||
|
type Backup struct {
|
||||||
|
conf *config.Config
|
||||||
|
lastRun time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBackup returns a new Backup worker.
|
||||||
|
func NewBackup(conf *config.Config) *Backup {
|
||||||
|
return &Backup{conf: conf}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start creates index and album backups based on the current configuration.
|
||||||
|
func (w *Backup) Start(index, albums, force bool) (err error) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
err = fmt.Errorf("backup: %s (worker panic)\nstack: %s", r, debug.Stack())
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Return if no backups should be created.
|
||||||
|
if !index && !albums {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return error if backup worker is already running.
|
||||||
|
if err = mutex.BackupWorker.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer mutex.BackupWorker.Stop()
|
||||||
|
|
||||||
|
// Start creating backups.
|
||||||
|
start := time.Now()
|
||||||
|
backupPath := filepath.Join(w.conf.BackupPath(), w.conf.DatabaseDriver())
|
||||||
|
|
||||||
|
if index && albums {
|
||||||
|
log.Infof("backup: creating index and album backups")
|
||||||
|
} else if index {
|
||||||
|
log.Infof("backup: creating index backup")
|
||||||
|
} else if albums {
|
||||||
|
log.Infof("backup: creating album backup")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create index database backup.
|
||||||
|
if !index {
|
||||||
|
// Skip.
|
||||||
|
} else if err = photoprism.BackupIndex(backupPath, "", false, force); err != nil {
|
||||||
|
log.Errorf("backup: %s (backup index)", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mutex.BackupWorker.Canceled() {
|
||||||
|
return errors.New("backup: canceled")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create album YAML file backup.
|
||||||
|
if !albums {
|
||||||
|
// Skip.
|
||||||
|
} else if count, backupErr := photoprism.BackupAlbums(w.conf.AlbumsPath(), force); backupErr != nil {
|
||||||
|
log.Errorf("backup: %s (backup albums)", backupErr.Error())
|
||||||
|
} else if count > 0 {
|
||||||
|
log.Debugf("backup: %d albums saved as yaml files", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update time when worker was last executed.
|
||||||
|
w.lastRun = entity.TimeStamp()
|
||||||
|
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
// Show success message.
|
||||||
|
log.Infof("backup: completed in %s", elapsed)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
41
internal/workers/backup_test.go
Normal file
41
internal/workers/backup_test.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package workers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/photoprism/photoprism/internal/config"
|
||||||
|
"github.com/photoprism/photoprism/internal/mutex"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBackup_Start(t *testing.T) {
|
||||||
|
conf := config.TestConfig()
|
||||||
|
|
||||||
|
t.Logf("database-dsn: %s", conf.DatabaseDsn())
|
||||||
|
|
||||||
|
worker := NewBackup(conf)
|
||||||
|
|
||||||
|
assert.IsType(t, &Backup{}, worker)
|
||||||
|
|
||||||
|
if err := mutex.BackupWorker.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mutex should prevent worker from starting.
|
||||||
|
if err := worker.Start(true, true, true); err == nil {
|
||||||
|
t.Fatal("error expected")
|
||||||
|
}
|
||||||
|
|
||||||
|
mutex.BackupWorker.Stop()
|
||||||
|
|
||||||
|
// Start worker.
|
||||||
|
if err := worker.Start(true, true, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rerun worker.
|
||||||
|
if err := worker.Start(true, true, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/photoprism/photoprism/internal/config"
|
"github.com/photoprism/photoprism/internal/config"
|
||||||
"github.com/photoprism/photoprism/internal/event"
|
"github.com/photoprism/photoprism/internal/event"
|
||||||
|
"github.com/photoprism/photoprism/internal/photoprism"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
@@ -17,6 +18,7 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
c := config.TestConfig()
|
c := config.TestConfig()
|
||||||
defer c.CloseDb()
|
defer c.CloseDb()
|
||||||
|
photoprism.SetConfig(c)
|
||||||
|
|
||||||
code := m.Run()
|
code := m.Run()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user