From f43c85662d06aaba871d4007186f4f08f2e5aa71 Mon Sep 17 00:00:00 2001
From: Jerome Charaoui <jerome@riseup.net>
Date: Fri, 26 Jan 2018 11:26:37 -0500
Subject: [PATCH] [borg] replace "keep*" options with simpler "keep"

The effect of "keep*" options is not straightforward to understand, so
replacing it with a simpler "keep" option, which replicates the
functionality of other backupninja handlers. This also simplifies the
helper, as the use is then only asked how many days of backups to keep.

At the same time, we add "prune_options" which allows for the use of the
"keep*" options as well as other useful prune options, like "--prefix".
---
 examples/example.borg   | 37 +++++++++++++++++++++++++++----------
 handlers/borg.helper.in | 22 +++++-----------------
 handlers/borg.in        | 15 ++++++++-------
 3 files changed, 40 insertions(+), 34 deletions(-)

diff --git a/examples/example.borg b/examples/example.borg
index 8644cbc..e5285f4 100644
--- a/examples/example.borg
+++ b/examples/example.borg
@@ -41,16 +41,6 @@
 ## Default:
 # init = yes
 
-## how many hours, days, weeks and months of data to keep
-##
-## for more info see : borg help prune
-##
-## Default:
-# keephourly = 1
-# keepdaily = 7
-# keepweekly = 4
-# keepmonthly = -1
-
 ## A few notes about includes and excludes:
 ## 1. include paths do not support any kind of pattern matching
 ## 2. exclude paths support several types of pattern matching, the default being
@@ -90,6 +80,33 @@ exclude = /var/lib/mysql
 ## Default:
 # excludenodump = no
 
+## whether to prune (remove) older backups
+##
+## Default:
+# prune = yes
+
+## keep all backups within this time frame.
+## must be defined as a number followed by one of the
+## following characters: "H", "d", "w", "m", "y"
+##
+## this option will be ignored if set to 0
+##
+## the default is to keep all backups made within the
+## last 30 days
+##
+## Default:
+# keep = 30d
+
+## define extra command-line options for the "borg prune" operation.
+##
+## Example:
+## prune_options = --keep-daily=7 --keep-weekly=4 --keep-monthly=6
+##
+## for more info see : borg help prune
+##
+## Default:
+# prune_options =
+
 ######################################################
 ## destination section
 ## (where the files are copied to)
diff --git a/handlers/borg.helper.in b/handlers/borg.helper.in
index a4cd89e..d042198 100644
--- a/handlers/borg.helper.in
+++ b/handlers/borg.helper.in
@@ -270,19 +270,13 @@ do_borg_prune() {
       declare -a tmp_array
       set -o noglob
       REPLY=
-      formBegin "$borg_title - pruning (how many backups to keep)"
-         formItem "hourly" "$borg_keephourly"
-         formItem "daily" "$borg_keepdaily"
-         formItem "weekly" "$borg_keepweekly"
-         formItem "monthly" "$borg_keepmonthly"
+      formBegin "$borg_title - keep all backups made within this number of days"
+         formItem "keep" "$borg_keep"
          formDisplay
 
       [ $? = 0 ] || return
       tmp_array=($REPLY)
-      borg_keephourly=${tmp_array[0]}
-      borg_keepdaily=${tmp_array[1]}
-      borg_keepweekly=${tmp_array[2]}
-      borg_keepmonthly=${tmp_array[3]}
+      borg_keep=${tmp_array[0]}
 
       set +o noglob
    fi
@@ -317,10 +311,7 @@ EOF
 
 ## for more info see : borg prune -h
 prune = $borg_prune
-keephourly = $borg_keephourly
-keepdaily = $borg_keepdaily
-keepweekly = $borg_keepweekly
-keepmonthly = $borg_keepmonthly
+keep = "${borg_keep}d"
 
 [dest]
 directory = $borg_directory
@@ -386,10 +377,7 @@ borg_wizard() {
    borg_compression=lz4
    borg_encryption=none
    borg_passphrase=
-   borg_keephourly=1
-   borg_keepdaily=7
-   borg_keepweekly=4
-   borg_keepmonthly=-1
+   borg_keep=30
 
    # Global variables whose '*' shall not be expanded
    set -o noglob
diff --git a/handlers/borg.in b/handlers/borg.in
index 698ba6d..0ca54c7 100644
--- a/handlers/borg.in
+++ b/handlers/borg.in
@@ -24,15 +24,13 @@ getconf bwlimit
 
 setsection source
 getconf init yes
-getconf prune yes
-getconf keephourly 1
-getconf keepdaily 7
-getconf keepweekly 4
-getconf keepmonthly -1
 getconf include
 getconf exclude
 getconf excludecaches no
 getconf excludenodump no
+getconf prune yes
+getconf keep 30d
+getconf prune_options
 
 setsection dest
 getconf user
@@ -153,8 +151,11 @@ fi
 
 # borg prune
 if [ "$prune" == "yes" ]; then
-  prunestr="borg prune --keep-hourly $keephourly --keep-daily $keepdaily --keep-weekly $keepweekly --keep-monthly $keepmonthly $execstr_repository"
-  debug "$prunestr"
+   if [ ! -z $keep ] && [ ! "$keep" == "0" ]; then
+      prune_options="${prune_options} --keep-within=${keep}"
+   fi
+   prunestr="borg prune $prune_options $execstr_repository"
+   debug "$prunestr"
    if [ $test = 0 ]; then
       output="`su -c "$prunestr" 2>&1`"
       if [ $? = 0 ]; then
-- 
GitLab