Darren Mothersele

Software Developer

Warning: You are viewing old, legacy content. Kept for posterity. Information is out of date. Code samples probably don't work. My opinions have probably changed. Browse at your own risk.

Easy Drupal Backup with Rsync and MySQLDump

Jul 25, 2008

web-dev

Here's a nice easy way of running backups for your Drupal sites. To successfully backup a Drupal website you need to:

  1. backup all the files, themes, modules, etc, and,
  2. backup the database content

I'll not go into the reasons why backups are essential part of support for Drupal, or any websites, because if you've ever had a server crash and die on you then you will understand. If you haven't then I suggest it's only a matter of time, and you start a rigorous backup routine now.

Read on for one backup method that produces nice incremental backups of your Drupal site without taking up too much diskspace, and dumps SQL content of your Drupal database at the same time.

Backing up the Drupal files on your web-server

You could just backup your site/ folder, which if Drupal is configured correctly should contain all your customisations for your site. Or, an easy alternative is to just backup the whole of your htdocs directory.

Making a full backup is a length process, and takes up more space than it needs to. That's why I suggest using an 'incremental' backup process, such as the one recommended by Mike Rubel. This involves just saving the changes since the last backup. Rsync is a perfect tool for achieving this, it is included in most Linux distributions (or easy to find at least), and works very efficiently over networks (which is good for FTP backups if you want to go down that route).

The process is as follows:

rm -rf backup.3
mv backup.2 backup.3
mv backup.1 backup.2
cp -al backup.0 backup.1
rsync -a --delete source_directory/  backup.0/

If you run this every day, the result will be folders called backup.0, backup.1, backup.2, and backup.3 - which look like full backups for today, yesterday, and the two days before that. What is actually stored on disc is one folder with the full latest backup, and then folders containing just the files that changed on the previous days.

This backup script should be created in a .sh file, set to executable and then included in your daily crontab.

Backing up the database

Now you have the backups of your files, all that's left to do is backup your database. Here's the command line you need to create a backup:

mysqldump -u username -p password database > filename.sql

You should then compress this file by using the following command:

gzip filename.sql

You can pipe the two commands together and add them to your crontab to execute daily.

The database backup could also be extended to keep snapshots from the previous days by adding a file rotation to the script as with the rsync backup, for example:

rm -rf databasebackup.3.sql
mv databasebackup.2.sql databasebackup.3.sql
mv databasebackup.1.sql databasebackup.2.sql
mv databasebackup.0.sql databasebackup.1.sql
mysqldump -u username -p password database > databasebackup.0.sql