Skip to content

Instantly share code, notes, and snippets.

@JackCuthbert
Last active January 3, 2020 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackCuthbert/1db41d737ac74f6c5b68d4d4e24bed49 to your computer and use it in GitHub Desktop.
Save JackCuthbert/1db41d737ac74f6c5b68d4d4e24bed49 to your computer and use it in GitHub Desktop.
#!/bin/sh
# sudo crontab -e
# * 3 * * * export BORG_PASSPHRASE=repo_passphrase; /path/to/backup.sh /path/to/repo.borg /source/folder/to/backup >> /path/to/backup.log 2>&1
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
info "Starting backup"
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-if-present .nobackup \
"$1"::"{hostname}-{utcnow}" "$2"
backup_exit=$?
info "Pruning repository"
borg prune \
--list \
--prefix '{hostname}-' \
--show-rc \
--keep-daily 3 \
--keep-weekly 2 \
--keep-monthly 3 \
"$1"
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
exit ${global_exit}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment