Friday, January 15, 2010

Simple backup script for Rackspace Cloud

I will have to look at implementing this one a couple of sites. - Mike

via Capellic by Stephen Musgrave on 12/21/09

I could have installed the Drupal Backup & Migrate module for this, but then that's yet another module and yet another activity during cron -- and how would it play with my caching done at cron? I didn't want to find out.
There was a very simple backup script that I found in Rackspace Cloud's knowledge base, but it didn't have rolling backups.  I've found the errors in data aren't found for a couple of days, so a backup down at 4:00 AM that morning wasn't going to be very helpful. 

I wanted rolling backups for 4 days, and I found this script which did just that.  I added some variables for the Rackspace Cloud environment, added some debugging statements and I was done.

The script will create a total of 4 backup directories with directory "01" always having the most current backup and "04" having the oldest backup.  Two archive files will be created in each folder – one for the database and one for the file system.

While I am using this to backup my Drupal sites, it certainly isn't limited to it.  In fact, if it was truly setup for Drupal, I would have some code in here that would put the site in maintenance mode, run the backup and then turn the site back on.  I would proabably also opt not to backup all tables – excluding access_log, cache_* and watchdog.  I'll get that in the new year.

Note that this script can also be used to run backups for any time interval.  It would be easy to copy this script to use for a weekly backup.  Just change the backup directory names so that they don't overwrite your daily backups and then setup the cron to run once a week.  Or it might be better to copy the existing backup to a new folder once a week.

Installing the Script

Follow these steps to setup on Rackspace Cloud:
  1. Create directory "
  2. Create script,, in backup directory
  3. Change variables at the top of
  4. Add cron job to run once a day that isn't running when your cron.php job is running (Drupal)
    1. Task name: Daily Backup
    2. Email Address for Output: (or your email address for testing)
    3. Command language: perl
    4. Command to run: backup/

The Script

Just change the variables at the top of the file to match your website settings and you don't have to touch anything under that.

Of course you can remove all the echo lines, but I find they are useful for debugging.  I like to have the resutls of the cron emailed to me for a week and then I'm reassured that the script is running consistently.  (Cron can be moody.)

 #!/bin/bash # Modeled after #### VARIABLES # ACCOUNT_ROOT can be found on the Features tab in the control panel for the site export ACCOUNT_ROOT="/mnt/stor2-wc1-dfw1/394655/394685/" export WEB_ROOT="${ACCOUNT_ROOT}/web/content" export DB_HOST="DB_SERVER_INTERNAL_NAME" export DB_USER="DB_USERNAME" export DB_PASSWORD="DB_PASSWORD" export DB_NAME="DB_NAME" #### PROGRAM - NO EDITING AFTER THIS LINE SHOULD BE NECESSARY echo "Rotating backups..." rm -rf $ACCOUNT_ROOT/backup/04 mv $ACCOUNT_ROOT/backup/03 $ACCOUNT_ROOT/backup/04 mv $ACCOUNT_ROOT/backup/02 $ACCOUNT_ROOT/backup/03 mv $ACCOUNT_ROOT/backup/01 $ACCOUNT_ROOT/backup/02 mkdir $ACCOUNT_ROOT/backup/01 echo "... done rotating backups." echo "Starting database backup..." mysqldump --host=$DB_HOST --user=$DB_USER --password=$DB_PASSWORD --all-databases | bzip2 > $ACCOUNT_ROOT/backup/01/mysql-`date +%Y-%m-%d`.bz2 echo "... database backup complete." echo "Starting file system backup..." tar czf $ACCOUNT_ROOT/backup/01/web_backup.tgz $ACCOUNT_ROOT/web/content/ echo "... file system backup complete." exit 0 #### END PROGRAM

Posted via email from Mike's posterous

No comments: