Last updated April 16, 2013. Created by likewhoa on July 25, 2011.
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}
# 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
# Disable maintenance mode.
drush -r ${drupal_root} vset --always-set maintenance_mode 0
# Remove old backups >30 days
find ${backup_dest} -mtime +30 -exec rm {} \; >> /dev/null 2>&1
echo "Backup for ${site} created"
}
site_prepare