Updated fullsitebackup.sh
Last modified: February 27, 2009 - 18:06
#!/bin/bash
#
# fullsitebackup.sh V1.1
#
# Full backup of website files and database content.
#
# 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.
#
# ----------------------
# March 2007 Updates
# - Updated script to resolve minor path bug
# - Added mysql password variable (caution - this script file is now a security risk - protect it)
# - Generates temp log file
# - Updated backup and restore scripts have been tested on Ubunutu Edgy server w/Drupal 5.1
#
# - Enjoy! BristolGuy
#-----------------------
#
## Parameters:
# tar_file_name (optional)
#
#
# Configuration
#
# Database connection information
dbname="drupal" # (e.g.: dbname=drupaldb)
dbhost="localhost"
dbuser="" # (e.g.: dbuser=drupaluser)
dbpw="" # (e.g.: dbuser password)
# Website Files
webrootdir="/var/www/drupal" # (e.g.: webrootdir=/home/user/public_html)
#
# Variables
#
# Default TAR Output File Base Name
tarnamebase=sitebackup-
datestamp=`date +'%m-%d-%Y'`
# Execution directory (script start point)
startdir=`pwd`
logfile=$startdir"/fullsite.log" # file path and name of log file to use
# Temporary Directory
tempdir=$datestamp
#
# Input Parameter Check
#
if test "$1" = ""
then
tarname=$tarnamebase$datestamp.tgz
else
tarname=$1
fi
#
# Begin logging
#
echo "Beginning drupal site backup using fullsitebackup.sh ..." > $logfile
#
# Create temporary working directory
#
echo " Creating temp working dir ..." >> $logfile
mkdir $tempdir
#
# TAR website files
#
echo " TARing website files into $webrootdir ..." >> $logfile
cd $webrootdir
tar cf $startdir/$tempdir/filecontent.tar .
#
# sqldump database information
#
echo " Dumping drupal database, using ..." >> $logfile
echo " user:$dbuser; database:$dbname host:$dbhost " >> $logfile
cd $startdir/$tempdir
mysqldump --user=$dbuser --password=$dbpw --add-drop-table $dbname > dbcontent.sql
#
# Create final backup file
#
echo " Creating final compressed (tgz) TAR file: $tarname ..." >> $logfile
tar czf $startdir/$tarname filecontent.tar dbcontent.sql
#
# Cleanup
#
echo " Removing temp dir $tempdir ..." >> $logfile
cd $startdir
rm -r $tempdir
#
# Exit banner
#
endtime=`date`
echo "Backup completed $endtime, TAR file at $tarname. " >> $logfile
Connect to remote host
I set this up, and the first time I tried to run it this error came up
mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) when trying to connect
Fix: Replace --host=$dbhost to appear in mysqldump command
FROM:
## sqldump database information
#
echo " Dumping drupal database, using ..." >> $logfile
echo " user:$dbuser; database:$dbname host:$dbhost " >> $logfile
cd $startdir/$tempdir
mysqldump --user=$dbuser --password=$dbpw --add-drop-table $dbname > dbcontent.sql
TO:
## sqldump database information
#
echo " Dumping drupal database, using ..." >> $logfile
echo " user:$dbuser; database:$dbname host:$dbhost " >> $logfile
cd $startdir/$tempdir
mysqldump --user=$dbuser --host=$dbhost --password=$dbpw --add-drop-table $dbname > dbcontent.sql
Running under CentOS with cron
I had a hard time getting this script to run properly using CentOS 4 and cron. Specifically the filecontent.tar archive was not being included in the file sitebackup.tgz file. I was able to get things working with a few modifications.
I changed:
mkdir $tempdirTo:
/bin/mkdir $startdir/$tempdirI thought this might be a helpful tip if other people are running into this problem.
htaccess
This backup script does not backup the .htaccess file - you need this to do a restore quickly.