ansible-role-server/templates/borgbackup.sh

68 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 /home/jannik/.ssh/id_rsa'
export BORG_RSH='ssh -i /home/jannik/.ssh/id_ed25519'
export BORG_PASSPHRASE='borgbackup.{{ borgbackup_host }}@hetznerbx'
export BORG_REPO='ssh://u182062-sub{{ borgbackup_sub }}@u182062.your-storagebox.de:23/./borg'
# 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_host }}-{now:%Y%m%d_%H%M}' \
/etc \
/var \
/root \
/home \
backup_exit=$?
# Prune old backups: keep 7 daily, 4 weekly and 3 monthly
borg prune \
--prefix '{{ borgbackup_host }}-' \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 3
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}