r/ScriptSwap • u/edingc • Mar 01 '12
Automatic MySQL backup script to Amazon S3 using s3cmd
I use Amazon S3 as a cheap MySQL backup medium for my VPS. This script backs up multiple MySQL databases each night when run as a cron job.
Combined with Amazon's recently introduced object expiration, I can keep one week’s worth of rolling backups with no work on my part.
Assumptions:
You have root access to your server and are able to schedule cron jobs.
You have an Amazon Web Services account.
s3cmd is installed and configured on your server. There are package repositories available for many Linux distributions.
Your website’s files are located in a folder named with the domain name. In this example, we will assume our websites’ domain names and their corresponding folders are coolwebsite.com and notsocoolwebsite.com.
The corresponding MySQL databases are named coolwebsite and notsocoolwebsite. Notice the domain (.com) is not included in the database name.
The script:
#!/bin/bash
# MySQL backup script to Amazon S3 using s3cmd
# Run from cron for automated backups
#
# By: Cody Eding ([email protected])
# From: http://codyeding.com/2012/01/27/mysql-backup-script-amazon-s3cmd
#
# License: none (public domain)
# Set variables for home folder, domains to backup and S3 bucket.
# Change these to match your environment.
HOME="user"
SITES=( "coolwebsite.com" "notsocoolwebsite.com" ) # Array of domains
S3BUCKET="backup-bucket"
# Set a variable with the current date in mmddyyyy format.
# This does not need to be changed.
DATE=$(date +"%m%d%Y")
# Loop through each website in the array.
for SITE in ${SITES[@]}
do
# Store the base URL sans domain in a variable.
# This is passed to the mysqldump command and used in the filename.
BASEURL=$(echo $SITE | cut -d"." -f1)
# Build the filename by concatenating strings.
FILE=$BASEURL$DATE.sql.gz
# Backup the database and pass the output to gzip for compression.
# Enter your MySQL credentials.
mysqldump -u <MYSQLUSER> -p<MYSQLPASSWORD> $BASEURL | gzip > /home/$HOME/$FILE
# Put the files on Amazon S3 with s3cmd.
# s3cmd will not work in a script without passing the path to the config file.
s3cmd --config /home/$HOME/.s3cfg put $FILE s3://$S3BUCKET/mysql/$DATE/$URL
# Delete the backup off of the local server.
rm $FILE
# End the loop.
done
1
u/edingc Mar 02 '12
I should add that this probably isn't a good solution if your databases are larger and you have decent web traffic -- it could overload your server.