#!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   copy my docroot to many web servers using rsync
# author
#   larg 15/04/03
# description
#   This remotely updates files on hosts.
#   Directories and files are sourced from the directory named <host>.
#   The matching files and directories on the remote host named <host>
#   are updated if they are different.
#   The rsync protocol is used for the transfer.
#
#   This recursively transfers all files from the directory
#   /a/b/c  on this machine into the identical /a/b/c directory
#   on the other machine. The files are transferred in "archive"
#   mode,  which  ensures  that  symbolic links, devices, attri-
#   butes, permissions, ownerships  etc  are  preserved  in  the
#   transfer.
#   Compression will be used to reduce the size of data portions of the transfer.
#
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
   usage_message="
   usage:
      $0 [--real] <dir> <host> [<host>...]
   description
      This remotely updates a directory from this host to remote hosts.
      Files are sourced from the directory <dir>. The matching files
      and directories on the remote host named <host> are updated if they are
      different.

      The rsync protocol over SSH is used for the transfer.
   options
      --real  Copy files. If this is not included, you are shown what would be
              copied but nothing is actually transferred.
   example:
      the command
         $0 --real /opt wds21
      updates the /opt directory tree on the host wds21 to match that in the
      local directory wds21.
   "
# checks
   # wrong options provided? tell the punter how to use this utility
   if [ $# -lt 2 ]; then
      echo ""
      echo "$usage_message" 1>&2
      exit 1
   fi

# main

# really update files?
   dryrun="-n"
   if [ "-$1" = "---real" ] ; then
     shift
     dryrun=""
   else
     echo "Dry run only. Use --real to update files."
   fi

# does source directory exist?
   [ -d $1 ] || {
      echo ""
      echo "No such directory '$1'"
      echo "$usage_message" 1>&2
      exit 2 ;
   }
   SRC_DIR=$1                      # source directory to copy from
   DST_DIR=`/usr/bin/dirname $1`   # destination directory to copy to (parent folder of $SRC_DIR)
   shift

# update hosts
   for HOST in $*
   do
      echo "`date`"
      echo "updating host $HOST from directory $SRC_DIR ..."
      echo ""

      # -a - archive mode, equivalent to -rlptgoD
      # -n ($dryrun) don't copy anything
      /usr/local/bin/rsync \
         -v -a -e /opt/ssh/bin/ssh --delete $dryrun  \
         $SRC_DIR $HOST:$DST_DIR
      RET=$?     # make a note of the return code

# check return code
        if [ $RET -ne 0 ]; then
                echo "$0: rsync to $HOST failed with return code $RET" >&2;
                echo "$0: examine the logfile for more information." >&2;
        fi

# clean up
   done
   echo "done."
   echo ""

