#!/bin/sh
#
# vim:tabstop=3:expandtab:shiftwidth=3 
#
# $Date: 2019-01-06 14:58:47 +0100 (Sun, 06 Jan 2019) $
# $Revision: 2898 $
#

BACKUP=`mount 2> /dev/null | awk '/ on \/[^ ]*\/Backup /{print $3}'`
RSYNC_OPTS="-av --delete --force --ignore-errors" # --no-g
EXCLUDEFILE=.sync2backup-exclude
BACKUP_HOST=`hostname -s`
if [ "$HOSTNAME" = "" ]
then
   HOSTNAME=`hostname`
fi

# Get variables from the global system file
eval `grep '^[^#[:space:]=]*=' /etc/sysconfig/sync2backup 2> /dev/null | \
      sed 's/^/export /'`

# Get variable overwrites from hidden file
eval `grep '^[^#[:space:]=]*=' $HOME/.sync2backup 2> /dev/null | \
      sed 's/^/export /'`

if [ "$BACKUP" = "" ] || [ ! -d $BACKUP ]
then
   echo "Backup not mounted, stopping.."
   exit 1
fi

# May we write there?
touch $BACKUP/sync2backup.$$
if [ ! -f $BACKUP/sync2backup.$$ ]
then
   echo "$BACKUP is not writable, stopping.."
   exit 1
else
   # Can we change the group owner?
   if ! chgrp `id -g` $BACKUP/sync2backup.$$ 2> /dev/null
   then
      echo "NOTE:unable to change ownerships on $BACKUP, skipping ownerships"
      RSYNC_OPTS="$RSYNC_OPTS --no-g"
   fi
   rm -f $BACKUP/sync2backup.$$
fi

# does rsync support --no-motd ? -> yes -> put it in
if rsync --help 2>&1 | grep '[-]-no-motd' > /dev/null 2>&1
then
   RSYNC_OPTS="$RSYNC_OPTS --no-motd"
fi

unset NO_RSYNC_HOSTS FILE_SYSTEM_FULL_SYSTEMS

for HOST_NAME in $BACKUP_HOST `egrep -v '^#|^[^[:space:]=]*=[^=]*' \
                               $HOME/.sync2backup 2> /dev/null |\
                               awk '{print $1}'`
do
   echo
   echo "== Syncing $USER@$HOST_NAME homedir to $BACKUP =="
   unset RSYNC_HOST_OPTS
   if [ "$HOST_NAME" = "$BACKUP_HOST" ] || [ "$HOST_NAME" = "$HOSTNAME" ]
   then
      cd
      HOST=`pwd`/
   else
      HOST="$HOST_NAME:"
      RSYNC_HOST_OPTS=`grep "^$HOST_NAME[[:space:]]" $HOME/.sync2backup | \
                       sed -e 's/^[^[:space:]]*[[:space:]]*//' \
                            -e 's/[[:space:]]*$//'`
   fi
   rm -f /tmp/${EXCLUDEFILE}.$$
   rsync $RSYNC_HOST_OPTS ${HOST}${EXCLUDEFILE} /tmp/${EXCLUDEFILE}.$$ \
      2> /dev/null
   unset EXCLUDE_OPTION
   if [ -f /tmp/${EXCLUDEFILE}.$$ ]
   then
      echo "-- Using exclude file ${EXCLUDEFILE} from $USER@$HOST_NAME --"
      EXCLUDE_OPTION="--exclude-from=/tmp/${EXCLUDEFILE}.$$"
   fi

   # Create the backup tree
   mkdir -p $BACKUP/$HOST_NAME &> /dev/null
   # Open up the host tree for other users
   chmod 777 $BACKUP/$HOST_NAME &> /dev/null

   # Do the actual sync
   rsync $RSYNC_OPTS $RSYNC_HOST_OPTS $EXCLUDE_OPTION $HOST \
            $BACKUP/$HOST_NAME/$USER/ 2> /dev/null
   RET=$?
   if [ $RET -ne 0 ] && [ $RET != 23 ]
   then
      NO_RSYNC_HOSTS="$NO_RSYNC_HOSTS $HOST"
      if [ $RET -eq 12 ]
      then
         FILE_SYSTEM_FULL_SYSTEMS="$FILE_SYSTEM_FULL_SYSTEMS $HOST"
      fi
   fi
   rm -f /tmp/${EXCLUDEFILE}.$$
   unset EXCLUDE_OPTION
done

if [ "$NO_RSYNC_HOSTS" != "" ]
then
   echo "*** Unable to rsync with the next hosts:"
   echo "$NO_RSYNC_HOSTS" | tr ' ' '\n' | sed -e 's/^/   /' -e 's/:$//'
   echo
fi

if [ "$FILE_SYSTEM_FULL_SYSTEMS" != "" ]
then
   NEARLY_FULL="`df -h $BACKUP 2> /dev/null | egrep ' 100% | 9[7-9]% '`"
   if [ "$NEARLY_FULL" != "" ]
   then
      echo
      echo " File system $BACKUP full??"
      echo
      echo " During rsync from:"
      echo 
      echo "$FILE_SYSTEM_FULL_SYSTEMS" | \
        tr ' ' '\n' | sed -e 's/^/   /' -e 's/:$//'
      echo
      echo "  $NEARLY_FULL"
      echo
   fi
fi

