From 83628bee9e9db5674a40a8fc85ef8c562dfa48ca Mon Sep 17 00:00:00 2001
From: Alex Iribarren <Alex.Iribarren@cern.ch>
Date: Tue, 14 Jun 2022 17:01:57 +0200
Subject: [PATCH] Remove stale locks first, better logging

---
 stream8_backups/backup.sh | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/stream8_backups/backup.sh b/stream8_backups/backup.sh
index d6aa7b8..abda4b7 100755
--- a/stream8_backups/backup.sh
+++ b/stream8_backups/backup.sh
@@ -7,21 +7,28 @@ source common.sh
 # Let's just backup $TODAY, ignoring symlink snaps (zero updates) as well
 EXCLUDE_LIST=`mktemp`
 RESTIC_LOGFILE=`mktemp`
+
+$RESTIC snapshots
+if [[ $? -ne 0 ]]; then
+  echo "Lock detected, attempting to unlock" | tee -a $RESTIC_LOGFILE
+  $RESTIC unlock 2>&1 | tee -a $RESTIC_LOGFILE
+fi
+
 find $SOURCE -mindepth 1 -maxdepth 1 \( -type l -o -type d \) ! -path "*$TODAY" > $EXCLUDE_LIST
-$RESTIC backup --tag $TODAY --exclude-file=$EXCLUDE_LIST $SOURCE &>> $RESTIC_LOGFILE
+echo "Performing backup..."
+$RESTIC backup --tag $TODAY --exclude-file=$EXCLUDE_LIST $SOURCE 2>&1 | tee -a $RESTIC_LOGFILE
+echo "Backup concluded with exit code $?"
 rm -f $EXCLUDE_LIST
 
 # Check if there are any snapshots to forget/purge
+echo "Checking for old snapshots..."
 SNAPS_TO_REMOVE=`$RESTIC forget --dry-run --group-by paths --keep-within $PRUNE_SNAPSHOTS_OLDER_THAN | grep "remove .* snapshots" | awk '{print $2}'`
 if [ ! -z $SNAPS_TO_REMOVE ]; then
-  echo "Found $SNAPS_TO_REMOVE snapshots that are older than $PRUNE_SNAPSHOTS_OLDER_THAN. Purging from restic store ..." >> $RESTIC_LOGFILE
-  $RESTIC unlock &>> $RESTIC_LOGFILE
-  $RESTIC forget --group-by paths --keep-within $PRUNE_SNAPSHOTS_OLDER_THAN --prune &>> $RESTIC_LOGFILE
+  echo "Found $SNAPS_TO_REMOVE snapshots that are older than $PRUNE_SNAPSHOTS_OLDER_THAN. Purging from restic store ..." 2>&1 | tee -a $RESTIC_LOGFILE
+  $RESTIC unlock 2>&1 | tee -a $RESTIC_LOGFILE
+  $RESTIC forget --group-by paths --keep-within $PRUNE_SNAPSHOTS_OLDER_THAN --prune 2>&1 | tee -a $RESTIC_LOGFILE
 fi
 
-# Just in case the log is too big and the mail doesn't make it...
-cat $RESTIC_LOGFILE
-
 echo "Sending email of $RESTIC_LOGFILE to admins"
 export TODAY="$TODAY"
 
-- 
GitLab