From 199a39f05b09d49377c3ed19327294be64edd089 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Thu, 10 Feb 2022 15:33:27 +0100
Subject: [PATCH] restic: support more keep policies

---
 docs/30_Configuration/30_Filetransfer.md      | 33 +++++++++++++++----
 docs/30_Configuration/50_File_transfer.job.md | 23 ++++++++++---
 jobs/transfer.job.dist                        | 13 ++++----
 plugins/transfer/restic.sh                    |  6 ++--
 4 files changed, 55 insertions(+), 20 deletions(-)

diff --git a/docs/30_Configuration/30_Filetransfer.md b/docs/30_Configuration/30_Filetransfer.md
index 6145c8c..de1f6cb 100644
--- a/docs/30_Configuration/30_Filetransfer.md
+++ b/docs/30_Configuration/30_Filetransfer.md
@@ -169,17 +169,36 @@ s, m, h, D, W, M, or Y (indicating seconds, minutes, hours, days, weeks, months,
 Restic works like a repository and uses deduplication. The first run of a file backup
 is a full backup and all following runs make an incremental backup.
 
+There are 2 simple cleanup variants for restic forget:
+
+* The value for "keep-last" never deletes the last N most recent snapshots.
+* "restic_keep-within" keeps all created snapshots within a given period (eg. 1y5m7d2h) 
+
+If you need more control and want less snapshots as older they are:
+
 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.
+Multiple values are allowed and work like an OR condition.
+See the docs for more details to the keep values of the forget command <https://restic.readthedocs.io/en/latest/060_forget.html>
 
 ```text
 # prune 
-restic_keep-last = 5
-restic_keep-hourly = 100
-restic_keep-daily = 90
-restic_keep-weekly = 12
-restic_keep-monthly = 12
-restic_keep-yearly = 10
+restic_keep-within = 6m
+# restic_keep-last = 5
+# restic_keep-hourly = 10
+# restic_keep-daily = 30
+# restic_keep-weekly = 12
+# restic_keep-monthly = 12
+# restic_keep-yearly = 10
 ```
+
+Supported values are:
+
+* restic_keep-last [N] - keep the last n snapshots
+* restic_keep-(hourly|daily|weekly|monthly|yearly) [N] - keep then last n snapshots of a period
+* restic_keep-within [duration] - keep snapshots that are newer than duration (eg. 1y5m7d2h) relative to the latest snapshot
+* restic_keep-within-(hourly|daily|weekly|monthly|yearly) [duration] - keep snapshots of given period that are newer than duration (eg. 1y5m7d2h) relative to the latest snapshot
+* restic_keep-tag [taglist] - keep snapshots with this taglist (can be specified only one in imlbackup) (default [])
+
+`restic_keep-within = 6m`
\ No newline at end of file
diff --git a/docs/30_Configuration/50_File_transfer.job.md b/docs/30_Configuration/50_File_transfer.job.md
index 9ee46da..42cd1d4 100644
--- a/docs/30_Configuration/50_File_transfer.job.md
+++ b/docs/30_Configuration/50_File_transfer.job.md
@@ -255,13 +255,26 @@ restic_tag = imlbackup
 restic_mountpoint = /mnt/restore
 
 # prune 
-restic_keep-hourly = 100
-restic_keep-daily = 90
-restic_keep-weekly = 12
-restic_keep-monthly = 12
-restic_keep-yearly = 10
+restic_keep-within = 6m
+# restic_keep-last = 5
+# restic_keep-hourly = 10
+# restic_keep-daily = 30
+# restic_keep-weekly = 12
+# restic_keep-monthly = 12
+# restic_keep-yearly = 10
 ```
 
+For pruning with restic forget the following values can be used. 
+Multiple values are allowed and work like an OR condition.
+
+See the docs for more details to the keep values of the forget command <https://restic.readthedocs.io/en/latest/060_forget.html>
+
+* restic_keep-last [N] - keep the last n snapshots
+* restic_keep-(hourly|daily|weekly|monthly|yearly) [N] - keep then last n snapshots of a period
+* restic_keep-within [duration] - keep snapshots that are newer than duration (eg. 1y5m7d2h) relative to the latest snapshot
+* restic_keep-within-(hourly|daily|weekly|monthly|yearly) [duration] - keep snapshots of given period that are newer than duration (eg. 1y5m7d2h) relative to the latest snapshot
+* restic_keep-tag [taglist] - keep snapshots with this taglist (can be specified only one in imlbackup) (default [])
+
 ## restore-path
 
 Default path where to put restored file when running *restore.sh*.
diff --git a/jobs/transfer.job.dist b/jobs/transfer.job.dist
index 87071fb..c4d6465 100644
--- a/jobs/transfer.job.dist
+++ b/jobs/transfer.job.dist
@@ -147,11 +147,12 @@ restic_tag = imlbackup
 restic_mountpoint = /mnt/restore
 
 # prune 
-restic_keep-last = 5
-restic_keep-hourly = 100
-restic_keep-daily = 90
-restic_keep-weekly = 12
-restic_keep-monthly = 12
-restic_keep-yearly = 10
+restic_keep-within = 6m
+# restic_keep-last = 5
+# restic_keep-hourly = 10
+# restic_keep-daily = 30
+# restic_keep-weekly = 12
+# restic_keep-monthly = 12
+# restic_keep-yearly = 10
 
 # ----------------------------------------------------------------------
diff --git a/plugins/transfer/restic.sh b/plugins/transfer/restic.sh
index 3496150..564e817 100644
--- a/plugins/transfer/restic.sh
+++ b/plugins/transfer/restic.sh
@@ -9,7 +9,8 @@
 # ah - Axel Hahn <axel.hahn@iml.unibe.ch>
 # 2021-05-19  ah    v0.1   INIT ... WIP
 # 2022-01-06  ah    v0.2   added support for Repository with REST and authentication 
-# 2022-02-09  ah    v0.3   show difff to last backup; update pruning
+# 2022-02-09  ah    v0.3   show diff to last backup; update pruning
+# 2022-02-09  ah    v0.3   update pruning; more keep-params
 # ================================================================================
 
 # --------------------------------------------------------------------------------
@@ -153,10 +154,11 @@
           ${ARGS_DEFAULT} \
           --tag $_tag \
           --group-by paths,tags \
+          --prune \
           --cleanup-cache"
 
         local _keep
-        for mykeep in last hourly daily weekly monthly yearly
+        for mykeep in last hourly daily weekly monthly yearly within within-hourly within-daily within-weekly within-monthly within-yearly tag
         do
             _keep=$( _j_getvar "${STORAGEFILE}" "${CFGPREFIX}keep-${mykeep}")
             if [ -n "$_keep" ]; then
-- 
GitLab