52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package appstate
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// ServerMigrationBackupDir is separate from local backup retention.
|
|
func ServerMigrationBackupDir(dbPath string) (string, error) {
|
|
root := filepath.Join(resolveBackupRoot(dbPath), "server-migrations")
|
|
if err := ValidateBackupLocation(root); err != nil {
|
|
return "", err
|
|
}
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
return "", fmt.Errorf("create server backup directory: %w", err)
|
|
}
|
|
resolved, err := filepath.EvalSymlinks(root)
|
|
if err != nil {
|
|
return "", fmt.Errorf("resolve server backup directory: %w", err)
|
|
}
|
|
if err := ValidateBackupLocation(resolved); err != nil {
|
|
return "", err
|
|
}
|
|
return resolved, nil
|
|
}
|
|
|
|
// ValidateBackupLocation checks the destination, including existing symlinks.
|
|
func ValidateBackupLocation(path string) error {
|
|
if err := validateBackupRoot(path); err != nil {
|
|
return err
|
|
}
|
|
existing := path
|
|
for {
|
|
if _, err := os.Lstat(existing); err == nil {
|
|
break
|
|
} else if !os.IsNotExist(err) {
|
|
return fmt.Errorf("inspect backup path: %w", err)
|
|
}
|
|
parent := filepath.Dir(existing)
|
|
if parent == existing {
|
|
return fmt.Errorf("backup path has no existing parent")
|
|
}
|
|
existing = parent
|
|
}
|
|
resolved, err := filepath.EvalSymlinks(existing)
|
|
if err != nil {
|
|
return fmt.Errorf("resolve backup path: %w", err)
|
|
}
|
|
return validateBackupRoot(resolved)
|
|
}
|