Created
July 20, 2015 13:19
-
-
Save TimKraemer/195483d45951a17c7cc5 to your computer and use it in GitHub Desktop.
backintime user-callback backup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# this file is called by backintime to handle it's current states | |
# see: http://backintime.le-web.org/documentation/usercallback/ | |
MOUNT_PATH="/media/backup" | |
LOG_FILE="/var/log/backintime/backintime.log" | |
EMAIL_ADDRESS="backup@tim-kraemer.de" | |
LAST_READ_MARKER_PATH="/root/backup_timestamps/marker" | |
NAME_INDICATOR_PATH=${MOUNT_PATH}"/name_backup" | |
LOG_WARNING_FILE="/var/log/backintime/last-mounted-warning" | |
NUMBER_OF_BACKUP_VOLUMES=2 | |
DAYS_UNTIL_WARNING=7 | |
# State 1: Backup process begins. | |
case "$3" in | |
"1") | |
# before the backup starts, mount the backup volume | |
mount -t ext4 -o user /dev/sdb1 ${MOUNT_PATH} #|| mount -t ext4 -o user /dev/sdc1 ${MOUNT_PATH} | |
;; | |
# State 2: Backup process ends. | |
"2") | |
# prüfen wann die Festplatten das letzte Mal getauscht wurden: | |
# es existieren LAST_READ_MARKER mit dem Namen der externen Festplatten | |
# es existiert jeweils ein NAME_INDICATOR auf jeder festplatte | |
# im Rahmen des Chronjobs wird der NAME_INDICATOR der aktuell gemounteten Festplatte gelesen | |
# der entsprechende marker wird mit dem aktuellem Datum aktualisiert | |
# das jeweilige Datum beider marker wird ausgelesen, ist es älter als DAYS_UNTIL_WARNING Tage wird eine Warnungsmail rausgeschickt | |
touch ${LAST_READ_MARKER_PATH}${id} | |
name=$(find ${NAME_INDICATOR_PATH}*) | |
id=${name:$((${#name}-1)):1} | |
for i in $(seq 1 ${NUMBER_OF_BACKUP_VOLUMES}) | |
do | |
days_since_last_seen=$(( ($(date +%s) - $(date --date=@$(stat --format %X ${LAST_READ_MARKER_PATH}${i}) +%s) )/(60*60*24) )) | |
if [ "${days_since_last_seen}" -gt "${DAYS_UNTIL_WARNING}" ] | |
then | |
echo "Die externe Festplatte #"${i}" wurde seit "${days_since_last_seen}" Tagen nicht mehr eingewechselt!" >${LOG_WARNING_FILE} | |
/usr/bin/mail -s "[HAL-Server] Backup-Medium sollte getauscht werden" "$EMAIL_ADDRESS" < ${LOG_WARNING_FILE} | |
fi | |
done | |
# unmount the backup volume | |
umount ${MOUNT_PATH} | |
;; | |
# State 3: A new snapshot was taken. | |
"3") | |
# send an email notification on successfull backups | |
name=$(find ${NAME_INDICATOR_PATH}*) | |
id=${name:$((${#name}-1)):1} | |
echo "Auf Backupmedium #"${id}" wurde ein Snapshot unter "$5" angelegt." | /usr/bin/mail -s "[HAL-Server] Backup erfolgreich" "$EMAIL_ADDRESS" | |
;; | |
# State 4: There was an error. | |
"4") | |
echo $(date) > ${LOG_FILE} | |
echo "================================================================" >>${LOG_FILE} | |
case "$4" in | |
1) echo "The application is not configured" >>${LOG_FILE} | |
;; | |
2) echo "A 'take snapshot' process is already running." >>${LOG_FILE} | |
echo "Vermutlich ist das Backup vom Vortrag noch nicht fertig." >>${LOG_FILE} | |
;; | |
3) echo "Can’t find snapshots directory." >>${LOG_FILE} | |
echo "Die externe USB Festplatte wird vermutlich nicht erkannt!" >>${LOG_FILE} | |
;; | |
4) echo "A snapshot for “now” already exist." >>${LOG_FILE} | |
echo "Es existiert bereits ein Backup von heute" >>${LOG_FILE} | |
;; | |
esac | |
/usr/bin/mail -s "[HAL-Server] Backup fehlgeschlagen" "$EMAIL_ADDRESS" < ${LOG_FILE} | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment