Database: Add restore command

This commit is contained in:
Michael Mayer
2020-12-11 13:52:34 +01:00
parent 20feb6f0a0
commit 8f80026b3e
7 changed files with 191 additions and 15 deletions

View File

@@ -63,6 +63,7 @@ func main() {
commands.ResampleCommand, commands.ResampleCommand,
commands.MigrateCommand, commands.MigrateCommand,
commands.BackupCommand, commands.BackupCommand,
commands.RestoreCommand,
commands.ResetCommand, commands.ResetCommand,
commands.ConfigCommand, commands.ConfigCommand,
commands.PasswdCommand, commands.PasswdCommand,

View File

@@ -5,13 +5,14 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/photoprism/photoprism/pkg/txt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"time" "time"
"github.com/photoprism/photoprism/pkg/txt"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@@ -19,7 +20,7 @@ import (
// BackupCommand configures the backup cli command. // BackupCommand configures the backup cli command.
var BackupCommand = cli.Command{ var BackupCommand = cli.Command{
Name: "backup", Name: "backup",
Usage: "Creates a database backup", Usage: "Creates an index database backup",
Flags: backupFlags, Flags: backupFlags,
Action: backupAction, Action: backupAction,
} }
@@ -31,7 +32,7 @@ var backupFlags = []cli.Flag{
}, },
} }
// migrateAction automatically migrates or initializes database // backupAction creates a database backup.
func backupAction(ctx *cli.Context) error { func backupAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
@@ -55,8 +56,7 @@ func backupAction(ctx *cli.Context) error {
} }
if _, err := os.Stat(fileName); err == nil && !ctx.Bool("force") { if _, err := os.Stat(fileName); err == nil && !ctx.Bool("force") {
log.Errorf("backup file already exists: %s", fileName) return fmt.Errorf("backup file already exists: %s", fileName)
return nil
} else if err == nil { } else if err == nil {
log.Warnf("replacing existing backup file") log.Warnf("replacing existing backup file")
} }

View File

@@ -11,11 +11,11 @@ import (
// MigrateCommand is used to register the migrate cli command // MigrateCommand is used to register the migrate cli command
var MigrateCommand = cli.Command{ var MigrateCommand = cli.Command{
Name: "migrate", Name: "migrate",
Usage: "Automatically initializes and migrates the database", Usage: "Initializes and migrates the index database if needed",
Action: migrateAction, Action: migrateAction,
} }
// migrateAction automatically migrates or initializes database // migrateAction initializes and migrates the database.
func migrateAction(ctx *cli.Context) error { func migrateAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()

View File

@@ -0,0 +1,163 @@
package commands
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os/exec"
"path/filepath"
"regexp"
"time"
"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{
Name: "restore",
Usage: "Restores the index from a backup",
Flags: restoreFlags,
Action: restoreAction,
}
var restoreFlags = []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "overwrite existing index",
},
}
// restoreAction restores a database backup.
func restoreAction(ctx *cli.Context) error {
start := time.Now()
conf := config.NewConfig(ctx)
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err := conf.Init(); err != nil {
return err
}
// Use command argument as backup file name.
fileName := ctx.Args().First()
// If empty, use default backup file name.
if fileName == "" {
backupPath := filepath.Join(conf.BackupPath(), conf.DatabaseDriver())
matches, err := filepath.Glob(filepath.Join(regexp.QuoteMeta(backupPath), "*.sql"))
if err != nil {
return err
}
if len(matches) == 0 {
log.Errorf("no backup files found in %s", backupPath)
return nil
}
fileName = matches[len(matches)-1]
}
if !fs.FileExists(fileName) {
log.Errorf("backup file not found: %s", fileName)
return nil
}
counts := struct{ Photos int }{}
conf.Db().Unscoped().Table("photos").
Select("COUNT(*) AS photos").
Take(&counts)
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)
}
log.Infof("restoring index from %s", txt.Quote(fileName))
sqlBackup, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
entity.SetDbProvider(conf)
tables := entity.Entities
var cmd *exec.Cmd
switch conf.DatabaseDriver() {
case config.MySQL:
cmd = exec.Command(
conf.MysqlBin(),
"-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())
}
// Fetch command output.
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
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")
}
}
log.Infoln("migrating database")
conf.InitDb()
elapsed := time.Since(start)
log.Infof("database restored in %s", elapsed)
conf.Shutdown()
return nil
}

View File

@@ -19,10 +19,10 @@ import (
) )
var dsnPattern = regexp.MustCompile( var dsnPattern = regexp.MustCompile(
`^(?:(?P<user>.*?)(?::(?P<password>.*))?@)?` + `^(?:(?P<user>.*?)(?::(?P<password>.*))?@)?` +
`(?:(?P<net>[^\(]*)(?:\((?P<server>[^\)]*)\))?)?` + `(?:(?P<net>[^\(]*)(?:\((?P<server>[^\)]*)\))?)?` +
`\/(?P<name>.*?)` + `\/(?P<name>.*?)` +
`(?:\?(?P<params>[^\?]*))?$`) `(?:\?(?P<params>[^\?]*))?$`)
// DatabaseDriver returns the database driver name. // DatabaseDriver returns the database driver name.
func (c *Config) DatabaseDriver() string { func (c *Config) DatabaseDriver() string {
@@ -231,6 +231,12 @@ func (c *Config) InitTestDb() {
go entity.SaveErrorMessages() go entity.SaveErrorMessages()
} }
// TruncateDb drops all contents so that they can be restored from a backup.
func (c *Config) TruncateDb() {
entity.SetDbProvider(c)
entity.Entities.Truncate()
}
// connectDb establishes a database connection. // connectDb establishes a database connection.
func (c *Config) connectDb() error { func (c *Config) connectDb() error {
mutex.Db.Lock() mutex.Db.Lock()

View File

@@ -1,8 +1,9 @@
package config package config
import ( import (
"github.com/stretchr/testify/assert"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestConfig_DatabaseDriver(t *testing.T) { func TestConfig_DatabaseDriver(t *testing.T) {
@@ -14,7 +15,7 @@ func TestConfig_DatabaseDriver(t *testing.T) {
func TestConfig_ParseDatabaseDsn(t *testing.T) { func TestConfig_ParseDatabaseDsn(t *testing.T) {
c := NewConfig(CliTestContext()) c := NewConfig(CliTestContext())
c.params.DatabaseDsn ="foo:b@r@tcp(honeypot:1234)/baz?charset=utf8mb4,utf8&parseTime=true" c.params.DatabaseDsn = "foo:b@r@tcp(honeypot:1234)/baz?charset=utf8mb4,utf8&parseTime=true"
assert.Equal(t, "honeypot:1234", c.DatabaseServer()) assert.Equal(t, "honeypot:1234", c.DatabaseServer())
assert.Equal(t, "honeypot", c.DatabaseHost()) assert.Equal(t, "honeypot", c.DatabaseHost())
@@ -65,4 +66,4 @@ func TestConfig_DatabaseDsn(t *testing.T) {
dsn := c.DatabaseDriver() dsn := c.DatabaseDriver()
assert.Equal(t, SQLite, dsn) assert.Equal(t, SQLite, dsn)
} }

View File

@@ -341,6 +341,11 @@ func (c *Config) TestdataPath() string {
return filepath.Join(c.StoragePath(), "testdata") return filepath.Join(c.StoragePath(), "testdata")
} }
// MysqlBin returns the mysql executable file name.
func (c *Config) MysqlBin() string {
return findExecutable("", "mysql")
}
// MysqldumpBin returns the mysqldump executable file name. // MysqldumpBin returns the mysqldump executable file name.
func (c *Config) MysqldumpBin() string { func (c *Config) MysqldumpBin() string {
return findExecutable("", "mysqldump") return findExecutable("", "mysqldump")
@@ -349,4 +354,4 @@ func (c *Config) MysqldumpBin() string {
// SqliteBin returns the sqlite executable file name. // SqliteBin returns the sqlite executable file name.
func (c *Config) SqliteBin() string { func (c *Config) SqliteBin() string {
return findExecutable("", "sqlite3") return findExecutable("", "sqlite3")
} }