Skip to content

Instantly share code, notes, and snippets.

@anthonyclarka2
Created August 14, 2017 17:06
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyclarka2/cef41d201dd5b890dae6786010531aeb to your computer and use it in GitHub Desktop.
Save anthonyclarka2/cef41d201dd5b890dae6786010531aeb to your computer and use it in GitHub Desktop.
Borg backup script with exit code checking and status push to Zabbix
#!/bin/sh
# Enhanced backup script
# Sends messages to Zabbix based on exit code of borg executable
#
# anthonyclark AT G MAIL
# Shellcheck will still kvetch at you if notices are enabled
#
# This script will not work AS-IS but is only provided as such
# NO WARRANTIES
# This work is licensed under a Creative Commons Attribution 4.0 International License.
# https://creativecommons.org/licenses/by/4.0/
#
# 2017-08-10
####################
# SETUP
####################
ZABBIX_SERVER=monitor01.example.net
RUN_HOST="$(hostname)"
RUN_DATE="$(date +%Y-%m-%d-%R)"
SEND_CMD=/bin/zabbix_sender
BORG_CMD=/usr/local/bin/borg
REPOSITORY=borgbackup@backup01.example.net:/backup/${RUN_HOST}
export BORG_PASSPHRASE='REPLACEME'
####################
# BACKUP
####################
if [ -x ${BORG_CMD} ]
then
echo "Starting backup on ${RUN_DATE}"
${BORG_CMD} create -v --stats \
$REPOSITORY::${RUN_HOST}-${RUN_DATE} \
/root \
/home \
/etc \
/opt \
/var/www/html
else
echo "error with borg executable"
${SEND_CMD} -z ${ZABBIX_SERVER} -s ${RUN_HOST} -k system.backup.state -o 3
exit 1
fi
####################
# EXIT CODE CHECK
####################
case $? in
0)
# Success
echo "Backup completed successfully"
${SEND_CMD} -z ${ZABBIX_SERVER} -s ${RUN_HOST} -k system.backup.state -o 0
;;
1)
# Warning
echo "Backup completed with warnings"
${SEND_CMD} -z ${ZABBIX_SERVER} -s ${RUN_HOST} -k system.backup.state -o 1
;;
2)
# Failure
echo "Backup failed!"
${SEND_CMD} -z ${ZABBIX_SERVER} -s ${RUN_HOST} -k system.backup.state -o 2
exit 1
;;
*)
# Unknown status
echo "Backup status unknown"
${SEND_CMD} -z ${ZABBIX_SERVER} -s ${RUN_HOST} -k system.backup.state -o 3
;;
esac
####################
# POST BACKUP PRUNE
####################
echo "Starting backup pruning"
${BORG_CMD} prune -v $REPOSITORY --prefix ${RUN_HOST}- \
--keep-daily=7 --keep-weekly=4 --keep-monthly=6
@avishayp
Copy link

s/case could be replaced with a single ${SEND_CMD} -z ${ZABBIX_SERVER} -s ${RUN_HOST} -k system.backup.state -o $?
and put the condition down on post-backup prune instead. That way you get actual status instead of 3 masking different errors.

The backup prune should probably execute only on err 0 or 1

Just bikeshedding here...

@anthonyclarka2
Copy link
Author

Thank you for the feedback, I will look into it and improve the script accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment