Skip to content

Instantly share code, notes, and snippets.

@luispabon
Last active February 16, 2018 06:09
Show Gist options
  • Save luispabon/cce157ecb230a112782123cd3e2d3016 to your computer and use it in GitHub Desktop.
Save luispabon/cce157ecb230a112782123cd3e2d3016 to your computer and use it in GitHub Desktop.
Encrypted borg backup to s3 of home folder
#!/bin/bash
# NOTE: decided to actually make a more reusable version with README and all, to be found at
# this repo: https://github.com/luispabon/borg-s3-home-backup
# This script will backup your home folder using borg. The backup will
# be both compressed and encrypted. Since borg does not natively support
# s3, and the fuse/mount solution is slow as heck, we're using aws sync instead
# at the end. This actually works pretty well.
#
# The side effect however is you'll have two backups, one wherever borg
# is backing up into, and another on s3. This is good though for as long
# as you have the space locally, always a good idea to keep several backups
# even if one is on the same computer.
#
# This also prunes your backups to keep 7 dailys and 4 end-of-weeks
# Environment vars (all standard to borg):
# - BORG_REPO: for this to work, this must be a local path (or a mounted
# network path, although this would be slower)
# - BORG_PASSPHRASE: encryption key for your backup
#
# AWS config:
# - S3_BUCKET: put in here the bucket name only
# - AWS_PROFILE: put in here the aws cli profile that has access to that bucket (or default if you only have one)
#
# Requirements: borg, awscli configured with key/secret, s3 bucket
# AWS Config
export S3_BUCKET=yer-bucket
export AWS_PROFILE=yer-profile|default
# Backup name
BACKUP_NAME=home-`date +%Y-%m-%dT%H.%M`
# Borg env vars MUST be set, these are standard to borg
if [[ ! "$BORG_REPO" ]]; then
printf "\n ** Please provide with BORG_REPO\n"
exit 1
fi
if [[ ! "$BORG_PASSPHRASE" ]]; then
printf "\n ** Please provide with BORG_PASSPHRASE\n"
exit 1
fi
# Local borg backup, low priority
# Adjust excludes to your particular usage, these are mine
nice -n19 \
borg create ::${BACKUP_NAME} \
~ \
--stats \
--progress \
--exclude "~/.cache" \
--exclude "~/.npm" \
--exclude "~/.composer" \
--exclude "~/.vagrant.d" \
--exclude "~/Downloads" \
--exclude 're:/vendor/' \
--exclude 're:/node_modules/' \
--compression zlib,6
if [ $? == 0 ]; then
# Clean up old backups: keep 7 end of day and 4 additional end of week archives.
borg prune -v --list --keep-daily=7 --keep-weekly=4
# Sync borg repo to s3, again low priority
nice -n19 aws s3 sync ${BORG_REPO} s3://${S3_BUCKET} --profile=${AWS_PROFILE} --delete
else
printf "\n Backup failed because reasons"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment