Complete backup scripts for my websites (Drupal, MySQL)

I’ve been spending some time lately trying to automate the process of backing up my websites, and in doing so I thought I would share the Linux shell scripts that I use to generate the backup files, including backups of my MySQL databases and Drupal website directories. If you are comfortable with shell programming in Linux, I think you’ll be able to follow the code in the following scripts.

MySQL database backup script

First, this is a backup script I use to backup a MySQL database using mysqldump:

#!/bin/sh

#----------------------------------------------------
# a simple mysql database backup script.
# version 2, updated March 26, 2011.
# copyright 2011 alvin alexander, http://devdaily.com
#----------------------------------------------------
# This work is licensed under a Creative Commons 
# Attribution-ShareAlike 3.0 Unported License;
# see http://creativecommons.org/licenses/by-sa/3.0/ 
# for more information.
#----------------------------------------------------

# (0) don't run if we're not given a shell script dir
if [ -z "$1" ]
then
  echo "Usage: this-script.sh shellScriptDir"
  exit -1
fi
SHELL_SCRIPT_DIR=$1

# (1) get all variables we need
cd $SHELL_SCRIPT_DIR
. shell-script-vars

# (2) move to the backup output directory
cd $BU_DIR

# (3) in case you run this twice in one day, remove the previous version of the file
unalias rm        2> /dev/null
rm ${DB_FILE}     2> /dev/null
rm ${DB_FILE}.gz  2> /dev/null

# (4) do the mysql database backup (dump)

# (a) use this command for a database server on a separate host:
mysqldump --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > ${DB_FILE}

# (b) use this command for a database server on localhost. add other options if need be.
#mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${DB_FILE}

# (5) gzip the mysql database dump file
gzip $DB_FILE

# (6) show the user the result
echo "${DB_FILE}.gz was created:"
ls -l ${DB_FILE}.gz

Website directories backup script

Next, this is the script I use to back up my website (Drupal) directories:

#!/bin/sh

#----------------------------------------------------
# a simple script to make a tar/gz website backup.
# version 1, updated July 13, 2011.
# copyright 2011 alvin alexander, http://devdaily.com
#----------------------------------------------------
# This work is licensed under a Creative Commons 
# Attribution-ShareAlike 3.0 Unported License;
# see http://creativecommons.org/licenses/by-sa/3.0/ 
# for more information.
#----------------------------------------------------

# (0) don't run if we're not given a shell script dir
if [ -z "$1" ]
then
  echo "Usage: this-script.sh shellScriptDir"
  exit -1
fi
SHELL_SCRIPT_DIR=$1

# (1) get our configuration variables
cd $SHELL_SCRIPT_DIR
. shell-script-vars

# (2) in case you run this twice in one day, remove the 
#     previous version of the file
unalias rm                   2> /dev/null
rm ${BU_DIR}/${WWW_BU_FILE}  2> /dev/null
rm ${BU_DIR}/${WWW_BU_FILE}  2> /dev/null

# (3) go to the 'www' directory you want to backup; we'll run
#     the tar command from there
cd $WWW_DIR

# (4) create the backup file
tar czf ${BU_DIR}/${WWW_BU_FILE} .

# (5) show the user the result
echo "${BU_DIR}/${WWW_BU_FILE} was created:"
ls -l ${BU_DIR}/${WWW_BU_FILE}

Shared data for shell scripts

Finally, I found it easiest to break the data that is used by multiple shell scripts into a file named shell-script-vars. That file contains contents like this:

#----------------------
# vars for both scripts
#----------------------
THE_DATE=`date +"%Y%m%d"`

# where the backup files will be written
BU_DIR=/path/to/where/you/keep/your/backups

# create files like 'vpp-sql.tgz' and 'vpp-www.tgz'
OUTPUT_FILE_PREFIX=vpp

#---------------
# db backup vars
#---------------
DB_FILE=${OUTPUT_FILE_PREFIX}-sql.${THE_DATE}
DBSERVER=yourServerNameHere(like localhost)
DATABASE=yourDatabaseName
USER=yourDatabaseUsername
PASS=yourDatabasePassword

#----------------
# www backup vars
#----------------
WWW_DIR=/your/website/root/directory
WWW_BU_FILE=${OUTPUT_FILE_PREFIX}-www.${THE_DATE}.tgz

Comments

One of the best things about these scripts is the use of a common file for shared data. Implementing this is simple:

  1. Create a file with the shared data, using sh or bash syntax, as desired.
  2. Include the shared data in your other scripts by “sourcing” it with the . command, as in . shell-script-vars.

Everything else is a matter of moving to the correct directories and issuing the right commands.