Backup and Restore Site Snapshots in Drupal with Drush

If you use Drupal on any kind of regular basis, Drush is your friend. It's a command-line utility available for all platforms (including Windows as of version 5!) that can do sometimes-seemingly innumerable tasks for you. I've been using it for installing, enabling, and disabling modules and themes; clearing the Drupal cache; and running code and database updates for years now, but that's just scratching the surface.

I've recently been working on a beginner course on Drupal, sort of a companion to the recently-released WordPress course. I had to make it more project-oriented since Drupal doesn't have as clear an out-of-the-box use case as WordPress, which meant I had to be more careful about what I recorded and when. The videos generally had to progress toward the completed site, but I didn't want to be restricted to having to record them in the order they'd finally be presented.

What I wanted to do was take snapshots of the site I was building, and be able to revert to previous versions easily. Drush ended up being a huge help for this.

To make the snapshots, I used

drush sql-dump
, which outputs a backup of your database directly to your screen. I used the following command to save the snapshots as compressed files in a backup directory:

drush sql-dump | gzip > ../backup/FILENAME.sql.gz

The command runs

drush sql-dump
, sends the output through gzip to compress it, and saves the result to the named file in a directory called "backup" one level above my Drupal installation, alongside public_html and safe from the web server. Of course, I included some meaningful information in the filenames so I could figure out what to restore later.

Then to restore, I needed to decompress the file of interest and feed it into MySQL. Here's the command:

gunzip < ../backup/FILENAME.sql.gz | drush sqlc

The order of these operations looks a little funny if you're not used to how Unix redirects work (it took me some fiddling to get it — it's been a few years since I used this sort of thing on a regular basis). What happens is: you feed the compressed database file into gunzip to decompress it, then run everything through

drush sqlc
. Run on its own,
drush sqlc
opens a MySQL console, but in this case it executes the piped MySQL commands from the database file.

I could save and restore snapshots easily by reusing the commands above, which took just a couple seconds to execute each time. This is much easier than saving backup files using phpMyAdmin or even the mysql command-line client, since Drush can load my database credentials from the settings.php file. This procedure didn't touch any of the files I'd uploaded in the course of the recording, but because Drupal stores so much configuration in the database along with the content, I could pretty much restore the site to any point I wanted, going forward and backward through the site-in-progress's timeline.

Drush can take a bit of effort to install and get used to if you're not used to the command line, but I think it pays off pretty quickly, even for relatively simple operations. And once you start getting into more advanced commands, you can really start to have your mind blown, or at least be pretty well impressed, at its usefulness.



Mauro Mazzerioli

Works like a charm. Thanks!


I'll just leave this here:

drush bam-backup db

Seen xkcd's #1205 yet? You're welcome.

Source: Only 10 Types

Joe Chellman

Backup and Migrate is a fine solution for what it does, but of course using it means you've installed yet another module, which might be overkill (and would have been in this case for me).

I just needed to take and restore database snapshots, and for that, drush by itself makes it easy enough that I didn't need another module.

Comments are closed on this post to keep spammers at bay. If you want to chime in, please email or send a tweet.