Skip to content

Instantly share code, notes, and snippets.

@omega8cc
Last active August 29, 2015 14:09
Show Gist options
  • Save omega8cc/2d507adb81ae203e9277 to your computer and use it in GitHub Desktop.
Save omega8cc/2d507adb81ae203e9277 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Backs up the database and filesystem for a webapp
# Dumps a current db snapshot and moves it to within the
# webapp's directory structure, so that Duplicity can
# back it up to Amazon's S3 service
# Based on instructions by Randy Sesser:
# http://www.randys.org/2007/11/16/how-to-automated-backups-to-amazon-s-s3-with-duplicity/
# Good practice...
set -u
set -e
# First, lets dump a snapshot of all tables in the appropriate MySQL db,
# bzip and chmod it appropriately, and move it to where we want it
echo "Starting db dump at `date`..."
NOW=$(date +"%Y%m%d-%H%M%S")
FILENAME="/path/to/dump/foo-$NOW.sql.bz2"
export DB_PASSWD=$(/path/to/db/password/executable/db-passwd)
mysqldump -u dbusername -p$DB_PASSWD databasename | bzip2 -z -9 > $FILENAME
echo "Dumped db as $FILENAME"
echo "Now chmodding and moving..."
chmod 600 $FILENAME
mv $FILENAME /path/to/where/you/want/dump/moved/to
echo "db dump complete at `date`"
# Next, backup with Duplicity
echo "Starting duplicity backup at `date`..."
# Export some ENV variables so we don't have to type anything
# The PYTHONPATH is a mess as Duplicity is built in my home directory
# and it was the only way to get all the required modules working
export PYTHONPATH="/usr/lib/python2.5:/home/username/duplicity/lib/python2.5/site-packages:/usr/lib/python2.5/site-packages:/usr/lib/python2.5/lib-dynload"
export AWS_ACCESS_KEY_ID="your_AWS_key_id"
export AWS_SECRET_ACCESS_KEY="your_AWS_secret_access_key"
export PASSPHRASE=$(/path/to/gpg/passphrase/executable/gpg-passphrase)
# The id of the GPG key we want to use
GPG_KEY="01234567"
# Source
# The source of what we're backing up
SOURCE=/path/of/what/you/want/to/backup/
# Destination
# Bucket name needs to be unique amongst Amazon S3 users
DEST=s3+http://"your_bucket_name"
# The actual Duplicity command
/path/to/duplicity \
--encrypt-key=${GPG_KEY} \
--sign-key=${GPG_KEY} \
--exclude=/path/of/any/directory/we/wish/to/exclude/ \
--s3-use-new-style \
--s3-european-buckets \
${SOURCE} ${DEST} \
>> /path/to/where/we/want/to/log/duplicity.log
echo "Backup finished at `date`, now sending email notification..."
# Semd an email to notify admin that the jobs been done
SUBJECT="Duplicity Backup - `date`"
TO="[email protected]"
MESSAGE=/path/to/where/we/want/to/log/duplicity.log
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
# Reset the ENV variables. Don't want them just sitting around.
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export PASSPHRASE=
export TO=
export SUBJECT=
export MESSAGE=
export DB_PASSWD=
echo "All finished at `date`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment