37baabe134
* EE-384: add endpoint to set auto backup (#224) * EE-383: add endpoint to fetch backup settings (#231) * add get backup settings handler * add api docs desc * EE-382: restore from s3 (#233) * EE-381: add GET backup status handler (#234) * EE-385: Add S3 backup execute handler (#237) * add s3 backup execute handler * refactories inside `./api/backup/backup_scheduler.go` and `./api/backup/backup_scheduler.go` * fix tests * EE-375: added backup to S3 form * EE-376: added restore from S3 form * EE-377: Update Home screen to display last backup run status * update backup service with back end endpoints. * restart admin monitor during s3 restores * use go 1.13 * go 1.13 compatibility * EE-375: added cron-validator lib * EE-375: using enum to compare form types * EE-375: validate cron rule field * try fix windows build * EE-375 EE-376 backup and restore forms validation changes * fix(autobackup): update autobackup settings validation rules (#260) * fix(autobackup): automate backup to s3 fe update (#261) * EE-292: fixed typo in property. * EE-292: updated auto backup front end validation. * EE-292: updated lib to validate cron rule in front end * fix dependencies * bumped libcompose version Co-authored-by: Hui <arris_li@hotmail.com> Co-authored-by: Felix Han <felix.han@portainer.io> Co-authored-by: fhanportainer <79428273+fhanportainer@users.noreply.github.com>
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package backup
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
portainer "github.com/portainer/portainer/api"
|
|
"github.com/portainer/portainer/api/archive"
|
|
"github.com/portainer/portainer/api/crypto"
|
|
"github.com/portainer/portainer/api/http/offlinegate"
|
|
)
|
|
|
|
var filesToRestore = append(filesToBackup, "portainer.db")
|
|
|
|
// Restores system state from backup archive, will trigger system shutdown, when finished.
|
|
func RestoreArchive(archive io.Reader, password string, filestorePath string, gate *offlinegate.OfflineGate, datastore portainer.DataStore, shutdownTrigger context.CancelFunc) error {
|
|
var err error
|
|
if password != "" {
|
|
archive, err = decrypt(archive, password)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to decrypt the archive")
|
|
}
|
|
}
|
|
|
|
restorePath := filepath.Join(filestorePath, "restore", time.Now().Format("20060102150405"))
|
|
defer os.RemoveAll(filepath.Dir(restorePath))
|
|
|
|
err = extractArchive(archive, restorePath)
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot extract files from the archive. Please ensure the password is correct and try again")
|
|
}
|
|
|
|
unlock := gate.Lock()
|
|
defer unlock()
|
|
|
|
if err = datastore.Close(); err != nil {
|
|
return errors.Wrap(err, "Failed to stop db")
|
|
}
|
|
|
|
if err = restoreFiles(restorePath, filestorePath); err != nil {
|
|
return errors.Wrap(err, "failed to restore the system state")
|
|
}
|
|
|
|
shutdownTrigger()
|
|
return nil
|
|
}
|
|
|
|
func decrypt(r io.Reader, password string) (io.Reader, error) {
|
|
return crypto.AesDecrypt(r, []byte(password))
|
|
}
|
|
|
|
func extractArchive(r io.Reader, destinationDirPath string) error {
|
|
return archive.ExtractTarGz(r, destinationDirPath)
|
|
}
|
|
|
|
func restoreFiles(srcDir string, destinationDir string) error {
|
|
for _, filename := range filesToRestore {
|
|
err := copyPath(filepath.Join(srcDir, filename), destinationDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|