From 28a5b7281a43c2f6427b032a10c970a602ba2e8b Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Wed, 9 Feb 2022 10:18:47 +0100
Subject: [PATCH] restic prune: add keep-last; add --cleanup-cache

---
 docs/30_Configuration/30_Filetransfer.md |  3 +++
 jobs/transfer.job.dist                   |  1 +
 plugins/transfer/restic.sh               | 25 ++++++++++++------------
 3 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/docs/30_Configuration/30_Filetransfer.md b/docs/30_Configuration/30_Filetransfer.md
index c0a0386..6145c8c 100644
--- a/docs/30_Configuration/30_Filetransfer.md
+++ b/docs/30_Configuration/30_Filetransfer.md
@@ -172,8 +172,11 @@ is a full backup and all following runs make an incremental backup.
 You have the possibility to keep all younger backups and have the possibility to delete
 older backups but to keep N weekly, monthly and yearly snapshots.
 
+The value for "keep-last" never deletes the last N most recent snapshots.
+
 ```text
 # prune 
+restic_keep-last = 5
 restic_keep-hourly = 100
 restic_keep-daily = 90
 restic_keep-weekly = 12
diff --git a/jobs/transfer.job.dist b/jobs/transfer.job.dist
index 06ee1b9..87071fb 100644
--- a/jobs/transfer.job.dist
+++ b/jobs/transfer.job.dist
@@ -147,6 +147,7 @@ restic_tag = imlbackup
 restic_mountpoint = /mnt/restore
 
 # prune 
+restic_keep-last = 5
 restic_keep-hourly = 100
 restic_keep-daily = 90
 restic_keep-weekly = 12
diff --git a/plugins/transfer/restic.sh b/plugins/transfer/restic.sh
index 004490f..5f12d28 100644
--- a/plugins/transfer/restic.sh
+++ b/plugins/transfer/restic.sh
@@ -160,23 +160,24 @@
 
         echo "--- PRUNE"
         local _tag=$( _j_getvar ${STORAGEFILE} "${CFGPREFIX}tag")
-
-        local _keep_h=$( _j_getvar ${STORAGEFILE} "${CFGPREFIX}keep-hourly")
-        local _keep_d=$( _j_getvar ${STORAGEFILE} "${CFGPREFIX}keep-daily")
-        local _keep_w=$( _j_getvar ${STORAGEFILE} "${CFGPREFIX}keep-weekly")
-        local _keep_m=$( _j_getvar ${STORAGEFILE} "${CFGPREFIX}keep-monthly")
-        local _keep_y=$( _j_getvar ${STORAGEFILE} "${CFGPREFIX}keep-yearly")
         
         local _mycmd="restic forget \
           ${ARGS_DEFAULT} \
           --tag $_tag \
-          --group-by "paths,tags" \
-          --keep-hourly $_keep_h \
-          --keep-daily $_keep_d \
-          --keep-weekly $_keep_w \
-          --keep-monthly $_keep_m \
-          --keep-yearly $_keep_y"
+          --group-by paths,tags \
+          --cleanup-cache"
+
+        local _keep
+        for mykeep in last hourly daily weekly monthly yearly
+        do
+            _keep=$( _j_getvar "${STORAGEFILE}" "${CFGPREFIX}keep-${mykeep}")
+            if [ -n "$_keep" ]; then
+                _mycmd="${_mycmd} --keep-${mykeep} ${_keep}"
+            fi
+        done
+
         echo $_mycmd
+        sleep 3
         color cmd 
         eval $_mycmd
         local _myrc=$?
-- 
GitLab