#!/bin/sh
###
### Do daily incremental backups of all subversion projects
###

Start=`date +%s`

umask 022

cd /scm/rep || exit 1;

for Proj in *
do
  if [ -d ${Proj} ] && [ -f ${Proj}/format ]
  then
    BAK=/scm/bak/${Proj}
    INCR=${BAK}/INCR.latest 
    if [ -s ${INCR} ]
    then 
        LAST=`cat ${INCR}`
    else
        LAST=0
    fi
    HEAD=`svnlook youngest ${Proj}`

    if [ ${LAST} -lt ${HEAD} ]
    then
        RANGE="$((${LAST} + 1)):${HEAD}"
        if [ ${LAST} -ne 0 ]
	then
	    type=--incremental
	else
            type=
	fi

	if svnadmin dump --quiet ${type} -r ${RANGE} \
	       ${Proj} > ${BAK}/${Proj}.incr.${RANGE}
	then
	    echo ${HEAD} > ${INCR}
	else
	    echo "Subversion incremental backup failed. ${RANGE}" 1>&2
	fi
    fi
  else
    echo "Not a subversion repository: /scm/rep/${Proj}" 1>&2
  fi
done

Stop=`date +%s`

Seconds=`echo ${Stop} ${Start} - p | dc` 

echo "Subversion Incremental Backups completed in ${Seconds} seconds"


