From d0009ce4511647d0226d762c17f82a6a47847d94 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Mon, 7 Mar 2022 16:17:08 +0100
Subject: [PATCH] update restic plugin: verify; detect existing repo

---
 backup.sh                     |  6 ++-
 check_clientbackup.sh         |  8 +++-
 plugins/transfer/duplicity.sh | 15 +++++++-
 plugins/transfer/restic.sh    | 72 +++++++++++++++++++++++++++--------
 transfer.sh                   | 11 +-----
 5 files changed, 81 insertions(+), 31 deletions(-)

diff --git a/backup.sh b/backup.sh
index 75fed16..da91e91 100755
--- a/backup.sh
+++ b/backup.sh
@@ -17,7 +17,7 @@
 # ================================================================================
 
 . $( dirname "$0" )/jobhelper.sh
-
+. `dirname $0`/inc_bash.sh
 
   typeset -i rcBackup=0
   typeset -i rcTransfer=0
@@ -224,12 +224,16 @@ EOFbackupinfo
     echo
     echo "INFO: $(date) - compress older logs"
     echo find "${DIR_LOGS}" -name "*.log" -mtime +2 -print -exec gzip {} \;
+    color cmd 
     find "${DIR_LOGS}" -name "*.log" -mtime +2 -print -exec gzip {} \;
+    color reset
     echo
 
     echo "INFO: $(date) - cleanup logs"
     echo find "${DIR_LOGS}" -mtime +28 -delete -print
+    color cmd
     find "${DIR_LOGS}" -mtime +28 -delete -print
+    color reset
     echo
 
     # ------------------------------------------------------------
diff --git a/check_clientbackup.sh b/check_clientbackup.sh
index 82a3206..b481c54 100755
--- a/check_clientbackup.sh
+++ b/check_clientbackup.sh
@@ -111,8 +111,12 @@ else
     # grep -E "^(${sSearch})" "$logfile"
 
     echo
-    echo ">>> Summary of backed up directories:"
-    cat $logfile | grep "DIR\ "
+    # echo ">>> Summary of backed up directories:"
+    # cat $logfile | grep "DIR\ "
+
+    echo ">>> Summary of backup actions:"
+    cat $logfile | grep "__[A-Z][A-Z]*__"
+
 
     sShort="$sShort - OK: $iOK ... Errors: $iError"
 
diff --git a/plugins/transfer/duplicity.sh b/plugins/transfer/duplicity.sh
index 523eb29..2840527 100644
--- a/plugins/transfer/duplicity.sh
+++ b/plugins/transfer/duplicity.sh
@@ -287,7 +287,9 @@
 # --------------------------------------------------------------------------------
 
     # init repository
+    # param  integer  exitcode of command
     function t_rcCheckInit(){
+        echo -n "__REPO__ "
         case $1 in
             0) color ok;      echo "OK - the repository was created." ;;
             *) color error;   echo "Verify output above - returncode of init was $1" ;;
@@ -295,24 +297,31 @@
         color reset
     }
     # backup files
+    # param  integer  exitcode of command
+    # param  string   directory that was backed up
     function t_rcCheckBackup(){
+        echo -n "__BACKUP__ "
         case $1 in
-            0) color ok;      echo "OK" ;;
+            0) color ok;      echo "OK $1" ;;
             23) color error
+                echo "FAILED - DIR ${2}"
                 echo A lock file was found. Maybe this server was rebooted while performing a backup.
                 echo If so delete the file lockfile.lock named in the output and start $0 again.
                 ;;
             31) color error
+                echo "FAILED - DIR ${2}"
                 echo Maybe you it is a problem with the gpg-agent.conf
                 ls -l ~/.gnupg/gpg-agent.conf && cat ~/.gnupg/gpg-agent.conf
                 ;;
-            *) color error;   echo "Backup error - returncode was $1" ;;
+            *) color error;   echo "FAILED - DIR ${2} - Backup error - returncode was $1" ;;
         esac
         color reset
     }
 
     # repoitory cleanup
+    # param  integer  exitcode of command
     function t_rcCheckCleanup(){
+        echo -n "__PRUNE__ "
         case $1 in
             0) color ok;      echo "OK" ;;
             *) color error;   echo "Cleanup error - returncode was $1" ;;
@@ -321,7 +330,9 @@
     }
 
     # restore files
+    # param  integer  exitcode of command
     function t_rcCheckRestore(){
+        echo -n "__RESTORE__ "
         case $1 in
             0) color ok;      echo "OK" ;;
             *) color error;   echo "Restore error - returncode was $1" ;;
diff --git a/plugins/transfer/restic.sh b/plugins/transfer/restic.sh
index 564e817..5d11fb6 100644
--- a/plugins/transfer/restic.sh
+++ b/plugins/transfer/restic.sh
@@ -11,6 +11,7 @@
 # 2022-01-06  ah    v0.2   added support for Repository with REST and authentication 
 # 2022-02-09  ah    v0.3   show diff to last backup; update pruning
 # 2022-02-09  ah    v0.3   update pruning; more keep-params
