Last updated March 29, 2015. Created on July 25, 2011.
Edited by petu, likewhoa. Log in to edit this page.

#!/bin/bash

www_root="/home" # Set the WWW root location
compression="xz" # Set the compression method for site archive and db

# Declare a list of domains to backup, and in the case of a non standard drupal root
# we specify it next to the domain name. See examples below

declare -a domains=(
drupal.org
kerneltrap.org:www
missionaccomplish.com
)

site_prepare() {

# Iterate through the available domains to backup along with their custom drupal root location
((n_elements=${#domains[@]}, max_index=n_elements - 1))

for ((i = 0; i <= max_index; i++)); do
  local site="${www_root}/${domains[i]/:*/}" drupal_root="${www_root}/${domains[i]/*:/}"

  # Skip domains that don't have a custom drupal root location
  if [ ${site} = ${drupal_root} ]; then
    drupal_root="${site}/htdocs"
  else
    drupal_root="${www_root}/${domains[i]/:*/}/${domains[i]/*:/}"
  fi
 
  site_backup
done
}

site_backup() {

backup_dest="${site}/backups" # A directory called backups inside home                                                                                                                                                                       
date=$(date +%Y%m%d-%H%M)
dbname="db-backup-${date}"
archive="backup-${date}.tar.bz2"

# Make sure our home and backup directories are available
if [ ! -d ${site} ]; then
  echo "Site directory is missing. exiting script"; exit 1
elif [ ! -d ${backup_dest} ]; then
  echo "Backup directory is missing in ${site}. exiting script"; exit 1
else
  echo "Starting ${site} backup..."
fi

# Put site in maintenance mode
drush -r ${drupal_root} vset --always-set maintenance_mode 1

# Compress the site directory
tar cjp --exclude=${backup_dest} --file=${backup_dest}/${archive} ${site} 2>/dev/null

# Dump our DATABASE
drush -r ${drupal_root} sql-dump >${backup_dest}/${dbname}

# Disable maintenance mode.
drush -r ${drupal_root} vset --always-set maintenance_mode 0

# Compress archives
case $compression in
  xz)
    hash xz || { echo "Missing xz compression support. Exiting"; exit 1; }
   
    echo "Compressing DB..."
    xz -z -9 -e ${backup_dest}/${dbname}

    echo "Compressing www root..."
    xz -z -9 ${backup_dest}/${archive}
    ;;
  lzma)
    hash lzma || { echo "Missing lzma compression support. Exiting"; exit 1; }

    echo "Compressing DB..."
    lzma -c ${backup_dest}/${dbname} >${backup_dest}/${dbname}.lzma && rm ${backup_dest}/${dbname}

    echo "Compressing www root..."
    lzma -c ${backup_dest}/${archive} >${backup_dest}/${archive}.lzma && rm ${backup_dest}/${archive}
    ;;
  gzip)
    hash gzip || { echo "Missing gzip compression support. Exiting"; exit 1; }

    echo "Compressing DB..."
    gzip -c ${backup_dest}/${dbname} >${backup_dest}/${dbname}.gzip && rm ${backup_dest}/${dbname}
esac

# Remove old backups >30 days
find ${backup_dest} -mtime +30 -exec rm {} \; >> /dev/null 2>&1

echo "Backup for ${site} created"
}

site_prepare

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

petu’s picture

This script drops site in maintanance mode for too long period. Especially in case of a big site.
What do you think about setting maintenance to 0 after 'drush sql-dump' line? It may save a lot of unoperable time.
Compressor uses just single core for compressing the files. It should be better to add multicore suport.