Restoring the whole site to a working state from an online site to a testing server, or from a testing server to an online site is a bit trickier. One has to account for differences in the server configurations. This leads to the locking of certain files on both the testing server and online server so that accidental updating doesn't occur. These files are usually the CMS configuration file, and .htaccess in the document root.
Hence in the following script, areas are left out that would be too specific to certain installations to be generically useful.
full_site_restore.sh
#! /bin/bash
# Shell script for restoring a CMS site.
# This should have the capability of being silent.
#
echo "RESTORE CMS SHELL SCRIPT"
echo "(c) - 2011,2012 - Alfred P. Reaud, Happy Cat Technologies"
echo "Released under the GNU-GPL V.2"
if [ -z $1 ]; then
echo "Unable to continue, no website archive file specified."
echo "Usuage: ./full_site_restore.sh archive_filename"
exit;
fi
# This second 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
siteroot='path to document root on testing server'
onlineserver=false;
else
siteroot='path to document root on online server'
onlineserver=true;
fi
sitebackup=$siteroot'backup'
pushd $sitebackup
# Check for temp folder.
if [ -d temp ]; then
# If it's there, empty it, VERY CAREFULLY!
echo "Emptying directory temp in "$sitebackup
cd temp
current_dir=`pwd`
should_be=$sitebackup'/temp'
if [ $current_dir != $should_be ]; then
echo "*** Can't continue! Not in proper dictory. ***"
echo "Should be in directory "$should_be
echo "But I find myself in "$current_dir
exit;
fi
rm -rf *;
else
echo "Creating directory temp in "$sitebackup
mkdir temp
cd temp;
fi
tar --no-same-owner -xzf ../$1
# Create html folder
if [ ! -d html ]; then
echo "Creating directory html in "$current_dir
mkdir html;
fi
# Jump into it, and extract site archive into it, checking for errors.
cd 'document root'
echo "EXTRACTING WEBSITE TO TEMP FOLDER..."
tar --no-same-owner --atime-preserve -xf ../filecontent.tar
# Copy site to actual html folder updating and creating were necessary.
# Remember that wp-config.php and .htaccess MUST BE MODIFIED MANUALLY between
# the testing server and the online site. Those two document root files should
# be manually copied, edited as necessary, and then locked to prevent
# accidental modification during backup, which will result in site breakage in
# most instances due to configuration differences between the testing server
# and the online server.
echo "UPDATING WEBSITE..."
cp -ruv . 'path to document root'
# Change file ownership to default ownsership if necessary.
cd ..
# Finally restore the database.
../restore_db.sh dbcontent.sql
# It may be necessary to fix the ownership and permissions of files and folders
# here via a small script that is called for that purpose. Whether or not that
# is necessary depends on the configuration of the system.
popd
echo "DONE WITH SITE RESTORE!"