+# 2022-03-07  ah    v0.4   add verify in post task
 # ================================================================================
 
 # --------------------------------------------------------------------------------
@@ -123,16 +124,21 @@
     # function t_backupDoPreTasks(){
     function t_backupDoPreTasks(){
 
-        local _mycmd="restic init ${ARGS_DEFAULT}"
-        echo $_mycmd
-        color cmd 
-        eval $_mycmd
-        local _myrc=$?
-        color reset
-
-        # detect return code ... do not abort on any error.
-        t_rcCheckInit $_myrc
+        if eval restic snapshots ${ARGS_DEFAULT} >/dev/null 2>&1
+        then
+            echo "__REPO__ OK, Backup repository already exists."
+        else
+            echo "Backup repository needs to be created."
+            local _mycmd="restic init ${ARGS_DEFAULT}"
+            echo $_mycmd
+            color cmd 
+            eval $_mycmd
+            local _myrc=$?
+            color reset
 
+            # detect return code ... and abort on any error.
+            t_rcCheckInit $_myrc
+        fi
     }
 
 
@@ -175,6 +181,21 @@
 
         t_rcCheckCleanup $_myrc
         echo
+
+        echo "--- VERIFY"
+        # param --read-data takes a long time. Maybe use an extra job with it.
+        # _mycmd="time restic check ${ARGS_DEFAULT} --with-cache --read-data"
+        _mycmd="restic check ${ARGS_DEFAULT} --with-cache"
+        echo $_mycmd
+        sleep 3
+        color cmd 
+        eval $_mycmd
+        local _myrc=$?
+        color reset
+
+        t_rcCheckVerify $_myrc
+        echo
+
     }
 
 # --------------------------------------------------------------------------------
@@ -318,35 +339,54 @@
 # --------------------------------------------------------------------------------
 
     # init repository
+    # param  integer  exitcode of command
     function t_rcCheckInit(){
+        echo -n "__REPO__ "
         case $1 in
             0) color ok;      echo "OK - the repository was created." ;;
-            1) color warning; echo "You can ignore the error if the repository was initialized already." ;;
-            *) color error;   echo "Verify output above - returncode of restic init was $1" ;;
+            1) color warning; echo "FAILED - You can ignore the error if the repository was initialized already." ;;
+            *) color error;   echo "FAILED - Verify output above - returncode of restic init was $1" ;;
         esac
         color reset
     }
+
     # backup files
+    # param  integer  exitcode of command
+    # param  string   directory that was backed up
     function t_rcCheckBackup(){
+        echo -n "__BACKUP__ "
         case $1 in
-            0) color ok;      echo "OK" ;;
-            1) color error;   echo "Unable to connect with restic repository." ;;
-            *) color error;   echo "Backup error - returncode was $1" ;;
+            0) color ok;      echo "OK - DIR ${2}" ;;
+            1) color error;   echo "FAILED - DIR ${2} - Unable to connect with restic repository." ;;
+            *) color error;   echo "FAILED - DIR ${2} - Backup error - returncode was $1" ;;
         esac
         color reset
     }
 
     # repository cleanup
+    # param  integer  exitcode of command
     function t_rcCheckCleanup(){
+        echo -n "__PRUNE__ "
         case $1 in
             0) color ok;      echo "OK" ;;
-            *) color error;   echo "Cleanup error - returncode was $1" ;;
+            *) color error;   echo "FAILED ${2} - Cleanup error - returncode was $1" ;;
+        esac
+        color reset
+    }
+    # verify backup data
+    # param  integer  exitcode of command
+    function t_rcCheckVerify(){
+        echo -n "__VERIFY__ "
+        case $1 in
+            0) color ok;      echo "OK" ;;
+            *) color error;   echo "FAILED - returncode was $1" ;;
         esac
         color reset
     }
-
     # restore files
+    # param  integer  exitcode of command
     function t_rcCheckRestore(){
+        echo -n "__RESTORE__ "
         case $1 in
             0) color ok;      echo "OK" ;;
             *) color error;   echo "Restore error - returncode was $1" ;;
diff --git a/transfer.sh b/transfer.sh
index cd4fdf0..216162e 100755
--- a/transfer.sh
+++ b/transfer.sh
@@ -312,17 +312,8 @@
           color reset
           echo
 
-          t_rcCheckBackup $myrc
+          t_rcCheckBackup $myrc "${BACKUP_DIR}"
 
-          if [ $myrc -ne 0 ]; then
-            color error
-            echo DIR ERROR ${BACKUP_DIR} rc=$myrc during file transfer
-
-          else
-            color ok
-            echo DIR OK ${BACKUP_DIR} was successful.
-          fi
-          color reset
         fi
         echo
 
-- 
GitLab