Skip to content

Instantly share code, notes, and snippets.

@fljdin
Last active September 29, 2021 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fljdin/de46c7c8d18cc37e591cb8364ecf8eef to your computer and use it in GitHub Desktop.
Save fljdin/de46c7c8d18cc37e591cb8364ecf8eef to your computer and use it in GitHub Desktop.
#/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