Skip to content

Instantly share code, notes, and snippets.

@scottalan
Created November 4, 2014 15:46
Show Gist options
  • Select an option

  • Save scottalan/b006a66bb4f3417285d1 to your computer and use it in GitHub Desktop.

Select an option

Save scottalan/b006a66bb4f3417285d1 to your computer and use it in GitHub Desktop.
Amazon S3 Database Backup
#!/bin/bash
# Make directories: log and backup at the $DIR.
# Move the '.s3cfg' file to the $DIR as well.
# NOTE: CHANGE ALL VARIABLES TO MATCH YOUR NEEDS!
# The name of the s3 bucket on Amazon.
S3BUCKET="REPLACE_BUCKET_NAME"
# The name of the database to backup.
DBNAME="REPLACE_DB_NAME"
# The email address to send log file to.
EMAIL="REPLACE_EMAIL_ADDRESS"
# The path to directory above Drupal root.
DIR="REPLACE/PATH/TO/DIR"
# Local sql dump directory.
SQLDUMP=$DIR/sqldumps
# The name of the file to be created for the backup.
FILE=$DBNAME-`date "+%Y%m%d-%H%M"`.sql.gz
if [ ! -f $DIR/s3/log/mysqlback.log ]; then
/usr/bin/touch $DIR/s3/log/mysqlback.log
echo "subject: S3 database backup" >> $DIR/s3/log/mysqlback.log
fi
mysqldump --defaults-extra-file=$DIR/s3/mysql.cnf $DBNAME | gzip -9 > $SQLDUMP/$FILE
/usr/local/bin/s3cmd --config $DIR/s3/.s3cfg put $SQLDUMP/$FILE s3://$S3BUCKET/ >> $DIR/s3/log/mysqlback.log
sleep 5
# This assumes there is a 'subject: My subject' line in mysqlback.log. Will not
# send without it.
sendmail $EMAIL < $DIR/s3/log/mysqlback.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment