Wednesday, November 19, 2008

SVN backup script

#!/bin/bash
#
# Backup of Subversion repos by svnadmin
# Umakanta Samantaray
# Nov 2008
#
# It will take full backup on Sunday and incremental backup daily at 00:00( set in crontab)
#
# full-backup tarball filenames will be of the form
# fullbackup.[repos name].YYYYMMDD@HHMM
# and Incremental will be incrembackup.[repos name].YYYYMMDD@HHMM
# This ensures that a numerical sort (sort -n) will produce proper date order
# Crontab log directory is /home/usamantaray/svnbackup.log
#

svn_repodir=/srv/svn/repos
backupdir=/home/backups/svnbackup
dtstamp=`date "+%Y%m%d@%H%M"`
date

# Create a file which will contain the repository directory list
tempdirlist=/tmp
cd $svn_repodir && ls -d */ | xargs -l basename > ${tempdirlist}/dirlist.`date "+%Y%m%d"` && cd -
sed -i '/infrastructure/,1 d' ${tempdirlist}/dirlist.`date "+%Y%m%d"`
sed -i '/livecat.original/,1 d' ${tempdirlist}/dirlist.`date "+%Y%m%d"`

if [ "`date +"%a"`" = "Sun" ] ; then
echo "Grab Full Backup"
{
cd $svn_repodir && echo > revision_no && cd -

value=1
while read line
do
value=`expr $value + 1`;
echo "`svnlook youngest ${svn_repodir}/${line}` ${line}" >> ${svn_repodir}/revision_no
svnadmin dump ${svn_repodir}/${line} > ${backupdir}/fullbackup.${line}.${dtstamp} \
&& find ${backupdir} -type f -name '*backup.*' -mtime +14 -exec rm {} \;

done < ${tempdirlist}/dirlist.`date "+%Y%m%d"`
cat ${svn_repodir}/revision_no
}
else
echo "Incremental Backup"
{
value=1
while read line
do
value=`expr $value + 1`;
RECENT=`svnlook youngest ${svn_repodir}/${line}`
REVNO=`cat ${svn_repodir}/revision_no | grep "\<${line}\>" | cut -d" " -f1`
LAST=`expr ${REVNO} + 1`
if [ "${REVNO}" ] ; then
{
if [ ${RECENT} != ${REVNO} ] ; then
{
echo "Incremental Backup of ${line} is Rev no ${LAST}:${RECENT}"
svnadmin dump --incremental -r ${LAST}:${RECENT} ${svn_repodir}/${line} > ${backupdir}/incrembackup.${line}.${dtstamp}
sed -i "/${REVNO} ${line}/,1 d" ${svn_repodir}/revision_no
echo "`svnlook youngest ${svn_repodir}/${line}` ${line}" >> ${svn_repodir}/revision_no
}
fi
}
fi
done < ${tempdirlist}/dirlist.`date "+%Y%m%d"`
}
fi

# remove the temp dir list file
if [ -d ${tempdirlist} ] ; then
cd ${tempdirlist} && find ./ -type f -name 'dirlist*' -exec rm {} \;
else
echo " ${tempdirlist} does not exist"
fi

exit

No comments: