Add backup directory path setting (#2953)

* add backup directory path setting
* Don't default backup path
* handle migration backup path input when given filename or path

Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
7dJx1qP
2022-09-29 20:00:50 -04:00
committed by GitHub
parent ad7fbce5f7
commit d274f86390
12 changed files with 78 additions and 14 deletions

View File

@@ -84,6 +84,15 @@ func (r *mutationResolver) ConfigureGeneral(ctx context.Context, input ConfigGen
c.Set(config.Database, input.DatabasePath)
}
existingBackupDirectoryPath := c.GetBackupDirectoryPath()
if input.BackupDirectoryPath != nil && existingBackupDirectoryPath != *input.BackupDirectoryPath {
if err := validateDir(config.BackupDirectoryPath, *input.BackupDirectoryPath, false); err != nil {
return makeConfigGeneralResult(), err
}
c.Set(config.BackupDirectoryPath, input.BackupDirectoryPath)
}
existingGeneratedPath := c.GetGeneratedPath()
if input.GeneratedPath != nil && existingGeneratedPath != *input.GeneratedPath {
if err := validateDir(config.Generated, *input.GeneratedPath, false); err != nil {

View File

@@ -124,7 +124,13 @@ func (r *mutationResolver) BackupDatabase(ctx context.Context, input BackupDatab
backupPath = f.Name()
f.Close()
} else {
backupPath = database.DatabaseBackupPath()
backupDirectoryPath := mgr.Config.GetBackupDirectoryPathOrDefault()
if backupDirectoryPath != "" {
if err := fsutil.EnsureDir(backupDirectoryPath); err != nil {
return nil, fmt.Errorf("could not create backup directory %v: %w", backupDirectoryPath, err)
}
}
backupPath = database.DatabaseBackupPath(backupDirectoryPath)
}
err := database.Backup(backupPath)
@@ -141,7 +147,7 @@ func (r *mutationResolver) BackupDatabase(ctx context.Context, input BackupDatab
baseURL, _ := ctx.Value(BaseURLCtxKey).(string)
fn := filepath.Base(database.DatabaseBackupPath())
fn := filepath.Base(database.DatabaseBackupPath(""))
ret := baseURL + "/downloads/" + downloadHash + "/" + fn
return &ret, nil
} else {

View File

@@ -85,6 +85,7 @@ func makeConfigGeneralResult() *ConfigGeneralResult {
return &ConfigGeneralResult{
Stashes: config.GetStashPaths(),
DatabasePath: config.GetDatabasePath(),
BackupDirectoryPath: config.GetBackupDirectoryPath(),
GeneratedPath: config.GetGeneratedPath(),
MetadataPath: config.GetMetadataPath(),
ConfigFilePath: config.GetConfigFile(),

View File

@@ -26,15 +26,16 @@ import (
var officialBuild string
const (
Stash = "stash"
Cache = "cache"
Generated = "generated"
Metadata = "metadata"
Downloads = "downloads"
ApiKey = "api_key"
Username = "username"
Password = "password"
MaxSessionAge = "max_session_age"
Stash = "stash"
Cache = "cache"
BackupDirectoryPath = "backup_directory_path"
Generated = "generated"
Metadata = "metadata"
Downloads = "downloads"
ApiKey = "api_key"
Username = "username"
Password = "password"
MaxSessionAge = "max_session_age"
DefaultMaxSessionAge = 60 * 60 * 1 // 1 hours
@@ -525,6 +526,19 @@ func (i *Instance) GetDatabasePath() string {
return i.getString(Database)
}
func (i *Instance) GetBackupDirectoryPath() string {
return i.getString(BackupDirectoryPath)
}
func (i *Instance) GetBackupDirectoryPathOrDefault() string {
ret := i.GetBackupDirectoryPath()
if ret == "" {
return i.GetConfigPath()
}
return ret
}
func (i *Instance) GetJWTSignKey() []byte {
return []byte(i.getString(JWTSignKey))
}
@@ -1351,6 +1365,7 @@ func (i *Instance) setDefaultValues(write bool) error {
// Set default scrapers and plugins paths
i.main.SetDefault(ScrapersPath, defaultScrapersPath)
i.main.SetDefault(PluginsPath, defaultPluginsPath)
if write {
return i.main.WriteConfig()
}

View File

@@ -26,6 +26,7 @@ func TestConcurrentConfigAccess(t *testing.T) {
i.GetConfigFile()
i.GetConfigPath()
i.GetDefaultDatabaseFilePath()
i.Set(BackupDirectoryPath, i.GetBackupDirectoryPath())
i.GetStashPaths()
_ = i.ValidateStashBoxes(nil)
_ = i.Validate()

View File

@@ -645,7 +645,14 @@ func (s *Manager) Migrate(ctx context.Context, input MigrateInput) error {
// migration fails
backupPath := input.BackupPath
if backupPath == "" {
backupPath = database.DatabaseBackupPath()
backupPath = database.DatabaseBackupPath(s.Config.GetBackupDirectoryPath())
} else {
// check if backup path is a filename or path
// filename goes into backup directory, path is kept as is
filename := filepath.Base(backupPath)
if backupPath == filename {
backupPath = filepath.Join(s.Config.GetBackupDirectoryPathOrDefault(), filename)
}
}
// perform database backup