Linux/Mac – Backing up Drupal Databases

I manage several servers that are hosts to a variety of Drupal 6 & 7 sites, and I needed a script to intelligently dump each site’s database. Enter drupal_db_dump.  A  script I wrote in Ruby that uses drush (command line tool to manage Drupal) to do exactly just that. This script is best run via a cron job and takes two or three parameters: a Drupal sites directory, a directory where you’d like the dumps to be stored, and an optional archive toggle. Here’s what it will do:

  1. Go into every site in the specified sites directory and dump each database to a dated folder inside the dump directory (organized by site).
  2. Create an md5 checksum of the dump and write it to a file inside the dated folder.
  3. Compress the dated folder, and remove the original.

I also included an optional archiving functionality. If specified (-a), it will purge all but the last backup for the previous month when a new month begins. The last backup is moved to a folder called monthly_archives. By default, this functionality is turned off.

Benefits of use:

  1. Eliminates the need to configure a separate backup for each site,  as it will traverse all sites in the given directory.
  2. Quicker to setup, as you do not need to configure a regular and MySQL user. Drush will use the information in the site’s settings.php file to dump the database.
  3. Checksums!

Caveats:

  1. If the backups for a site span more than two months, this script will not attempt to archive them. This is by design. You will need to manually move or remove the backups until they only span one or two months.