restore functions
This commit is contained in:
parent
cce762cb78
commit
697c09c2c5
56
pkg/restore/root.go
Normal file
56
pkg/restore/root.go
Normal file
@ -0,0 +1,56 @@
|
||||
package restore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
||||
"github.com/desmo999r/formolcli/pkg/restic"
|
||||
"github.com/desmo999r/formolcli/pkg/session"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/zapr"
|
||||
"go.uber.org/zap"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
var (
|
||||
pg_restoreExec = "/usr/bin/pg_restore"
|
||||
logger logr.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
zapLog, _ := zap.NewDevelopment()
|
||||
logger = zapr.NewLogger(zapLog)
|
||||
}
|
||||
|
||||
func RestoreVolume(snapshotId string) error {
|
||||
log := logger.WithName("restore-volume")
|
||||
state := formolv1alpha1.Success
|
||||
repository := os.Getenv("RESTIC_REPOSITORY")
|
||||
output, err := restic.RestorePaths(repository, snapshotId)
|
||||
if err != nil {
|
||||
log.Error(err, "unable to restore volume", "output", string(output))
|
||||
state = formolv1alpha1.Failure
|
||||
}
|
||||
session.RestoreSessionUpdateTargetStatus(state)
|
||||
return err
|
||||
}
|
||||
|
||||
func RestorePostgres(file string, hostname string, database string, username string, password string) error {
|
||||
log := logger.WithName("restore-postgres")
|
||||
pgpass := []byte(fmt.Sprintf("%s:*:%s:%s:%s", hostname, database, username, password))
|
||||
if err := ioutil.WriteFile("/output/.pgpass", pgpass, 0600); err != nil {
|
||||
log.Error(err, "unable to write password to /output/.pgpass")
|
||||
return err
|
||||
}
|
||||
defer os.Remove("/output/.pgpass")
|
||||
cmd := exec.Command(pg_restoreExec, "--clean", "--create", "--file", file, "--host", hostname, "--dbname", database, "--username", username, "--no-password")
|
||||
cmd.Env = append(os.Environ(), "PGPASSFILE=/output/.pgpass")
|
||||
output, err := cmd.CombinedOutput()
|
||||
log.V(1).Info("postgres restore output", "output", string(output))
|
||||
if err != nil {
|
||||
log.Error(err, "something went wrong during the restore")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user