Skip to content

Instantly share code, notes, and snippets.

@fljdin
Last active Aug 24, 2021
Embed
What would you like to do?
#/bin/bash
# encrypted USB stick by uuid
UUID=a20ec820-de7d-40ec-ac29-4595a10c38e7
PASS=xxxxxxxx
EXCLUDE=$HOME/.config/borg/exclude.list
# helpers
info() { notify-send "$*"; }
trap 'echo $( date ) Backup interrupted >&2; disk_unmount; exit 2' INT TERM
disk_mount() {
udisksctl unlock \
--key-file=<(echo -n "$PASS") \
--block-device=/dev/disk/by-uuid/$UUID && \
udisksctl mount \
--block-device=/dev/mapper/luks-$UUID
}
disk_unmount() {
udisksctl unmount \
--block-device=/dev/mapper/luks-$UUID && \
udisksctl lock \
--block-device=/dev/disk/by-uuid/$UUID
}
# main
info "Starting backup"
disk_mount
export BORG_REPO=$(ls -d1 /run/media/$USER/*/borg-backup 2> /dev/null | head)
if [ ! -d "$BORG_REPO" ] ; then
info "Borg repository is unavailable"
exit 2
fi
borg create \
--stats --show-rc \
--exclude-from $EXCLUDE \
::'{hostname}-{now}' \
$HOME
backup_exit=$?
borg prune \
--stats --show-rc \
--prefix '{hostname}-' \
--save-space --keep-daily 7
prune_exit=$?
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings"
else
info "Backup and/or Prune finished with errors"
fi
disk_unmount
exit ${global_exit}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment