2020-08-18 11:57:53 +02:00
#!/bin/bash
2021-03-07 19:40:32 +01:00
{ { file_header | default ( ) } }
2021-01-05 17:41:18 +01:00
LOCKDIR = ${ HOME } /.cache # set lockdir
function exlock( ) { # define Function for setting lock; stops the script i a lock exists
2020-08-18 11:57:53 +02:00
exec { lock_fd} >${ LOCKDIR } /$( basename $0 ) .lock
flock -nx " $lock_fd "
if [ [ $? = = 1 ] ] ; then
exit 1
fi
}
2021-01-05 17:41:18 +01:00
function unlock( ) { # define function for removing lock
2020-08-18 11:57:53 +02:00
rm " ${ LOCKDIR } / $( basename $0 ) .lock "
[ [ -n $1 ] ] && exit $1
exit
}
2021-01-05 17:41:18 +01:00
exlock # set lock
abbruch_restic = 0 # set counter for error
mount -t cifs -o credentials = "/etc/restic/smb_password.txt" ,vers= 3.0 { { restic_repository } } { { restic_mount } } # mount share
mount_return_value = $? # schreib Exit Code in Variable
if ( [ " $mount_return_value " -ne 0 ] ) ; then
{
echo "--------------------------------------------------" # Trenner logfile
2021-01-10 16:10:24 +01:00
echo $( date +%d.%m.%Y-%T) # Datum für logfile
2021-01-05 17:41:18 +01:00
echo "mount error"
} >> /var/log/restic.log 2>& 1;
tail --lines= 5 "/var/log/restic.log" | mail -s " Backup-Error - restic - $HOSTNAME " { { empfaenger_mail } }
exit 1
else
{
echo "--------------------------------------------------" # Trenner logfile
2021-01-10 16:10:24 +01:00
echo $( date +%d.%m.%Y-%T) # Datum für logfile
2021-01-05 17:41:18 +01:00
echo "mount successful"
} >> /var/log/restic.log 2>& 1;
2020-12-18 22:07:03 +01:00
fi
2021-01-05 17:41:18 +01:00
2021-02-23 12:35:07 +01:00
while [ [ " $abbruch_restic " -le { { restic_anzahl_versuche_backup } } ] ] # Schleife für Abbruchbedingung; um die eckigen Klammern(Befehl "test") muss immer ein leerzeichen sein
2021-01-05 17:41:18 +01:00
do
{ # ist keine Subshell sondern Grouping; https://askubuntu.com/questions/662190/write-the-output-of-multiple-sequential-commands-to-a-text-file
echo "--------------------------------------------------" # Trenner logfile
2021-01-10 16:10:24 +01:00
echo $( date +%d.%m.%Y-%T) # Datum für logfile
2021-01-05 17:58:59 +01:00
restic -r { { restic_mount } } --password-file /etc/restic/password.txt backup --exclude-file /etc/restic/exclude.txt { { restic_folders_to_backup } } # execute Backup
2021-01-05 17:41:18 +01:00
restic_return_value = $? # schreib Exit Code in Variable
if ( [ [ " $restic_return_value " -eq 0 ] ] ) ; # Prüfung ob restic erfolgreich war(setze Abbruchbedingung), wenn nicht warte 1min und zähle die Abbruchbedingung hoch
then
abbruch_restic = 99
else
sleep { { restic_wartezeit } }
abbruch_restic = $(( " $abbruch_restic " + 1 ))
fi
2021-03-13 13:47:01 +01:00
echo $( date +%d.%m.%Y-%T) # Datum für logfile
2021-01-05 17:41:18 +01:00
} >> /var/log/restic.log 2>& 1; # leite die komplette Ausgabe in logfile um
done
umount { { restic_mount } } >> /var/log/restic.log 2>& 1; # unmount
if ( [ [ " $restic_return_value " -ne 0 ] ] ) ; then # sende eMail wenn Restic Fehler ungleich 0, also Fehler; #https://stackoverflow.com/a/45817972
tail --lines= 50 "/var/log/restic.log" | mail -s " Backup-Error - restic - $HOSTNAME " { { empfaenger_mail } } # schreibe die letzten 50 Zeilen aus dem Logfile in den Body der Mail
fi
unlock # entferne lock