Here is a script I wrote to keep 2 or more web sites in sync. The data files can be anything under the data tree.
The database is MySQL.
The script works by pulling the latest data and database backup from the production server. The script can be placed in a cron.
The DR server user should be able to ssh without a password to the production server. This is accomplished using RSA keys under SSH.
Once both the web site and database are setup, this script pulls the newest files, and latest mysql database backup via rsync.
Change the First set of VARIABLES, and <usernames> and <pathing> where needed.
I should probably have more information on the script here, but I wanted to save a copy somewhere before I forgot about it.
Anyway, my thought is if I setup a primary web server somewhere and want n+1 backend systems I might be able to use this poor-man’s approach. If the backed systems need to have database updates then you are looking at database replication. But, the rsync file copy and all of the rest could be used to make web server production updates to a cluster of systems. You just update the main instance and all of the slave servers pull the latest information, including a database update script if needed., unix, script
#!/bin/ksh -x
#######################################
#
# Script to sync Prod web with DR server.
# The script is designed to pull from the
# production server.
#
# 12/2008: Initial setup.
# : Mike Byers
#######################################
PRIME_HOST=’prod.edmz.com’ #Production host
DR_HOST=’dr-orprod2.edmz.com’ #DR server
DATABASES=”mysql DB1 DB2″ #database to transfer space sepa
rated
#MYSQLUSER=backup #User for database backup
MYSQLUSER=restore #User for database restore
#MYSQLPWD=’ABC123′ #Backup user Password
MYSQLPWD=’ABC123′ #Restore user Password (Restore user needs more DB permissions)
DB_PATH=’/opt/mysql5/backups’ #Path to database backup files
#YESTERDAY=`date +%Y%m%d -d”1 day ago”` #Linux Version
YESTERDAY=`TZ=GMT+24 date +%Y%m%d` #Solaris Version
TODAY=`date +%Y%m%d`
PATH=$PATH:/opt/mysql5/bin/;export PATH #Path to mysql binaries
/usr/bin/rsync –exclude ‘configuration.*’ \
–exclude ‘**/logs’ \
–exclude ‘**/modules’ \
–exclude ‘**/conf’ \
–exclude ‘**/bin’ \
-vaue ’ssh -x -C -l <username>’ $PRIME_HOST:apache/htdocs/* /<path>/apache/htdocs/
for DB in $DATABASES
do
/usr/bin/rsync -vaue ’ssh -x -C -l <username>’ $PRIME_HOST:$DB_PATH/$DB-$TODAY.sql.
gz $DB_PATH
#Compare sizes of database backup files to see if anything changed
S1=`du -sk $DB_PATH/$DB-$YESTERDAY.sql.gz | awk ‘{ print $1 }’`
S2=`du -sk $DB_PATH/$DB-$TODAY.sql.gz | awk ‘{ print $1 }’`
if [[ S1 -eq S2 ]]; then
echo “No update for database $DB needed”
else
/bin/gunzip < $DB_PATH/$DB-$TODAY.sql.gz | mysql -u $MYSQLUSER -p$MYSQLPWD
$DB
fi
done
