Backing up the whole site

The following script integrates backing up the database with backing up the site code. Please note that descriptions place-hold for actual values in the script. Those actual values must be edited in, depending on your site configuration, for the script to work.

This script is adapted from the updated fullsitebackup.sh script created by Bristolguy on Drupal.org. This script is currently operational on Fedora 14, and has not been tested on other versions of Linux.

full_site_backup.sh
#! /bin/bash
#
# fullsitebackup.sh V1.1.20120118
#
# Full backup of website files and database content.
#
# Insure that this script IS NOT visible outside of the hosting account as
# it contains critical security information.
#
# A number of variables defining file location and database connection
# information must be set before this script will run.
# Files are tar'ed from the root directory of the website. All files are
# saved. The MySQL database tables are dumped without a database name and
# and with the option to drop and recreate the tables.
#
echo ""

# Configuration
dbname='database name, may be the database user name also'   # (e.g.: dbname=drupaldb)
dbuser='database user name, may be the database name also'  # (e.g.: dbuser=drupaluser)
dbpwd='database password'

# This first test below checks whether we are on the testing server or on the
# onlines server. In the document root of the testing server, one creates a
# directory called testing.server, and then locks it read only.
if [ -d 'path to document root'/testing.server ]; then
  echo "fullsitebackup.sh V1.1.20120118-sitename.test"
  dbhost='localhost'
  # Website Files
  webrootdir='path to document root on testing server'  # (e.g.: webrootdir=/home/user/public_html)
  # Default TAR Output File Base Name
  tarnamebase='sitename.test_backup-';
else
  echo "fullsitebackup.sh V1.1.20120118-sitename"
  dbhost='database hostname, not an ip address preferably'
  # Website Files (e.g.: webrootdir=/home/user/public_html)
  webrootdir='path to document root on online server'
  # Default TAR Output File Base Name
  tarnamebase='sitename_backup-';
fi

#
# Variables
#
# Form rest of name as a time stamp.
datestamp=`date +'%Y-%m-%d-%R'`
# Convert colon so that Windows won't complain.
datestamp=`echo $datestamp | sed "s/://"`

# Get in Execution directory (script start point)
pushd 'backup script directory, where archives are stored'
startdir=`pwd`

# Temporary Directory
if [ ! -d tmpbckdir$datestamp ]; then
  tempdir=tmpbckdir$datestamp
fi

#
# Input Parameter Check
#
if test "$1" = ""
  then
    tarname=$tarnamebase$datestamp.tgz
  else
    tarname=$1
fi

#
# Create temporary working directory
#
echo " .. Setup"
mkdir $tempdir
echo "    done"

#
# TAR website files
#
echo " .. TARing website files in $webrootdir"
cd $webrootdir
tar cf $startdir/$tempdir/filecontent.tar .
echo "    done"

#
# sqldump database information
#
echo " .. sqldump'ing database:"
echo "    user: $dbuser; database: $dbname; host: $dbhost"
cd $startdir/$tempdir
mysqldump -p$dbpwd --user=$dbuser --host=$dbhost --add-drop-table $dbname > dbcontent.sql
echo "    done"

#
# Create final backup file
#
echo " .. Creating final compressed (tgz) TAR file: $tarname"
tar czf $startdir/$tarname filecontent.tar dbcontent.sql
echo "    done"

#
# Cleanup
#
echo " .. Clean-up"
cd $startdir
rm -r $tempdir
echo "    done"

#
# Exit banner
#
echo " .. Full site backup complete"
echo ""
popd