Skip to content

Instantly share code, notes, and snippets.

@awaddell
Forked from oodavid/README.md
Last active February 15, 2016 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awaddell/13112c5f26e8ef0a477f to your computer and use it in GitHub Desktop.
Save awaddell/13112c5f26e8ef0a477f to your computer and use it in GitHub Desktop.
Backup MySQL to Amazon S3

Backup MySQL to Amazon S3

This is a simple way to backup your MySQL tables to Amazon S3 for a nightly backup - this is all to be done on your server :-)

Sister Document - Restore MySQL from Amazon S3 - read that next

1 - Install s3cmd

this is for Centos 5.6, see http://s3tools.org/repositories for other systems like ubuntu etc

# Install s3cmd
cd /etc/yum.repos.d/
wget http://s3tools.org/repo/CentOS_5/s3tools.repo
yum install s3cmd
# Setup s3cmd
s3cmd --configure
    # You’ll need to enter your AWS access key and secret key here, everything is optional and can be ignored :-)

2 - Add your script

Upload a copy of s3mysqlbackup.sh (it will need some tweaks for your setup), make it executable and test it

# Add the executable bit
chmod 0700 /usr/local/bin/s3mysqlbackup.sh
# Run the script to make sure it's all tickety boo
/usr/local/bin/s3mysqlbackup.sh

3 - Run it every night with CRON

Assuming the backup script is stored in /usr/local/bin we need to add a crontask to run it automatically:

# Edit the crontab
env EDITOR=nano crontab -e
# params for s3mysqlbackup.sh
mysqlpass="password"
bucket="s3://xxxxx-backups/rds"
mysqlhost="mysql-master"
    # Add the following lines:
    # Run the database backup script at 3am
    0 3 * * * mysqlpass=$mysqlpass bucket=$bucket mysqlhost=$mysqlhost /usr/local/bin/s3mysqlbackup.sh /usr/local/bin/s3mysqlbackup.sh >/dev/null 2>&1
#!/bin/bash
# Based on https://gist.github.com/2206527
# Be pretty
echo -e " "
echo -e " . ____ . ______________________________"
echo -e " |/ \| | |"
echo -e "[| \e[1;31m♥ ♥\e[00m |] | S3 MySQL Backup Script v.0.1 |"
echo -e " |___==___| / © oodavid 2012 |"
echo -e " |______________________________|"
echo -e " "
# aws cli needs the region
region=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/'`
# Timestamp (sortable AND readable)
stamp=`date +"%s - %A %d %B %Y @ %H%M"`
# List all the databases
databases=`mysql -u root -p$mysqlpass -h $mysqlhost -e "SHOW DATABASES;" | tr -d "| " | grep -v "\(Database\|information_schema\|performance_schema\|mysql\|test\)"`
# Feedback
echo -e "Dumping to \e[1;32m$bucket/$stamp/\e[00m"
# Loop the databases
for db in $databases; do
# Define our filenames
filename="$stamp - $db.sql.gz"
tmpfile="/tmp/$filename"
object="$bucket/$stamp/$filename"
# Feedback
echo -e "\e[1;34m$db\e[00m"
# Dump and zip
echo -e " creating \e[0;35m$tmpfile\e[00m"
mysqldump -u root -p$mysqlpass -h $mysqlhost --force --opt --databases "$db" | gzip -c > "$tmpfile"
# Upload
echo -e " uploading..."
aws --region $region s3 cp "$tmpfile" "$object"
# Delete
rm -f "$tmpfile"
done;
# Jobs a goodun
echo -e "\e[1;32mJobs a goodun\e[00m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment