ansible-role-server/templates/borgbackup.sh

69 lines
2.3 KiB
Bash

#!/bin/sh
# pass an arbitrary number of additional arguments to borg create by using
# borgbackup.sh $option1 $option2 ... $optionN
# pass -v --stats to show more information
# pass --list --filter AME to show all fiels Added Modified or with Error
export BORG_RSH='ssh -i {{ borgbackup_ssh_id }}'
export BORG_PASSPHRASE='{{ borgbackup_passphrase }}'
export BORG_REPO='{{ borgbackup_repo }}'
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
# Backup the most important directories
borg create \
$@ \
--compression lz4 \
--exclude-caches \
--exclude '/home/*/.cache/*' \
--exclude '/var/run' \
--exclude '/var/cache/*' \
--exclude '/var/tmp/*' \
--exclude '/var/lib/apt/*' \
--exclude '/var/lib/dpkg/*' \
--exclude '/var/lib/yum/*' \
--exclude '/var/lib/docker/overlay2' \
--exclude '/var/lib/docker/containers' \
--exclude '/var/lib/docker/image' \
--exclude '/var/lib/docker/tmp' \
--exclude '/var/lib/lxcfs' \
--exclude '/var/log/*' \
\
$BORG_REPO::'{{ borgbackup_hostname }}-{now:%Y%m%d_%H%M}' \
/etc \
/var \
/usr/lib \
/usr/local/etc \
/root \
/home \
backup_exit=$?
# Prune old backups: keep 7 daily, 3 weekly and 2 monthly (3 months total)
borg prune \
--prefix '{{ borgbackup_hostname }}-' \
--keep-daily 7 \
--keep-weekly 3 \
--keep-monthly 2
prune_exit=$?
# 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}