I made this script so as the tile describe “watcher and sync” to watch a certain folder ans sync it to another server through rsync. I made it to sync the document root of a web server in load balance in AWS cloud!!!

 #!/bin/bash
SOURCE="full_SOURCE_path"
 SLAVE1="remote_IP_or_FQDN"
 SLAVE2="remote_IP_or_FQDN"
 SLAVES="$SLAVE1 $SLAVE2"

for PID in $(pgrep watcher)
 do
   if [ $PID -ne $$ ]
     then
     echo "Killing old watcher with pid $PID"
     kill $PID
   fi
 done

for PID in $(pgrep inotifywait)
 do
   echo "Killing old inotifywait with pid $PID"
   kill $PID
 done
for SLAVE in $SLAVES
 do
   echo "Initial syncronization of ${SOURCE} to ${SLAVE}"
   rsync -av --del --exclude 'uploads/*' ${SOURCE}/ ${SLAVE}:${SOURCE}/
 done
echo "Start watching on ${SOURCE}"
 inotifywait -mr --timefmt "%d/%m/%y@%H:%M:%S" --format '%T %e %w %f' --exclude "full_path_to_exclude" \
 -e create -e modify -e move -e delete \
 $SOURCE | while read TIME EVENT DIR FILENAME
do
 echo "$TIME: sync $DIR because of $EVENT $FILENAME"
 for SLAVE in $SLAVES
do
 echo "to: ${SLAVE} aaa ${DIR}"
 rsync -av --del --exclude 'full_path_to_exclude/*' ${DIR} ${SLAVE}:${DIR}
done
done

This page has been readed 530 times

Leave a Reply

Your email address will not be published. Required fields are marked *