Permalink
Browse files

New fantabulous and definitive mysql to s3 backup script

  • Loading branch information...
Diego Toharia Diego Toharia
Diego Toharia authored and Diego Toharia committed Jan 30, 2012
1 parent 9b962d4 commit 625f8b9934c2e891a956581b41c5f51f9fa59c07
Showing with 101 additions and 0 deletions.
  1. +27 −0 mysql-to-s3-backupper/README.md
  2. +74 −0 mysql-to-s3-backupper/mysql-to-s3-backupper.sh
@@ -0,0 +1,27 @@
+backup-databases-into-s3
+================================
+
+Description
+-------------------------
+
+Generic script to upload one or more mysql schemas (even all that are available for the given user) to a S3 bucket.
+
+Dependencies
+-------------------------
+
+ * mysqldump
+ * mysql
+ * bunzip2
+ * s3cmd
+
+Usage
+-------------------------
+
+$ backup-mysql-db-to-s3.sh -b s3://myAwesomeBucket/a/cool/path -u mysqlUser -p superSecretPassword [ -s aMysqlSchema -s anotherMysqlSchema ]
+
+Mandatory switches:
+ * -b s3_bucket_and_path: the S3 bucket followed by an optional path
+ * -u the mysql user
+ * -p the password for the given mysql user
+Optional switches:
+ * -s a mysql schema to backup. Can be multi-valuated. If none provided, all the schemas will be backed up
@@ -0,0 +1,74 @@
+#!/bin/bash
+MYSQL_CMD="mysql --skip-column-names"
+MYSQL_DUMP_CMD="mysqldump --single-transaction"
+COMPRESS_CMD="tar jcf"
+S3CMD_PUT="s3cmd put"
+TMP_DIR=/tmp/`basename $0`.$$
+DATE="`date +%F-%H_%M`"
+
+function usage
+{
+ echo "Usage: `basename $0` -b s3_bucket_and_path -u mysql_user -p mysql_password [ -s mysql_schema -s other_mysql_schema ] "
+ exit 1
+}
+
+function backup_schema
+{
+ cd $TMP_DIR
+ SCHEMA="$1"
+ DUMP_FILE="$DATE-$SCHEMA.sql"
+ COMPRESSED_FILE="$DUMP_FILE.tar.bz2"
+ echo "Backing up $SCHEMA"
+ if $MYSQL_DUMP_CMD $SCHEMA > $DUMP_FILE ; then
+ $COMPRESS_CMD $COMPRESSED_FILE $DUMP_FILE
+ $S3CMD_PUT $COMPRESSED_FILE $S3BUCKET/$SCHEMA/$COMPRESSED_FILE
+ else
+ echo "Failed to backup $SCHEMA"
+ fi
+}
+
+while getopts ":u:p:s:b:" opt; do
+ case $opt in
+ u)
+ DBUSER=$OPTARG
+ ;;
+ p)
+ DBPASSWD=$OPTARG
+ ;;
+ s)
+ DBSCHEMAS="$DBSCHEMAS $OPTARG"
+ ;;
+ b)
+ S3BUCKET=$OPTARG
+ ;;
+ \?)
+ echo "Invalid option: -$OPTARG" >&2
+ usage
+ ;;
+ esac
+done
+
+if [[ -z "$DBUSER" ]] || [[ -z "$DBPASSWD" ]] || [[ -z "$S3BUCKET" ]] ; then
+ usage
+else
+ MYSQL_OPTS="-u $DBUSER -p$DBPASSWD"
+ MYSQL_CMD="$MYSQL_CMD $MYSQL_OPTS"
+ MYSQL_DUMP_CMD="$MYSQL_DUMP_CMD $MYSQL_OPTS"
+ mkdir $TMP_DIR
+fi
+
+if [[ -z "$DBSCHEMAS" ]] ; then
+ echo "No schemas supplied... all schemas will be backup"
+ for DBSCHEMA in `echo "show databases" | $MYSQL_CMD` ; do
+ backup_schema $DBSCHEMA
+ done
+else
+ for DBSCHEMA in $DBSCHEMAS ; do
+ backup_schema $DBSCHEMA
+ done
+fi
+
+if [[ -d $TMP_DIR ]] ; then
+ rm -rf $TMP_DIR
+fi
+

0 comments on commit 625f8b9

Please sign in to comment.