backup / restore of OnlineKind and JobKind work

This commit is contained in:
Jean-Marc ANDRE 2023-03-24 21:52:49 +01:00
parent 5e5e4a9a77
commit 560271a294
5 changed files with 48 additions and 4 deletions

View File

@ -50,7 +50,7 @@ func (r *BackupSessionReconciler) backupJob(tag string, target formolv1alpha1.Ta
paths := []string{}
for _, container := range target.Containers {
for _, job := range container.Job {
if err = r.runFunction(job.Name); err != nil {
if err = r.runFunction("backup-" + job.Name); err != nil {
r.Log.Error(err, "unable to run job")
return
}

View File

@ -55,12 +55,14 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reque
// we don't want a copy because we will modify and update it.
var target formolv1alpha1.Target
var restoreTargetStatus *formolv1alpha1.TargetStatus
var backupTargetStatus formolv1alpha1.TargetStatus
targetName := os.Getenv(formolv1alpha1.TARGET_NAME)
for i, t := range backupConf.Spec.Targets {
if t.TargetName == targetName {
target = t
restoreTargetStatus = &(restoreSession.Status.Targets[i])
backupTargetStatus = backupSession.Status.Targets[i]
break
}
}
@ -93,12 +95,33 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reque
// The restore is different if the Backup was an OnlineKind or a JobKind
switch target.BackupType {
case formolv1alpha1.JobKind:
r.Log.V(0).Info("restoring job backup", "target", target)
if err := r.restoreJob(target, backupTargetStatus); err != nil {
r.Log.Error(err, "unable to restore job", "target", target)
newSessionState = formolv1alpha1.Failure
} else {
r.Log.V(0).Info("job backup restore was a success", "target", target)
newSessionState = formolv1alpha1.Success
}
case formolv1alpha1.OnlineKind:
// The initContainer will update the SessionState of the target
// once it is done with the restore
r.Log.V(0).Info("restoring online backup", "target", target)
if err := r.restoreInitContainer(target); err != nil {
r.Log.Error(err, "unable to create restore initContainer", "target", target)
return ctrl.Result{}, err
newSessionState = formolv1alpha1.Failure
}
}
case formolv1alpha1.Finalize:
r.Log.V(0).Info("We are done with the restore. Run the finalize steps")
// Runs the finalize Steps functions in chroot env
if err := r.runFinalizeSteps(target); err != nil {
r.Log.Error(err, "unable to run finalize steps")
newSessionState = formolv1alpha1.Failure
} else {
r.Log.V(0).Info("Ran the finalize steps. Restore was a success")
newSessionState = formolv1alpha1.Success
}
}
if newSessionState != "" {
restoreTargetStatus.SessionState = newSessionState

View File

@ -3,6 +3,7 @@ package controllers
import (
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"os/exec"
"sigs.k8s.io/controller-runtime/pkg/client"
)
@ -54,3 +55,21 @@ func (r *RestoreSessionReconciler) restoreInitContainer(target formolv1alpha1.Ta
}
return nil
}
func (r *RestoreSessionReconciler) restoreJob(target formolv1alpha1.Target, targetStatus formolv1alpha1.TargetStatus) error {
cmd := exec.Command(RESTIC_EXEC, "restore", targetStatus.SnapshotId, "--target", "/")
// the restic restore command does not support JSON output
if output, err := cmd.CombinedOutput(); err != nil {
r.Log.Error(err, "unable to restore snapshot", "output", output)
return err
}
for _, container := range target.Containers {
for _, job := range container.Job {
if err := r.runFunction("restore-" + job.Name); err != nil {
r.Log.Error(err, "unable to run restore job")
return err
}
}
}
return nil
}

View File

@ -271,7 +271,6 @@ func (s Session) runTargetContainerChroot(runCmd string, args ...string) error {
}
func (s Session) runSteps(initializeSteps bool, target formolv1alpha1.Target) error {
s.Log.V(0).Info("start to run the backup steps it any")
// For every container listed in the target, run the initialization steps
for _, container := range target.Containers {
// Runs the steps one after the other
@ -282,12 +281,14 @@ func (s Session) runSteps(initializeSteps bool, target formolv1alpha1.Target) er
return s.runFunction(step.Name)
}
}
s.Log.V(0).Info("Done running steps")
return nil
}
// Run the initializing steps in the INITIALIZING state of the controller
// before actualy doing the backup in the RUNNING state
func (s Session) runFinalizeSteps(target formolv1alpha1.Target) error {
s.Log.V(0).Info("start to run the finalize steps it any")
return s.runSteps(false, target)
}
@ -295,5 +296,6 @@ func (s Session) runFinalizeSteps(target formolv1alpha1.Target) error {
// after the backup in the RUNNING state.
// The finalize happens whatever the result of the backup.
func (s Session) runInitializeSteps(target formolv1alpha1.Target) error {
s.Log.V(0).Info("start to run the initialize steps it any")
return s.runSteps(true, target)
}

2
formol

@ -1 +1 @@
Subproject commit b2d80d66ae6bca9e1bc5d04737998806296f8a93
Subproject commit b7747b635d0253d39829d64ae96472da09c64297