Ho creato questo script  come descritto nel titolo “watch e sync” per osservare una determinata cartella e sincronizzarla tramite rsync  su un’altro server.

Nello specifico l’ho scritto per sincronizzare la document root di un server in load balance sulla piattaforma 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 392 times

Leave a Reply

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