Skip to content

Instantly share code, notes, and snippets.

@jerivas
Last active February 4, 2019 16:30
Show Gist options
  • Save jerivas/e2122e44f3d18f809246cbf04871ff7e to your computer and use it in GitHub Desktop.
Save jerivas/e2122e44f3d18f809246cbf04871ff7e to your computer and use it in GitHub Desktop.
Create a new borg archive on an rsync.net server. Make sure you `borg init` first! Based on: http://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
#!/bin/sh
if [ -z "$1" ] ; then
echo "Please provide a project name as argument"
exit 1
fi
project=$1
# Configure your remote repository here
export BORG_REPO=YOUR_REPO_PATH
export BORG_PASSPHRASE=YOUR_PASSPHRASE
export BORG_REMOTE_PATH=borg1 # Use borg 1.X as provided by rsync.net
# 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 for $project"
# Create a new archive on the remote repo
borg create \
--progress \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
\
--exclude '*/.thumbnails' \
\
::'{now:%Y-%m-%d}' \
"$HOME/backup/$project" \
"$HOME/webapps/$project/static/media"
backup_exit=$?
info "Pruning repository for $project"
# Prune according to the rotation schedule
borg prune \
--list \
--show-rc \
--keep-weekly 8 \
--keep-monthly 18 \
--keep-yearly 3 \
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 for $project finished with a warning"
fi
if [ ${global_exit} -gt 1 ];
then
info "Backup and/or Prune for $project 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