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.
- Robert Douglass also posted about another approach using FTP: How to backup your web server.