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
- Robert Douglass also posted about another approach using FTP: How to backup your web server.
Comments
Visitor (not verified)
Not secure
Mon, 2007/07/09 - 02:51Please 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.
Visitor (not verified)
Other Options
Sun, 2007/09/23 - 15:51There 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
Visitor (not verified)
Ibackup also has secure way
Thu, 2008/12/04 - 18:56Ibackup also has secure way to backup files through rsync with stunnel
Visitor (not verified)
Secure rsync? rdiff-backup
Mon, 2007/07/09 - 15:58Rdiff backup is interesting solution for this job. http://www.nongnu.org/rdiff-backup/
Visitor (not verified)
Thanks!
Mon, 2007/07/09 - 20:33iBackup.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.
Visitor (not verified)
Minor correction.
Sun, 2007/10/07 - 12:35When 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.
Visitor (not verified)
I'm getting errors using this
Sun, 2007/10/21 - 14:04I 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:
And here's the edited setup that I'm using...
Thanks for your help!