Backup your Drupal sites with Drush and Git

Git is great for code development! But it can also be used for backing up your sites! Here is how I do it. I wrote a script leveraging Drush to dump the database(s) of your sites and then commit all these changes to a git repository.

cd /var/sites/multi6/

drupal/sites/default
drupal/sites/example1.org
drupal/sites/example2.com

database/default.sql
database/example1.org.sql
database/example2.com.sql

.git/

Here is the script:

<?php
#!/bin/bash

# This script backups the databases of all the sites found in the
# Drupal sites folder as well as the Drupal code base and sites files.

DRUSH='/Users/scor/.drush/drush/drush.php'

# Absolute path to the backup directory
BACKUP='/Users/scor/drupal_sites/multi6'
# Absolute path to the root folder of the Drupal code base.
DRUPAL_ROOT='/Users/scor/htdocs/d6ei'


for site in $( ls "$DRUPAL_ROOT/sites" )
do
 
site_path="$DRUPAL_ROOT/sites/$site"

 
# Selects directories which contain settings.php (excluding symbolic links)
 
if [ -d $site_path -a -f "$site_path/settings.php"  -a ! -L $site_path ]; then
   
echo "Dumping database for $site"
   
$DRUSH -r $site_path sql dump --ordered-dump --structure-tables-key=structure-tables > "$BACKUP/database/$site.sql"
   
echo "Done"
 
fi
done

# ensure all the files (even the new ones) get added to the git repository
cd $BACKUP
git add database
/production.sql
git add drupal
/

# commit every change
git commit -am "auto-commit changes database and files"
?>

Comments

Add new comment