Hello. When I was running the 2001everest.com web site, I used the
following shell script to mirror the web tree from one host to another,
using ssh and scp. It worked quite well. I'll send a copy of the
dobackup_initbdir script under a separate message.
Note that you have to use ssh's allowed_hosts feature for this to work so
that the script doesn't cause ssh to prompt you for passwords each time it
runs.
-Brian
#!/bin/sh
#$Id: mirror.sh,v 1.4 2001/05/13 16:15:41 buhrow Exp $
#NAME: Brian Buhrow
#DATE: May 10, 2001
#PURPOSE:
#This script compares the checksums of all the files in a given tree with
#a stored set of checksums, and transfers any files which have changed to
#the remote location.
#
#On the day specified in "COPYDAY", the entire tree is copied regardless
#of the state of the checksums. This is to guard
#against possible checksum matches on two separate permutations of a file.
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin; export PATH
#Define some constants here
LOGFILE="/var/www/everest/logs/mirror.log"
COMPFILE="/var/www/everest/logs/checksums"
CURSUM="/var/www/everest/logs/checksums.cmp"
TOPDIR="/var/www/everest/docs"
REMOTESITE="audio.nfbcal.org:/usr/local/httpd/everest/docs"
REMOTEMACHINE="audio.nfbcal.org"
REMOTEDIR="/usr/local/httpd/everest/docs"
INITDIR=/usr/local/sbin/dobackup_initbdir
COPYDAY="Sun"
do_compares="false"
#Now, actually do some work
cd $TOPDIR
today=`date +%a`
touch $LOGFILE
date >> $LOGFILE
if [ -f /var/tmp/mirror.lock ]; then
echo "Script already running -- Please delete /var/tmp/mirror.lock to fix..."
>> $LOGFILE
exit 0
else
touch /var/tmp/mirror.lock
fi
if [ X"${COPYDAY}" = X"${today}" ]; then
if [ ! -f /var/tmp/copy_already_done ]; then
echo "Today is $COPYDAY -- copying entire tree..."
echo "Today is $COPYDAY -- copying entire tree..." >> $LOGFILE
echo "tar fc - . |ssh $REMOTEMACHINE '(cd $REMOTEDIR; tar fxp -)'" >>
$LOGFILE
tar fc - . |ssh $REMOTEMACHINE "(cd $REMOTEDIR;tar fxp -)" >> $LOGFILE
2>&1
touch /var/tmp/copy_already_done
else
do_compares="true"
fi
else
rm -f /var/tmp/copy_already_done
do_compares="true"
fi
if [ X"${do_compares}" = X"true" ]; then
if [ ! -s $COMPFILE ]; then
echo "Calculating initial checksums for $COMPFILE..."
echo "Calculating initial checksums for $COMPFILE..." >> $LOGFILE
find . -type f -exec cksum '{}' \; > $COMPFILE
else
echo "Calculating comparative checksums for $CURSUM..."
echo "Calculating comparative checksums for $CURSUM..." >> $LOGFILE
find . -type f -exec cksum '{}' \; > $CURSUM
echo "Comparing files..."
echo "Comparing files..." >> $LOGFILE
diff $COMPFILE $CURSUM > /var/tmp/mirror.$$
if [ -s /var/tmp/mirror.$$ ]; then
echo "Differences encountered, copying files..."
echo "Differences encountered, copying files..." >> $LOGFILE
fillist=`grep '>' /var/tmp/mirror.$$ |awk '{print $NF}'`
for name in $fillist; do
destdir=`dirname $REMOTEDIR/$name`
echo "ssh $REMOTEMACHINE $INITDIR $destdir" >>
$LOGFILE 2>&1
echo "scp -p $name $REMOTESITE/$name" >> $LOGFILE
echo "ssh $REMOTEMACHINE chmod -R go+rX $destdir" >>
$LOGFILE 2>&1
ssh $REMOTEMACHINE $INITDIR "$destdir" >> $LOGFILE 2>&1
scp -p $name "$REMOTESITE/$name" >> $LOGFILE 2>&1
ssh $REMOTEMACHINE chmod -R go+rX "$destdir" >>
$LOGFILE 2>&1
done
mv $CURSUM $COMPFILE >> $LOGFILE 2>&1
else
echo "No changes..."
echo "No changes..." >> $LOGFILE
fi
fi
fi
rm -f /var/tmp/mirror.$$ /var/tmp/mirror.lock
exit 0
--
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug