How to use EC2 as a failover WWW Server

The combination of EC2 and S3 are truly powerful services. There is also quite a bit of ambiguity in terms of applications. One of my recent projects was to automate an S3 backup on our web server and have an EC2 instance on stand by in case that server failed. What I came up with is a fairly good setup and more importantly it is simple to use.

The Server I was backing up was a simple LAMP box running about 5 domains. This was more of a practical academic exercise than a mission critical failover system. That being said, it would make an excellent mission critical failover system!

This isn't really an absolute beginners guide. This is more of an outline of what I did and assumes you are familiar with both EC2 and S3. It dovetails nicely with my previous article on creating an EC2 image. This setup uses the EC2 image I created of one of our production web server.

What is needed:

  • An Amazon EC2/S3 account
  • A working EC2 instance
  • S3sync installed on your prod box
  • A running server to backup
  • Some patience

Backing up to S3

The first step is backing up your web data to S3. There are a many ways to do this. I needed something simple, quick and dirty. I choose to hack up some bash and use the S3Sync tools. They can be downloaded here. Follow the README in the docs to get s3 all setup and rocking. Once you can successfully run a: "s3cmd.rb listbuckets" you are good.

I hacked up this bash script to be fired off via cron. It ain't pretty but it works.

#!/bin/bash
# Get location of executables
tar="$(/usr/bin/which tar)"
mysqldump="$(/usr/bin/which mysqldump)"
chown="$(/usr/bin/which chown)"
cat="$(/usr/bin/which cat)"
md5sum="$(/usr/bin/which md5sum)"
# Define config vars
mysql_user=mysql
mysql_pass=mysqlpass
backup_user=backup
backup_dir=/home/backup/backups
# Creates backup of websites and mysql
cd /var/www
$tar -cvzf $backup_dir/vhosts.tgz vhosts"
$mysqldump --all-databases -u$mysql_user -p$mysql_pass > /$backup_dir/mysql.sql 
$chown -R $backup_user:$backup_user $backup_dir
#S3 Sync Time:
/opt/s3sync/s3sync.rb -r --ssl --delete $backup_dir/ mysite.com:/backups

Set that script as a cron job and you've just created a simple S3 backup solution!

Importing S3 into EC2

When I created my EC2 instances I also bundled all the scripts I needed to import the S3 data. It is simple and just plain works. Right now the switch over is manually executed but it would be just as easy to automate. Something similar to the following script is used to pull data from our S3 backup:

#!/bin/bash
s3sync=/opt/s3sync/s3sync.rb
s3cmd=/opt/s3sync/s3cmd.rb
#Define Config Variables
remote_path=mysite.com:/backups
local_path=/root/files
$s3sync -r --ssl --delete --progress $remote_path $local_path

Once that script is finished your S3 data is now on your EC2 instance! From there I have another script that is used to import the data. It would be trivial to combine these into one script. I seperated them for testing purposes.

#!/bin/bash
# Imports WWW sync into server
#Command Vars
mysql="$(/usr/bin/which mysql)"
mysqladmin="$(/usr/bin/which mysqladmin)"
tar="$(/usr/bin/which tar)"
chmod="$(/usr/bin/which chmod)"
move="$(/usr/bin/which mv)"
apachectl="$(/usr/bin/which apachectl)"
#Config Vars
file_path=/root/files/backups
www_dir=/var/www
mysql_user=mysql
mysql_pass=mysqlpass
 
#Import MySQL
echo "Importing SQL Data..."
$mysql -u$mysql_user -p$mysql_pass < $file_path/mysql.sql
echo "Flushing Mysql Permissions"
$mysqladmin -u$mysql_user -p$mysql_pass flush-privileges
 
#Move Vhost files
echo "Extracting web files..."
$tar -xzf $file_path/vhosts.tgz -C $www_dir
echo "Modifying Permissions..."
$chmod 755 -R $www_dir/vhosts
 
#Restart Apache
echo "Restart Apache..."
$apachectl graceful
 
echo "Web Data successfully imported!"

Once the preceding script is run your Mysql and web data should be all imported into the EC2 instance. As stated, there is room for improvement but these "recipes" will get you up and running if your vps server ever goes down!