Skip to content

Instantly share code, notes, and snippets.

@wheel5up
Created February 19, 2024 22:09
Show Gist options
  • Save wheel5up/1ec2099bb4616d2ce159041694266579 to your computer and use it in GitHub Desktop.
Save wheel5up/1ec2099bb4616d2ce159041694266579 to your computer and use it in GitHub Desktop.
borg backup with zabbix sender
#!/bin/zsh
# Automated User Data Backup Script for BorgBackup
PATH=$PATH:/usr/local/bin
BORG=/usr/local/bin/borg
ZSENDER=/usr/local/bin/zabbix_sender
JQ=/usr/local/bin/jq
HOST=$(hostname -s)
# Send all output to Graylog
# https://tldp.org/LDP/abs/html/process-sub.html
# https://www.urbanautomaton.com/blog/2014/09/09/redirecting-bash-script-output-to-syslog/
exec 1> >(mlogger -n graylog.home -d -i -p local0.notice -t $(hostname)\ $(basename $0)) 2>&1
# Trap is from here https://www.howtogeek.com/821320/how-to-trap-errors-in-bash-scripts-on-linux/
# set statement is from here https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
# or here http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
trap 'ehandler $? $LINENO' ERR INT TERM
ehandler () {
echo "$( date ) Backup interrupted. Error: ($1) on $2" >&2;
exit 2
}
command -v $JQ >/dev/null 2>&1 || { echo >&2 "I require $JQ but it's not installed. Aborting."; exit 1; }
command -v $ZSENDER >/dev/null 2>&1 || { echo >&2 "I require $ZSENDER but it's not installed. Aborting."; exit 1; }
# Setup environment variables
# shellcheck source=vars.sh
source ~/.config/borg/vars.sh
# some helpers and error handling:
info() {
printf "\\n%s %s\\n\\n" "$( date )" "$*" >&2;
}
info "Starting backup"
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
# --filter AME \
# --stats \
${BORG} create \
--verbose \
--list \
--filter AME \
--show-rc \
--exclude-from exclusions \
--compression lz4 \
--exclude-caches \
\
::'{hostname}-{now}' \
${BORG_PATHS}
backup_exit=$?
#backup success with exit status of zero
if [ ${backup_exit} -eq 0 ]
then
${ZSENDER} -v -z ${ZABBIXHOST} -s ${HOST} -k ${ZABBIXSTATUS} -o 0
${BORG} info --last 1 --json | ${JQ} -c |${JQ} -R |xargs -0 printf "${HOST} ${ZABBIXITEM} %s" | ${ZSENDER} -v -z ${ZABBIXHOST} -i -
else
${ZSENDER} -v -z ${ZABBIXHOST} -s ${HOST} -k ${ZABBIXSTATUS} -o 1
fi
info "Pruning repository"
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
/usr/local/bin/borg prune \
--list \
--show-rc \
--keep-daily 30 \
--keep-weekly 8 \
--keep-monthly 12 \
prune_exit=$?
#prune_exit=0
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 1 ];
then
info "Backup and/or Prune finished with a warning"
fi
if [ ${global_exit} -gt 1 ];
then
info "Backup and/or Prune finished with an error"
fi
exit ${global_exit}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment