Rsync is a remote file sync protocol written by the same people who brought us the Samba project.

Rsync copies files from one location to another keeping both copies in sync. This means that files changed on location A will be copied to B and vice versa.

Rsync is also used by ibackup.com is a commerical service for online backup and online storage.

One of the nice features they have is the ability to use rsync to backup your files to the space allocated to you.

This article explains how to write a script to backup Drupal to ibackup via rsync. This is not limited to ibackup, and is applicable to rsync capable servers under your direct control.

The strategy is to inform rsync of the location to be backed up, as well as a special location to hold database dumps. Rsycn then copies the entire database dump, and any files that changed on the server (e.g. image files users created as attachments to node, or new modules you installed).

Here is the script. Save it to /usr/local/bin/ibackup.sh or a location of your choice.

#!/bin/sh
# Directory that holds the database backup
DB_BACKUP_DIR=/home/backup/dbdump
# Directories to rsync (Change these)
DIRS_TO_RSYNC="/var/www/test /var/www/production $DB_BACKUP_DIR"
# Names and passwords (Change these)
DB_NAME=production
DB_USER=prod
DB_PASS=mypassword
RSYNC_USER=myuser
RSYNC_PASSWORD=mypass
SUFFIX=`date +%y%m%d`
DB_ARCHIVE=$DB_BACKUP_DIR/$DB_NAME.$SUFFIX.sql.bz2
# Create the dump file, compressed by bzip2
mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME | bzip2 -c > $DB_ARCHIVE
# Export the rsync password
export RSYNC_PASSWORD
# Run the rsync
for DIR in $DIRS_TO_RSYNC
do
rsync -rvzt $DIR $RSYNC_USER@rsync.ibackup.com::ibackup
done 

You need to change the passwords in the appropriate place. I have some shell scripts that extract the user, database name and password for Drupal's database from settings.php automatically, but did not have time to integrate them into this script yet.

Then in cron, add the following to run this backup every day at 1:20 am

1 20 * * * /usr/local/bin/ibackup.sh 

There you have it. You will backup a copy of the entire database every day, and any changed files as well.

You will get an email informing you of whether the backup was successful or if there are errors.

Resources

Comments

Mon, 2007/07/09 - 02:51

Please note that RSYNC, like FTP sends everything in plaintext across the wire. This effectively means anyone can potentially grab your data and read it.
Rsync is also possible over SSH, eliminating this drawback.
Not sure if the mentioned iBackup is capable of that but if you consider doing this consider doing it secure through SSH.

Sun, 2007/09/23 - 15:51

There is also rsnapshot and boxbackup, rsnapshot is a wrapper for rsync to keep multiple backups, boxbackup encrypts at the client end and all the server sees is encrypted data.

Also rsync can be run over SSL

Mon, 2007/07/09 - 15:58

Rdiff backup is interesting solution for this job. http://www.nongnu.org/rdiff-backup/

Mon, 2007/07/09 - 20:33

iBackup.com looks like a great service for a great price. The lowest plan already exceeds my needs. Or perhaps somebody knows an even better service?

Thank you very much for this article!

@Matt: it can easily be used secure. See http://www.ibackup.com/online-backup-linux/index.html.

Sun, 2007/10/07 - 12:35

When you use rsync,ibackup.com, you'll get nasty rsync errors. You have to use rsync4.ibackup.com, then everything works just fine.

Thanks again for this article! And while I've only *just* started using ibackup, I must say that it's *very* fast. The web interface - while sufficient - could be a lot better though, and definitely much faster. You have to use a HTTPS connection, but there's too many images on a page to make it really responsive.

Sun, 2007/10/21 - 14:04

I am getting the following errors using this setup. rsync is installed on both machines, though I'm not if I need to do any special configuring....

[Note: I'm actually not using ibackup, but a remote ftp server where I have backup space reserved.]

Do you have any ideas on what I'm doing wrong?

Thanks.

Albert

Here is the error:

rsync: failed to connect to 38.103.144.98: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(104) [sender=2.6.9]

And here's the edited setup that I'm using...

#!/bin/sh
# Directory that holds the database backup
DB_BACKUP_DIR=/home/backup/dbdump
# Directories to rsync (Change these)
DIRS_TO_RSYNC="/var/www $DB_BACKUP_DIR"
# Names and passwords (Change these)
DB_NAME=ithou_new3
DB_USER=root
DB_PASS=xxxxxx
RSYNC_USER=ajwwong
RSYNC_PASSWORD=xxxxxxx
SUFFIX=`date +%y%m%d`
DB_ARCHIVE=$DB_BACKUP_DIR/$DB_NAME.$SUFFIX.sql.bz2
# Create the dump file, compressed by bzip2
mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME | bzip2 -c > $DB_ARCHIVE
# Export the rsync password
export RSYNC_PASSWORD
# Run the rsync
for DIR in $DIRS_TO_RSYNC
do
rsync -rvzt $DIR $RSYNC_USER@38.103.144.98::ibackup
done

Thanks for your help!

Is your Drupal or Backdrop CMS site slow?
Is it suffering from server resources shortages?
Is it experiencing outages?
Contact us for Drupal or Backdrop CMS Performance Optimization and Tuning Consulting