Having now set up several sites on an Amazon EC2 Ubuntu instance, I wanted a way to ensure regular backups of the sites were sent somewhere else for disaster recovery. It’s a variety of different sites, three WordPress blogs, one Webtrees genealogy site and a ZenPhoto library. However they are all of the same typical architecture… php apps with a mysql database backing it. Zen photo also has the big filesystem dump of images arranged into albums (directories).
Fortunately Amazon makes it relatively cheap and cost effective! S3 storage is both cheap and has redundancy that should make even the most paranoid people sleep well at night! And the s3cmd package makes it a simple task to script syncronisation of an ec2 filesystem (or part of it) to S3 storage which is independent of the EC2 instance.
So assuming you have the s3cmd package installed and configured, he is a very quick and very dirty first draft of a script I wrote to backup the sites to S3. I also have an AMI made of my current working ubuntu install, so disaster recovery should be really simple…
- Create new instance from the AMI
- Restore the databases from these backups
- Restore the webdocs from these backups
- Switch elastic IP to new instance
Anyway, as promised here is the very rough script, I am no bash master so I am sure you may be able to make many suggestions on how to improve it!
#!/bin/bash
## LOG FILE
LOGFILE=/home/ubuntu/s3_backup.log
exec > $LOGFILE 2>&1## ROOT DIR FOR BACKUPS
BAKROOT=/mnt/public_html/backups/## CLEAN UP THE OLD BACKUPS
cd $BAKROOT
echo “You are In Backup Directory” $BAKROOT
find -mindepth 2 -delete
echo “Old files deleted”
DSTAMP=$(date +%Y-%m-%d-%H-%M)## CREATE THE DB BACKUPS
mysqldump -h localhost -u USER1 -p’SOMEPASS’ DB1 > $BAKROOT/SITE1.COM/SITE1.COM_$DSTAMP.sql
mysqldump -h localhost -u USER2 -p’SOMEPASS’ DB2 > $BAKROOT/SITE2.COM/SITE2.COM_$DSTAMP.sql
mysqldump -h localhost -u USER3 -p’SOMEPASS’ DB3 > $BAKROOT/SITE3.COM/SITE3.COM_$DSTAMP.sql
mysqldump -h localhost -u USER4 -p’SOMEPASS’ DB4 > $BAKROOT/SITE4.COM/SITE4.COM_$DSTAMP.sql
mysqldump -h localhost -u USER5 -p’SOMEPASS’ DB5 > $BAKROOT/SITE5.COM/SITE5.COM_$DSTAMP.sqlecho “Your Database Backups Successfully Completed”
## CREATE THE WEBDOCS BACKUP
tar -cvf $BAKROOT/SITE1.COM/SITE1.COM-$DSTAMP.tar /mnt/public_html/SITE1.COM
tar -cvf $BAKROOT/SITE2.COM/SITE2.COM-$DSTAMP.tar /mnt/public_html/SITE2.COM
tar -cvf $BAKROOT/SITE3.COM/SITE3.COM-$DSTAMP.tar /mnt/public_html/SITE3.COM
tar -cvf $BAKROOT/SITE4.COM/SITE4.COM-$DSTAMP.tar /mnt/public_html/SITE4.COM
tar -cvf $BAKROOT/SITE5.COM/SITE5.COM-$DSTAMP.tar /mnt/public_html/SITE5.COMecho “Your WebDocs Backups Successfully Completed”
## Send backups to S3
s3cmd put –recursive $BAKROOT* s3://SITE_BACKUPS/
echo “Files sent to S3”## Sync the albums to S3 Storage
echo “Photo sync starting at ”
echo $DATE
s3cmd sync /mnt/photos/ s3://SITE_PHOTOS/
echo “Photo sync finished at ”
echo $DATE
Recent Comments