admin-scripts/system_backup.sh

98 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
#
# Script configuration
#
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
set -o allexport
source ${script_dir}/config/system_backup
set +o allexport
export BORG_REPO=$MOUNTPOINT/borg/backups
DATE=$(date -u +%Y-%m-%d-%H-%M)
#
# Create backups
#
# Options for borg create
BORG_OPTS="--stats --compression lz4 --one-file-system --checkpoint-interval 86400"
# No one can answer if Borg asks these questions, it is better to just fail quickly
# instead of hanging.
export BORG_RELOCATED_REPO_ACCESS_IS_OK=no
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=no
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
borg --version
info "Starting backup for $DATE"
borg create $BORG_OPTS \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
--exclude 'root/.cache' \
--exclude 'home/*/.cache/*' \
--exclude 'var/tmp/*' \
::'{hostname}-debian-{now}' \
/home /root /etc /var
backup_exit=$?
info "Pruning repository"
borg prune \
--list \
--glob-archives '{hostname}-debian-*' \
--show-rc \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6
prune_exit_1=$?
borg prune \
--list \
--glob-archives '{hostname}-trident-*' \
--show-rc \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6
prune_exit_2=$?
prune_exit=$(( prune_exit_2 > prune_exit_1 ? prune_exit_2 : prune_exit_1 ))
info "Compacting repository"
borg compact
compact_exit=$?
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup, Prune, and Compact finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Backup, Prune, and/or Compact finished with warnings"
else
info "Backup, Prune, and/or Compact finished with errors"
fi
exit ${global_exit}