Skip to content

Instantly share code, notes, and snippets.

@mattconnell
Last active March 20, 2018 03:31
Show Gist options
  • Save mattconnell/a6f0615f9d978fb1abeda3692807977a to your computer and use it in GitHub Desktop.
Save mattconnell/a6f0615f9d978fb1abeda3692807977a to your computer and use it in GitHub Desktop.
Borg backup bash script for rsync.net
#!/bin/sh
### Setup ###
REPO_SERVER="rsync.net"
REPO_NAME="detroit"
REPO_PATH="${REPO_SERVER}:${REPO_NAME}"
# Get the date+timestamp
CURDATE="$(date -I)-$(date +%s)"
# Set the desired archive name.
ARCH_NAME="${REPO_NAME}"_"${CURDATE}"
# Helper function
info()
{
printf "\n%s %s\n\n" "$( date )" "$*" >&2;
}
# Some basic error handling.
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
# ---
### Start work ###
info "Starting backup"
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--remote-path borg1 \
--exclude-caches \
--exclude '/home/m/.cache/*' \
--exclude '/home/m/.mozilla/*' \
--exclude '/home/m/.thunderbird/*'\
--exclude '/home/m/.thumbnails/*' \
--exclude '/home/m/.local/share/*'\
--exclude '/home/m/qemu/*' \
--exclude '/home/m/projects/*' \
--exclude '/home/m/Downloads/*' \
--exclude '/var/cache/*' \
--exclude '/var/tmp/*' \
\
"${REPO_PATH}"::"${ARCH_NAME}" \
/home/m/ \
/etc/ \
/var/lib/portage/ \
backup_exit=$?
info "Pruning repository"
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
borg prune \
--list \
--prefix "${REPO_NAME}_" \
--show-rc \
--remote-path borg1 \
--keep-daily 30 \
--keep-weekly 8 \
--keep-monthly 3 \
"${REPO_PATH}"
prune_exit=$?
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 1 ];
then
info "Backup and/or Prune finished with a warning"
fi
if [ ${global_exit} -gt 1 ];
then
info "Backup and/or Prune finished with an error"
fi
exit ${global_exit}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment