#!/bin/sh

######################################
#BEGIN CONFIGURATION
######################################

#The backup directory name cannot have a space in it.
backups="/backupdir"

#maxsize is the maximum number of KB that you want the backup directory to 
#grow to.  We will use the output of df -k.  This assumes that backups
#are the only thing in that filesystem, of course.
#This is 375 GB

maxsize="3750000000"

#Here is where you enter the names of the systems to be backed up
#This part will be the sourcedir for rsync, so you need to follow
#rsync practice. Notice the trailing slash.  It is required to
#backup a directory. Also don't have any trailing new lines.
#Have the quote just before the first entry and just after the
#last entry, not on the next line.  It confuses things.
#Read the rsync man page for examples 

echo "root@example.com:/home/username/" >/tmp/sources.$$

######################################
#END CONFIGURATION
######################################

df -k |grep -i "$backups" >/dev/null

if [ $? -gt 0 ] ; then
  echo "The backup filesystem does not appear to be mounted."
  exit 1
fi

echo $backups |grep "[ 	]"
if [ $? -eq 0 ] ; then
  echo "The backup directory name cannot have a space or tab in it"
  exit 1
fi

date=`date "+%Y-%m-%d-%H-%M-%S"`

#This section is designed to make room for new backups
#Since it deletes stuff, I left it commented out
#cd $backups
#df -k . >/tmp/df.$$
#size=`grep -v Available /tmp/df.$$|awk '{print $3}'`
#rm /tmp/df.$$
#
#echo "Size is $size, max is $maxsize"
#
#ct=0
#while [ $size -gt $maxsize ] ; do
#
#  echo "Looking for backups to delete"
#
#  ct=`expr $ct + 1`
#
#  cd $backups
#
#  for dir in Backups.*
#  do
#
#    echo "  Looking in $backups/$dir for backups to delete"
#
#    cd $backups/$dir
#    oldest=`ls -td Backup.*|tail -1`
#    latest=`ls -l Latest|awk '{print $11}'`
#    if [ -z "$oldest" ] ; then
#      echo "There are no older backups to delete"
#    fi
#    if [ $oldest != $latest ] ; then
#      echo "Removing $backups/$oldest"
#      rm -rf $oldest
#    else
#      echo "Cannot remove newest backup"
#    fi
#
#    size=`du -sk $backups|awk '{print $1}'`
#    echo "Size is now $size, max is $maxsize"
#    [ $size -lt $maxsize ] && break
#
#  done
#
#  [ $ct -gt 30 ] && echo "Something's wrong" && exit 1
#
#done

cat /tmp/sources.$$|while read tmpsource
do

  echo $tmpsource|grep '\/$' >/dev/null

  if [ $? -gt 0 ] ; then
    echo "The source line \"${i}\" is missing the trailing slash."
    echo "Adding it."
    source="${tmpsource}/"
  else
    source="${tmpsource}"
  fi

  cd $tmpsource
  cd $backups
  
  [ -n "`echo $source|grep :`" ] && ssh="-e ssh" || ssh=""
  
  tmpbackupdir=`echo $source|sed 's/[	 \/:@]/-/g'|sed 's/^-//'|sed 's/--*$//'|sed 's/--*/-/g'`
  
  if [ -n "$ssh" ] ; then
    backupdir="Backups.$tmpbackupdir"
  else
    host=`hostname`
    [ -n "$tmpbackupdir" ] && backupdir="Backups.$host-$tmpbackupdir" || backupdir="Backups.$host"
  fi
  
  [ -d "$backupdir" ] || mkdir "$backupdir"
  
  cd "$backupdir"
  
  [ -h Latest ] && linkdest="--link-dest=$backups/$backupdir/Latest" || linkdest=""
  
  inprogress=`ls -td *ess|head -1`
  [ -n "$inprogress" ] || inprogress="Backup.${date}.inProgress"

  [ -d "$inprogress" ] || mkdir "$inprogress"

  log="Backups.log"
  [ -d $backups/$log ] || mkdir $backups/$log
  
  #Keep 5 copies of the backup log
  [ -f $backups/$log/$backupdir.5.log ] && rm -f $backups/$log/$backupdir.5.log
  [ -f $backups/$log/$backupdir.4.log ] && mv $backups/$log/$backupdir.4.log $backups/$log/$backupdir.5.log
  [ -f $backups/$log/$backupdir.3.log ] && mv $backups/$log/$backupdir.3.log $backups/$log/$backupdir.4.log
  [ -f $backups/$log/$backupdir.2.log ] && mv $backups/$log/$backupdir.2.log $backups/$log/$backupdir.3.log
  [ -f $backups/$log/$backupdir.1.log ] && mv $backups/$log/$backupdir.1.log $backups/$log/$backupdir.2.log
  [ -f $backups/$log/$backupdir.log ] && mv $backups/$log/$backupdir.log $backups/$log/$backupdir.1.log
  
  echo "Backing up $source to $backupdir/$inprogress"
  rsync -avx --numeric-ids --delete $ssh $linkdest "$source" "$backups/$backupdir/$inprogress"  >$backups/$log/$backupdir.log
  
  #Rsync often spits out error 24 if you're backing up an actively used system.  It means that files disappeared while backing them up.
  #So I consider it OK
  #If you only want to consider 0s successful, then you'll need to change the line to this:
  #if [ $? -eq 0 ] ; then

  if [ $? -eq 0 -o $? -eq 24 ] ; then
    echo "Successful.  Moving $inprogress to Backup.$date and updating link."
    mv "$backups/$backupdir/$inprogress" "$backups/$backupdir/Backup.$date"
    rm Latest
    ln -s "Backup.$date" Latest
  fi

done

