Community Documentation

fullsiterestore.sh

Last updated March 25, 2010. Created by dale42 on April 19, 2006.
Edited by esmerel, bryan kennedy, cel4145. Log in to edit this page.

Copy this text to a file named fullsiterestore.sh. Please ensure that special characters, such as backtick ("`") and quotes, are saved correctly.

#!/bin/bash
#
# fullsiterestore.sh v1.0
#
# 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.
#
# Parameters:
#    tar_file_name

#
# Configuration
#

# Database connection information
  dbname={DB Name}     # (e.g.: dbname=drupaldb)
  dbhost=localhost
  dbuser={DB Username} # (e.g.: dbuser=drupaluser)

# Website Files
  webrootdir={Directory Path}  # (e.g.: webrootdir=/home/user/public_html)


#
# Variables
#

# Execution directory (script start point)
  startdir=`pwd`

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


#
# Banner
#
echo ""
echo "fullsiterestore.sh v1.0"


#
# 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"
    echo ""
    exit
fi   

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


#
# Create temporary working directory and expand tar file
#
echo " .. Setup"
mkdir $tempdir
cd $tempdir
tar xzf $startdir/$tarfile
echo "    done"


#
# Remove old website files
#
echo " .. removing old files from $webrootdir"
rm -r $webrootdir/*
echo "    done"


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


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


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


#
# Exit banner
#
echo " .. Full site restore complete"
echo ""

If you have problems with character encoding you could modify the script with

echo "SET NAMES utf8; use $dbname; source dbcontent.sql;" | mysql --password --user=$dbuser --host=$dbhost
nobody click here