Simple backup script for Rackspace Cloud

By Stephen Musgrave
Dec 21, 2009
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 "www.SITENAME.com/backup
  2. Create script, backup.sh, in backup directory
  3. Change variables at the top of backup.sh
  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: devnull@mosso.com (or your email address for testing)
    3. Command language: perl
    4. Command to run: backup/backup.sh

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 http://snippets.dzone.com/posts/show/4172

#### 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/www.DOMAIN_NAME.com"
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

Comments

Nice tip!

This improves on what I have been using for my Rackspace backups.

Thanks for the tip!

Joe

Great script!

Thanks for this! You just saved me a stack load of time putting a similar thing together! Now I just need to figure out a way of pushing the backup across to another server in a secure way!

Cheers!
K

Nice script

Worked first time once I changed the variables! Sweet.

Then I modified to create 7 days of daily backsups and 8 weeks of weekly backups.

Thank you very much.

Backup location

Thanks Stephen. Is it possible to backup to above the doc root?

cheers,
Andrew

I believe that's what this

I believe that's what this script does -- note that it is referencing $ACCOUNT_ROOT. I can't verify this because I am no longer with Rackspace Cloud. In fact, I am now using Backup & Migrate to do my backups on cron.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
    Drupal Association Organisation Member     N-TEN Member
X
You may login with either your assigned username or your e-mail address.
The password field is case sensitive.

Loading