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

#!/bin/bash

www_root="/var/www" # Set the WWW root location
compression="gzip" # 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

if [ ${1} ]; then
  declare -a domains=(${@})
else
  declare -a domains=(
testing.com
)
fi

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}.tgz"

# 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 and use multiple cores if pigz if available
if hash pigz 2>/dev/null; then
  echo "Pigz parallel compression detected!"
  tar -I pigz -cp --exclude=${backup_dest} --file=${backup_dest}/${archive} ${site} 2>/dev/null
else
  tar cjp --exclude=${backup_dest} --file=${backup_dest}/${archive} ${site} 2>/dev/null
fi

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

# Compress archives
case $compression in
  xz)
    hash xz 2>/dev/null || { echo "Missing xz compression support."; break; }

    echo "Compressing database and www root with xz..."
    xz -T 0 -z -9 -e ${backup_dest}/${dbname}
    xz -T 0 -z -9 ${backup_dest}/${archive}
    ;;
  lzma)
    hash lzma 2>/dev/null || { echo "Missing lzma compression support."; break; }

    echo "Compressing database and www root with lzma..."
    lzma -c ${backup_dest}/${dbname} >${backup_dest}/${dbname}.lzma &&
    rm ${backup_dest}/${dbname}
    lzma -c ${backup_dest}/${archive} >${backup_dest}/${archive}.lzma &&
    rm ${backup_dest}/${archive}
    ;;
  gzip)
    hash gzip 2>/dev/null || { echo "Missing gzip compression support."; break; }

    echo "Compressing database and www root with gzip..."

    # Check for pigz support
    if hash pigz 2>/dev/null; then
      tar -I pigz -cf ${backup_dest}/${dbname}.tgz ${backup_dest}/${dbname} 2>/dev/null &&
      rm ${backup_dest}/${dbname}
    else
      gzip -c ${backup_dest}/${dbname} >${backup_dest}/${dbname}.gz &&
      rm ${backup_dest}/${dbname}
    fi
esac

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

# Remove old backups >30 days
echo "Cleaning up of old archives..."
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.

likewhoa’s picture

I have committed a change to allow pigz parallel compression support. The benchmarks show that using gzip compression with pigz greatly increases compression time. Below are some benchmarks.

The file sizes are for archive and sql file.

Tar+Gzip

real 0m6.421s
user 0m8.292s
sys 0m1.026s

file size: 58353510 & 1585050

Tar+LZMA

real 0m33.503s
user 0m35.098s
sys 0m1.275s

file size: 53651169 & 863399

Tar+XZ

real 0m38.659s
user 0m39.922s
sys 0m1.722s

file size: 51614132 & 795128

Bending technology to fit business