backupsession housekeeping. delete the old backup and the corresponding restic snapshots

This commit is contained in:
Jean-Marc ANDRE 2023-03-25 21:23:11 +01:00
parent b91c767e82
commit 06b372765b
6 changed files with 52 additions and 6 deletions

View File

@ -56,6 +56,22 @@ var backupSessionCmd = &cobra.Command{
Short: "All the BackupSession related commands",
}
var snapshotCmd = &cobra.Command{
Use: "snapshot",
Short: "All the snapshot related commands",
}
var deleteSnapshotCmd = &cobra.Command{
Use: "delete",
Short: "Delete a snapshot",
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
namespace, _ := cmd.Flags().GetString("namespace")
snapshotId, _ := cmd.Flags().GetString("snapshot-id")
standalone.DeleteSnapshot(namespace, name, snapshotId)
},
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "formolcli",
@ -83,8 +99,10 @@ func Execute() {
func init() {
rootCmd.AddCommand(backupSessionCmd)
rootCmd.AddCommand(restoreSessionCmd)
rootCmd.AddCommand(snapshotCmd)
backupSessionCmd.AddCommand(createBackupSessionCmd)
restoreSessionCmd.AddCommand(startRestoreSessionCmd)
snapshotCmd.AddCommand(deleteSnapshotCmd)
rootCmd.AddCommand(startServerCmd)
createBackupSessionCmd.Flags().String("namespace", "", "The namespace of the BackupConfiguration containing the information about the backup.")
createBackupSessionCmd.Flags().String("name", "", "The name of the BackupConfiguration containing the information about the backup.")
@ -96,4 +114,10 @@ func init() {
startRestoreSessionCmd.MarkFlagRequired("namespace")
startRestoreSessionCmd.MarkFlagRequired("name")
startRestoreSessionCmd.MarkFlagRequired("target-name")
deleteSnapshotCmd.Flags().String("snapshot-id", "", "The snapshot id to delete")
deleteSnapshotCmd.Flags().String("namespace", "", "The namespace of the BackupConfiguration containing the information about the backup.")
deleteSnapshotCmd.Flags().String("name", "", "The name of the BackupConfiguration containing the information about the backup.")
deleteSnapshotCmd.MarkFlagRequired("snapshot-id")
deleteSnapshotCmd.MarkFlagRequired("namespace")
deleteSnapshotCmd.MarkFlagRequired("name")
}

View File

@ -67,7 +67,7 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
// Do preliminary checks with the repository
if err = r.setResticEnv(backupConf); err != nil {
if err = r.SetResticEnv(backupConf); err != nil {
r.Log.Error(err, "unable to set restic env")
return ctrl.Result{}, err
}

View File

@ -68,7 +68,7 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
// Do preliminary checks with the repository
if err = r.setResticEnv(backupConf); err != nil {
if err = r.SetResticEnv(backupConf); err != nil {
r.Log.Error(err, "unable to set restic env")
return ctrl.Result{}, err
}

View File

@ -69,7 +69,7 @@ func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration) (en
return
}
func (s Session) setResticEnv(backupConf formolv1alpha1.BackupConfiguration) error {
func (s Session) SetResticEnv(backupConf formolv1alpha1.BackupConfiguration) error {
envs, err := s.getResticEnv(backupConf)
for _, env := range envs {
os.Setenv(env.Name, env.Value)
@ -95,9 +95,8 @@ func (s Session) CheckRepo() error {
func (s Session) getSecretData(name string) map[string][]byte {
secret := corev1.Secret{}
namespace := os.Getenv(formolv1alpha1.POD_NAMESPACE)
if err := s.Get(s.Context, client.ObjectKey{
Namespace: namespace,
Namespace: s.Namespace,
Name: name,
}, &secret); err != nil {
s.Log.Error(err, "unable to get Secret", "Secret", name)

2
formol

@ -1 +1 @@
Subproject commit e73ef7c3f24ee612421b62963f443ff1a1e790dc
Subproject commit f890962221bf05fb54c9d46d839957a59d644cc8

View File

@ -141,3 +141,26 @@ func CreateBackupSession(ref corev1.ObjectReference) {
os.Exit(1)
}
}
func DeleteSnapshot(namespace string, name string, snapshotId string) {
log := session.Log.WithName("DeleteSnapshot")
session.Namespace = namespace
backupConf := formolv1alpha1.BackupConfiguration{}
if err := session.Get(session.Context, client.ObjectKey{
Namespace: namespace,
Name: name,
}, &backupConf); err != nil {
log.Error(err, "unable to get the BackupConf")
return
}
if err := session.SetResticEnv(backupConf); err != nil {
log.Error(err, "unable to set the restic env")
return
}
log.V(0).Info("deleting restic snapshot", "snapshotId", snapshotId)
cmd := exec.Command(controllers.RESTIC_EXEC, "forget", "--prune", snapshotId)
_, err := cmd.CombinedOutput()
if err != nil {
log.Error(err, "unable to delete snapshot", "snapshoId", snapshotId)
}
}