Updated fullsiterestore.sh

#!/bin/bash
#
# fullsiterestore.sh v1.1
#
# Restore of website file and database content made with full site backup.
#
# A number of variables defining file location and database connection
# information must be set before this script will run.
# This script expects a compressed tar file (tgz) made by fullsitebackup.sh.
# Website files should be in a tar file named filecontent.tar, and database
# content should be in a sqldump sql file named dbcontent.sql. This script
# expects the sql to drop the table before readdding the data. In other words,
# it does not do any database preparation.
#
# ----------------------
# 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:
# tarfile # name of backup file to restore
#
#
# Database connection information
dbname="drupal" # (e.g.: dbname=drupaldb)
dbhost="localhost" #
dbuser="" # (e.g.: dbuser=drupaluser)
dbpw="" # database user password

# Website location
webrootdir="/var/www/drupal" # (e.g.: where you keep your drupal directory structure)
#
# Variables

# Execution directory (script start point)
startdir=`pwd` # return of pwd() populates statdir var
logfile=$startdir+"fullsite.log" # file path and name of log file to use

# Temporary Directory
datestamp=`date +'%Y-%m-%d'` # uses US format
tempdir=$datestamp

#
# Begin logging
#
echo "Beginning drupal site restore using \'fullsiterestore.sh\' ..." > $logfile

#
# Input Parameter Check
#

# If no input parameter is given, echo usage and exit
if [ $# -eq 0 ]
then
echo " Usage: sh fullsiterestore.sh {backupfile.tgz}"
echo ""
exit
fi

tarfile=$1

# Check that the file exists
if [ ! -f "$tarfile" ]
then
echo " Can not find file: $tarfile" >> $logfile
echo " Exiting ..." >> $logfile
exit
fi

# Check that the webroot directory exists
if [ ! -d "$webrootdir" ]
then
echo " Invalid internal parameter: webrootdir" >> $logfile
echo " Directory: $webrootdir does not exist" >> $logfile
echo " Exiting ..." >> $logfile
exit
fi

#
# Create temporary working directory and expand tar file
#
echo " Creating temp working dir ..." >> $logfile
mkdir $tempdir
cd $tempdir
echo " unTARing db and file tgz files ..." >> $logfile
tar xzf $startdir/$tarfile

#
# Remove old website files
#
echo " Removing old files from $webrootdir ..." >> $logfile
rm -r $webrootdir/*

#
# unTAR website files
#
echo " unTARing website files into $webrootdir ..." >> $logfile
cd $webrootdir
tar xf $startdir/$tempdir/filecontent.tar

#
# Load database information
#
cd $startdir/$tempdir
echo " Restoring database ..." >> $logfile
echo " user: $dbuser; database: $dbname; host: $dbhost" >> $logfile
echo "use $dbname; source dbcontent.sql;" | mysql --password=$dbpw --user=$dbuser --host=$dbhost

#
# Cleanup
#
echo " Cleaning up ..." >> $logfile
cd $startdir
sudo rm -r $tempdir

#
# Exit banner
#
endtime=`date`
echo "Restoration completed $endtime for $tarfile. " >> $logfile

Some manual work still needed

asbdpl - July 5, 2007 - 14:15

It might be worth mentioning, that the script - as it is - *does not* create a database user nor is able insert anything into MySQL, unless one prepares that *before* trying to run fullsiterestore.sh. The version below did not prompt me to enter a password with sufficent privileges:

ERROR 1044 (42000) at line 1: Access denied for user 'drupal_user'@'localhost' to database 'drupal_datbase'

To avoid this, (1) create the database in MySQL

mysqladmin -uroot -p create drupal_database

then (2) create the database user, and (3) grant the database user the necessary privileges as documented in INSTALL.mysql.txt (these are the usual steps to set up MySQL for a fresh Drupal installation).

I assume that nothing of this will be necessary if db user = mysql root, but this of cource should be avoided.

Also, when running the script as root in Debian "Etch", it fails reproducable:

fullsiterestore.sh: line 119: sudo: command not found

I think this is not too bad since it references just to the following instruction:

sudo rm -r $tempdir

However, it doesn't feel so good if a restore script exits with an error ;-)

Besides these small glitches the script seems to work fine and even leaves non-english characters alone (opposed to other methods of backing up Drupal databases). Also, it will help to save some time when backing up Drupal on a regular basis.

Thanks!

Regards, asb

Small typo

somnoliento - October 23, 2007 - 02:16

Line 42 has a small typo. It should be identical to fullsitebackup.sh:

logfile=$startdir"/fullsite.log" # file path and name of log file to use

(In some cases, the 'sudo' command in line 119 could be omitted)

German Umlaute

Christian Winte... - January 7, 2008 - 21:56

I wonder that no one from the german drupal friends talked about the difficulties caused by german "Umlaute". It must be obvious that you have problems during the process of backup and restore of mysql-databases with german "umlaute". I think that i am not the only one who have this problem:

backup your mysql-database with "mysqldump"
restore the dump with "mysql"

and after this you will find no german "umlaute" in the restored database but some other symbols....
Perhaps no one has restored ever a database??

utf8

aleix - April 18, 2008 - 19:37

Maybe will be helpful to explain that using
echo "SET NAMES utf8; use $dbname; source dbcontent.sql;" | mysql --password --user=$dbuser --host=$dbhost
will help some cases which have problems with the encoding.

 
 

Drupal is a registered trademark of Dries Buytaert.