From 10438d1246703a54a9de4694dd3ced582afba3df Mon Sep 17 00:00:00 2001 From: mg Date: Wed, 23 Dec 2020 13:23:14 +0100 Subject: [PATCH] Restic Mail: bei Error Mail mit Log (#10) Styling Befehle nicht mehr in Subshell sondern gruppiert, Ausdgabe erfolgt in Datei und bei Fehler wird eine Mail mit dem Log gesendet Co-authored-by: Michael Grote <38253905+quotengrote@users.noreply.github.com> Reviewed-on: https://git.mgrote.net/mg/ansible/pulls/10 --- .../mgrote.restic/templates/restic_backup.sh | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/roles/mgrote.restic/templates/restic_backup.sh b/roles/mgrote.restic/templates/restic_backup.sh index f96a08b6..ac16bdf1 100644 --- a/roles/mgrote.restic/templates/restic_backup.sh +++ b/roles/mgrote.restic/templates/restic_backup.sh @@ -15,28 +15,20 @@ function unlock() { [[ -n $1 ]] && exit $1 exit } - # Damit prueft ob das Script ob es schon laeuft exlock -( +{ #<-- ist keine Subshell sondern Grouping, https://askubuntu.com/questions/662190/write-the-output-of-multiple-sequential-commands-to-a-text-file echo "--------------------------------------------------" echo $(date) - mount -t cifs -o credentials="/etc/restic/smb_password.txt",vers=3.0 {{ restic_repository }} {{ restic_mount }} - restic -r {{ restic_mount }} --password-file /etc/restic/password.txt backup --exclude-file /etc/restic/exclude.txt {{ restic_folders_to_backup }} -# Mail wenn Restix Exit != 0 #https://stackoverflow.com/a/45817972 -retVal=$? -if [ $retVal -ne 0 ]; then - echo "ERROR - Restic - $HOSTNAME" | mail -s "ERROR - Restic - $HOSTNAME" {{ empfaenger_mail }} -fi - - +retVal=$? #schreib Exit Code in Variable umount {{ restic_mount }} - echo "--------------------------------------------------" -) >> /var/log/restic.log 2>&1 - - - +} >> /var/log/restic.log 2>&1; +echo $retVal +# Mail wenn Restix Exit != 0 #https://stackoverflow.com/a/45817972 +if [ $retVal -ne 0 ]; then + tail "/var/log/restic.log" | mail -s "ERROR - Restic - $HOSTNAME" {{ empfaenger_mail }} +fi #Hiermit wird die Lockdatei geloescht unlock