Backup and restore using bash shell scripts

Last modified: April 2, 2007 - 19:35

The two Bash shell scripts presented here work together automating the Drupal backup and restore process. They work "as is" for simple installations and can be customized for multisite or non-standard installations.

The scripts have two uses:

  1. Creating Drupal backups which can be restored to their original location
    e.g., restore from a failed upgrade attempt or recover from a disk failure
  2. Staging data between Drupal configurations
    e.g., backup a production instance and restore to a test instance

These scripts have been used successfully on and between Linux systems and on Windows systems using Cygwin.

Functional Overview

A full Drupal backup requires data backups from both the database and file system. These two backups must then be tied to each other.

The backup script performs a file system backup using the tar command and a database backup using the mysqldump utility. The two backups are then combined into a single tar file. The tar is compressed and its file name includes the backup date.

The restore script reverses the process by unpacking the file and database backups from the container tar file and applying them to their respective targets.

The Drupal instance backed up or restored is specified by variables defined in the backup and restore scripts.

Usage notes and cautions

Detailed usage notes are provided for each script. The following points bear highlighting:

  • The restore script has no safeguards against accidental restore. If you don't understand how it can destroy your system you are encouraged NOT to use it.
  • The backup script backs up every file and directory in the Drupal root directory. If you have a hosted site with subdomain websites contained in subdirectories a backup of the main website directory will include the subdomain websites. For backups this will only bloat your backup file. For restores this could be disastrous, since you would restoring the file systems for ALL of the website. If you have this kind of configuration you must customize the tar command to make a selective backup.
  • These scripts make no provision to lock out users while the backup or restore is in progress.
  • This script does not understand Drupal multisite configurations
  • The backup script requires read access to all files/directories and the restore script requires read/write access. This is sometimes a problem in hosted configurations.
  • Because the backup and restore is done in stages enough temporary space must be available for the intermediate files
  • Some commands used in the scripts will vary between operating systems. You may have to tailor commands for your operating system.

Warning!

Ningbo1 - April 26, 2007 - 12:46

Despite the fact that the scripts are presented as .sh files, in reality, the first line of each script is #!/bin/bash, making them .bash scripts instead. Be careful, otherwise you'll waste a lot of time trying to figure out why they won't work on a Solaris or BSD system.

An alternative script

nicholasThompson - November 27, 2008 - 22:11

I wrote an alternative script on my Blog which "intelligently" dumps certain tables as structure only. Tables such as cache, session and watchdog do not really need a full backup and can sometimes bloat a dump file by up to 100%. You can find it on my blog, how to backup a drupal database.

Re: another improvement

starkos - March 10, 2009 - 18:42

A script, like nicholasThompson's, that skips temporary and cache tables, and also works around a MySQL issue that will corrupt the anonymous user record: http://industriousone.com/drupal-backup-and-restore

Drupal management scripts.

toddgee - March 20, 2009 - 04:09

Checky!

http://svn.toddgee.com/drupalScripts

I have written a comprehensive suite of fully documented bash scripts for the maintenance of a collection of Drupal sites. See esp. the README file for some best-practices guidelines. All these scripts are in heavy use by multiple developers who run many, many drupal sites. Please let me know if you find them useful.

Yet Another Drupal Dev company!
http://chicagodrupalauthority.com/

 
 

Drupal is a registered trademark of Dries Buytaert.