From 10ed97bee856daee44c45e3f58418789dca4bc82 Mon Sep 17 00:00:00 2001 From: Robin Meier Date: Wed, 24 Apr 2024 23:35:02 +0200 Subject: [PATCH] Add borg system backup script --- config/system_backup.EXAMPLE | 3 ++ system_backup.sh | 97 ++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 config/system_backup.EXAMPLE create mode 100755 system_backup.sh diff --git a/config/system_backup.EXAMPLE b/config/system_backup.EXAMPLE new file mode 100644 index 0000000..a747ade --- /dev/null +++ b/config/system_backup.EXAMPLE @@ -0,0 +1,3 @@ +BORG_PASSPHRASE="some.random.strong.password.here" +MOUNTPOINT=/mnt/backup_disk + diff --git a/system_backup.sh b/system_backup.sh new file mode 100755 index 0000000..32648d6 --- /dev/null +++ b/system_backup.sh @@ -0,0 +1,97 @@ +#!/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}