1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

search for correct source directory when doing a restore (#8676)

This commit is contained in:
Matt Hook 2023-04-06 10:39:10 +12:00 committed by GitHub
parent b00aa68c2b
commit bf56bdb8f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,8 +3,10 @@ package backup
import ( import (
"context" "context"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -43,6 +45,12 @@ func RestoreArchive(archive io.Reader, password string, filestorePath string, ga
return errors.Wrap(err, "Failed to stop db") return errors.Wrap(err, "Failed to stop db")
} }
// At some point, backups were created containing a subdirectory, now we need to handle both
restorePath, err = getRestoreSourcePath(restorePath)
if err != nil {
return errors.Wrap(err, "failed to restore from backup. Portainer database missing from backup file")
}
if err = restoreFiles(restorePath, filestorePath); err != nil { if err = restoreFiles(restorePath, filestorePath); err != nil {
return errors.Wrap(err, "failed to restore the system state") return errors.Wrap(err, "failed to restore the system state")
} }
@ -59,6 +67,26 @@ func extractArchive(r io.Reader, destinationDirPath string) error {
return archive.ExtractTarGz(r, destinationDirPath) return archive.ExtractTarGz(r, destinationDirPath)
} }
func getRestoreSourcePath(dir string) (string, error) {
// find portainer.db or portainer.edb file. Return the parent directory
var portainerdbRegex = regexp.MustCompile(`^portainer.e?db$`)
backupDirPath := dir
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if portainerdbRegex.MatchString(d.Name()) {
backupDirPath = filepath.Dir(path)
return filepath.SkipDir
}
return nil
})
return backupDirPath, err
}
func restoreFiles(srcDir string, destinationDir string) error { func restoreFiles(srcDir string, destinationDir string) error {
for _, filename := range filesToRestore { for _, filename := range filesToRestore {
err := filesystem.CopyPath(filepath.Join(srcDir, filename), destinationDir) err := filesystem.CopyPath(filepath.Join(srcDir, filename), destinationDir)