Backup and Maintenance for Drupal

Feargal O Kane's picture

Our backup and maintenance strategy is based around the five basic parts of Drupal:

  1. The core drupal codebase
  2. The database
  3. Contributed modules
  4. Contributed themes
  5. Files directory

Our site architecture means that 3,4 and 5 are all within the sites directory but when doing backups it's good to think of them as separate.

Master backup

Make a master backup when:

  1. The site is launched
  2. After you upgrade the site

The master backup is simply a copy of the entire web directory running your drupal site. The easiest way to do this is through using the command line:

Create a staging directory on the server, using

mkdir ~/backup

Move to this directory

cd ~/backup

Copy the entire directory running drupal

cp -pr /var/www/DRUPAL-INSTALL DRUPAL_INSTALL_SITE_DATE

-pr means that the entire directory will be copied retaining the permission levels you had previously set

Compress this file

tar cvf DRUPAL_INSTALL_SITE_DATE DRUPAL_INSTALL_SITE_DATE

gzip DRUPAL_INSTALL_SITE_DATE.tar

Copy this file from the server and place it in our backup directory on a separate server.

Core drupal codebase

You should only go near the core drupal codebase when upgrading to a newer version of Drupal. Instructions for upgrading are here (http://tech.nicva.org/content/upgrading-drupal-610).

Database Backups

Use the db_maintenance module (http://drupal.org/project/db_maintenance) to backup databases.

This module:

  1. optimises the database
  2. creates a backup of the database
  3. creates a backup of the file system
  4. emails an admin when this is completed

Upon initial setup it's a good idea to take a backup using phpmyadmin or navicat in case something happens when running the first optimisation. After you've taken this backup it's worth optimising all tables, logging the queries and attempting to repair if optimisation fails.

You can then view these log entries and make the decision about which tables require further optimisation over time - helping to make your site more efficient.

Make sure to create a backup directory for your files and database backups - it should be outside the site root /var/www.

Contributed modules and themes

Back up your contributed modules and themes before upgrading either. You should need to backup these directories less often than the database and files.

Depending on the structure of the site you can grab the entire sites directory and add it to an archive, but as the files directory is being backed up by db_maintenance it is better to grab the individual site modules and themes directories.

Move to your backup folder

cd ~/backup

Tar the themes folder

tar cvf contributed_themes.tar /var/www/DRUPAL_INSTALL/sites/all/themes

Gzip the tar

gzip contributed_themes.tar

Do the same for the modules folder

tar cvf contributed_modules.tar /var/www/DRUPAL_INSTALL/sites/all/modules

gzip contributed_modules.tar

In a multisite install you can then grab the directories from the individual site directories and add them to this directory.