#!/bin/bash


STATUS_DIR=/usr/local/var/lib/backup
mkdir -p $STATUS_DIR

RDIR=MANO
touch -d 'now -23 hour' $STATUS_DIR/timestamp

# Als er geen copy is, of als de laatste complete copy ouder is dan 24 uur,
# probeer dan een nieuwe copy te maken 
# Het tijdstip van  ${STATUS_DIR}/${RDIR} is het tijdstip van de 
# laatste complete copy
# De inhoud van ${STATUS_DIR}/${RDIR} is de directory van de laatste 
# compete copy 

if [ ! -e ${STATUS_DIR}/${RDIR} ] || 
   [ ${STATUS_DIR}/${RDIR} -ot $STATUS_DIR/timestamp ]
then if ssh -i /root/.ssh/id_rsa_rsync rsync@mano date 2> /dev/null > /dev/null
     then TMP_FILE=`mktemp -t` 
          lockfile /var/lock/LinuxBackup
          mount /LinuxBackup
	  if [ -e ${STATUS_DIR}/${RDIR} ]
	  then LINKDIR=`cat ${STATUS_DIR}/${RDIR}`
	  else LINKDIR=/dev/null
	  fi
	  if rsync -av --rsh="ssh -i /root/.ssh/id_rsa_rsync" \
                   --modify-window=3601 \
	           --delete  --exclude=/proc/ \
		   --exclude=/mnt/windows/windows/win386.swp \
		   --modify-window=3601 --link-dest=$LINKDIR \
	           rsync@mano:/ /LinuxBackup/MANO/TMP > $TMP_FILE
          then mv $TMP_FILE ${STATUS_DIR}/${RDIR}.list
               BACKUPDIR=/LinuxBackup/MANO/`date +%Y%m%d-%H%M%S`
	       mv /LinuxBackup/MANO/TMP $BACKUPDIR
	       echo $BACKUPDIR > ${STATUS_DIR}/${RDIR}
          else mv --backup=t $TMP_FILE ${STATUS_DIR}/${RDIR}half
          fi
	  umount /LinuxBackup
	  rm -f /var/lock/LinuxBackup
     fi
fi

exit
