Total: £0.00

Automated Encrypted Backups of Drupal files and database from Ubuntu 10.04 in Amazon EC2 to Amazon S3

By marksams, 06/23/2011 - 00:00

Following on from my simple article about manually backing up a Drupal site, it's now time to get automated....




My first port of call was Backup and Restore using bash shell scripts on Drupal.org from which I selected to try out the backup scripts posted by starkos. These scripts seem well thought out and with a helpful write-up plus comments. I tested the backup script which worked first time.

Next I needed a way to take my site offline automatically as Starkos warns "Be sure to take your site offline first, to avoid capturing an update in progress". This sounds like good advice for the database backup, and I didn't want to hang around to do this manually at some off-peak hour.

Following some reading about drush on drupal.org, I found a nice introduction to Drush: The Drupal Power Tool - this gave me the information I needed:

drush vset site_offline 1 --yes     Put a site into maintenance mode (D6 only)

drush vset maintenance_mode 1 --yes     Put a site into maintenance mode (D7 only)

drush vset site_offline 0 --yes     Take a site out of maintenance mode (D6 only)

drush vset maintenance_mode 0 --yes     Take a site out of maintenance mode (D7 only)

Then at http://groups.drupal.org/drush/commands I discovered that not only could I backup/restore the database using the drush SQL commands, the Backup and Migrate module also had drush integration.


I knew from my manual backup process that I could already use a simple "tar -czpf ~/somefile.tar.gz /var/www/mywebsite" command to back up my files.

I had also found a nice simple tutorial for file backups on the Ubuntu help pages.

Moving files between my Amazon EC2 Server and Amazon S3

The s3tools, especially s3cmd, provide a nice simple way to encrypt a backup file (database, files, or both) and copy it to an s3 bucket.

Review Requirements

Seemingly spoilt for choice (for database backups at least), I had to review my requirements...

My primary requirement is for a simple-as-possible nightly backup of production sites, that would allow me to restore the sites (database and files) in the event of disaster.

I had no requirement for version control or comparing differences between backups.

I wanted encrypted backups in an s3 bucket.

At first, my preference was also to keep my server configuration minimal (reference Ubuntu 10.04 LAMP server on Amazon EC2 Quick Start), and handle things with Drupal if possible. At this point I was very tempted to go with the Backup and Migrate Module with the Backup and Migrate Files plugin, but by now I had realised the full potential of drush and s3cmd, which would almost certainly be utilised beyond this backup process.


Final Decision

I decided not to use the modules (which are probably perfect for a shared hosting setup) and go with a combination of the afore mentioned options.

Regarding the server configuration, I already had my server's crontab configured to run drupal's cron.php, so I just added an extra cron command to run a backup script. I didn't mind installing drush and s3cmd (instructions below).

...and rather than edit the backup script provided by starkos (to keep this interchangeable), and to include the relevant commands for drush and s3cmd, I created a wrapper script that does the following:

  1. Execute the drush command to take the site offline: drush vset site_offline 1 --yes
  2. Run the database backup script by starkos (specifying .mysql file outside of the web root)
  3. Create a tar archive of my sites files and the .mysql file

    • Specifying .tar.gz file outside of the web root
    • Adding the day of the week to the file name
  4. Execute the drush command to bring the site back online: drush vset site_offline 0 --yes
  5. Copy the combined tar archive of database and files to my s3 bucket

    • Specifying -e for GPG encryption
    • Specifying -c /home/ubuntu/.s3cfg for the benefit of cron
  6. Delete all temporary files, leaving just my single backup file in my s3 bucket


For my script to work, I had to pre-install Drush and s3cmd, as described in my other knowledge base articles:

My script also depends upon starkos' backup.sh - with my script residing next to his in my Drupal sites/default folder. This keeps things simple for the $1 parameter, which is passed through to starkos' script (as well as used for building the file name for the tar archives) and drush, which likes to be run from within the Drupal directory. It also assumes your site is in /var/www.

Running this script from cron required a few changes, mostly hardcoding paths to binaries and config files. Because the script(s) need to be run in the /sites/default directory, I required a lengthy cron command (replace mywebsite.com with your web site directory):

0 0 * * * cd /var/www/mywebsite.com/sites/default && /bin/bash /var/www/mywebsite.com/sites/default/s3backup.sh mywebsite.com

If running manually from the command line, you can just use "sudo bash s3backup.sh mywebsite.com" when in the sites/default directory.

My Script

You can download my backup script here.



By billmegelich on

Great post....really love to read you blog post and how you have many to write it. Today through this comment comment I am share you information post about Bill Megelich and I think you will really love it after reading it.