Back To The Main Support Page

Backups

Keeping Backups isn't just being safe, it's smart. You're going to need to keep your code safe, and that means backing up at least every night to an off-site location.

Contents

Backups using Amazon's S3
Local Backups and using rdiff-backup

Backups using Amazon's S3

Amazon S3 is storage for the Internet. It is designed to make web-scale computing easier for developers.

Amazon S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers. -- Amazon.com: Amazon S3, Amazon Simple Storage Service

You're going to need to sign-up for an S3 account, and accept all the terms of services before continuing. We'll need your access key and your secret access key after signing up, so keep those handy.

We're going to use the most excellent set of recipes by Adam Greene of 6 Bar 8, LLC., Sweetspot.dm. His s3.rake recipes allow you to easily backup code, database, and even subversion repositories (if they exist publicly).

Adam's setting up a rubyforge project soon because we've made some additions and improvements, but in the mean time, grab the latest from our repository: deployment/trunk/s3.rake and place it in your lib/tasks directory. You should now have access to the s3 tasks, which you can see by running "rake -T".

Now, install the Amazon S3 ruby library by downloading and placing it in your lib directory. It can be found here: http://developer.amazonwebservices.com/connect/entry.jspa?externalID=135&categoryID=47.

Create a 's3.yml' file in your config directory. It should look like this:

#!/bin/bash
aws_access_key: 'your_key_here'
aws_secret_access_key: 'your_secret_access_key_here'
options:
  use_ssl: true #set it to true or false

Now, we can execute these tasks using capistrano or automate using cron.

If you're using the slingshot library, you can simply add or overwrite the crontab_configuration_setup task and add these lines:

# BACKUP tasks - to supress output use: cd #{deploy_to}/current && rake s3:backup >/dev/null 2>&1
# every day at 3:00am:
00 3 * * *        root    cd #{deploy_to}/current && /usr/local/bin/rake RAILS_ENV=production s3:backup:code INCREMENT=daily && /usr/local/bin/rake RAILS_ENV=production s3:backup:db INCREMENT=daily && /usr/local/bin/rake RAILS_ENV=production s3:manage:clean_up KEEP=7 INCREMENT=daily
# every sunday at 3:22am:
22 3 * * 0        root    cd #{deploy_to}/current && /usr/local/bin/rake RAILS_ENV=production s3:backup:code INCREMENT=weekly && /usr/local/bin/rake RAILS_ENV=production s3:backup:db INCREMENT=weekly && /usr/local/bin/rake RAILS_ENV=production s3:manage:clean_up KEEP=5 INCREMENT=weekly
# every 1st of month at 3:42am:
42 3 1 * *        root    cd #{deploy_to}/current && /usr/local/bin/rake RAILS_ENV=production s3:backup:code INCREMENT=monthly && /usr/local/bin/rake RAILS_ENV=production s3:backup:db INCREMENT=monthly && /usr/local/bin/rake RAILS_ENV=production s3:manage:clean_up KEEP=12 INCREMENT=monthly
# every year on dec 31 at 4:00am:
00 4 31 12 *      root    cd #{deploy_to}/current && /usr/local/bin/rake RAILS_ENV=production s3:backup:code INCREMENT=yearly && /usr/local/bin/rake RAILS_ENV=production s3:backup:db INCREMENT=yearly && /usr/local/bin/rake RAILS_ENV=production s3:manage:clean_up KEEP=1 INCREMENT=yearly

If you ever get tired of the output, you can of course add to the end of the line:

>/dev/null 2>&1

to pipe all output to the void.

Adam cleverly recommends something like this:

	task :before_migrate, :roles => [:app, :db, :web] do
	   # this will back up your svn repository, your code directory, and your mysql db.
	   run cd #{current_path} && rake --trace RAILS_ENV=production s3:backup
	end

Now, you're all setup and backing up the last 7 daily increments, the last 5 weekly increments, the last 12 monthly, and the last 1 yearly backups of code and database.

Local Backups and using rdiff-backup

First, let's back up the files we want locally on the server itself.

Put this script into /etc/cron.daily/ and you're on your way:

#!/bin/bash

# backup and make hotcopies of svn repos
# ex: 
# svnadmin hotcopy /var/svn/repos/simple /backups/svn-backup/simple
# 
# to keep archives on server use:
# DATE=`date +%y-%m-%d`
# backupDir=/backups/svn-backup/$DATE

svnDirs=`find /var/svn/repos/* -type d -prune`
backupDir=/backups/svn-backup

# remove old backp
rm -rf $backupDir

mkdir $backupDir

for dir in $svnDirs; do
        #svnadmin hotcopy $dir /backups/svn-backup/
        appName=`basename $dir`
        /usr/bin/svnadmin hotcopy $dir $backupDir/$appName
        echo Backed up: $appName
done

To keep your databases backed up, consider at least a nightly dump as well:

#!/bin/bash

mysqldump --user=user --password=password --all-databases > /backups/mysql-backup/mysqlbackup_all_databases.sql

We recommend the use of something like rdiff-backup to keep all your data off-site, on another server.

For more information see: Automated Backups in Linux using rdiff-backup.