Skip to content

Instantly share code, notes, and snippets.

@mickm
Created March 23, 2011 02:51
Show Gist options
  • Save mickm/882530 to your computer and use it in GitHub Desktop.
Save mickm/882530 to your computer and use it in GitHub Desktop.
Duplicity wrapper to manage S3 backups for media files
#!/bin/bash
#
# Duplicity wrapper to manage S3 backups for media files.
#
# aptitude install duplicity python-boto
#
# Configure the settings below then run a test backup AND a test restore.
# Once working, schedule a regular backup with cron. Only one backup will
# run at a time so set a short cron period if you want.
#
# Example usage:
#
# s3helper backup
# s3helper restore path/to/file /path/to/restored/file
# s3helper list
# s3helper status
#
# Example crontab to backup every hour:
#
# MAILTO=you@example.com
# 0 * * * * $HOME/bin/s3helper backup 2>&1
#
script_name=${0##*/}
# Configure your S3 settings below then comment this line out.
echo "Edit this script to configure your S3 settings" && exit 1
# S3 settings.
export AWS_ACCESS_KEY_ID=changeme
export AWS_SECRET_ACCESS_KEY=changeme
dst=s3+http://changeme/changeme
src=/srv/myapp/shared/system/content
lockfile=/tmp/${script_name}.lock
# Normal use should not require editing beyond this point.
cleanup () {
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
}
cleanup_and_unlock () {
cleanup; rm -f ${lockfile}
}
die () {
echo "$*" >&2; exit 1
}
trap cleanup EXIT
case $1 in
backup)
# Duplicity backs up symlinks as symlinks. We're backing up media
# files only, so we resolve symlinks as a precaution.
src=$(readlink -f $src)
# Lock to prevent concurrent backups.
lockfile -r 0 ${lockfile} || exit 1
trap cleanup_and_unlock EXIT
/usr/bin/duplicity \
--s3-use-new-style \
--no-encryption \
--asynchronous-upload \
--volsize 25 \
"--full-if-older-than" 1M incremental \
--include ${src} \
--exclude '**' $(dirname ${src}) ${dst}
;;
restore)
remote_path=$2
local_path=$3
[ "$local_path" = "" ] && \
die "usage: ${script_name} restore remote_path local_path"
/usr/bin/duplicity \
--s3-use-new-style \
--no-encryption \
--file-to-restore=${remote_path} restore ${dst} ${local_path}
;;
status)
/usr/bin/duplicity \
--s3-use-new-style \
--no-encryption \
collection-status ${dst}
;;
list)
/usr/bin/duplicity \
--s3-use-new-style \
--no-encryption \
list-current-files ${dst}
;;
*)
die "usage: ${script_name} {backup|status|list|restore}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment