原文:http://carywu.blog.51cto.com/13185/207571
最近要给美国的SVN服务器做升级,由于这台服务器有一定年份了,在本公司也算是个三朝元老,保存了多少code啊!。为了保险起见,老板将迁移的工作郑重的交到我的手上。在欣欣然的同时也颤抖着。
不过还好我已经对其轻车熟路,在新的服务器上安装好apache和subversion软件后,开始着手备份与迁移了。
由于旧服务器版本太低,没有svnsync的功能,考虑再三,我选择使用了svnadmin dump去做备份。
可是有一个问题了,每个库的版本都有几万了,然后服务器上有十几个库,我可没耐心一个个输入svnadmin dump去备份它们,那会让我觉得长夜漫漫,难于睡眠!
管理员的最高境界就是花力气去懒惰,我很向往这个境界。所以,我花了些时间写了个shell,就让系统自己去干这些事情吧!
shell功能:
只需要大家修改源库所在的目录,和备份存放的目录就可以了。
第一次运行会全部备份,然后以后每次运行会增量备份。
所以,恢复的时候,先恢复全部备份,然后依次按照日期恢复增量备份即可。
#!/bin/sh
#########################################################
# this script is used for svn repository backup
# written: carywu
# date: 2008/11/21
#########################################################
###################################
# mail address for status updates
# - This is used to email you a status report
###################################
MAILADDR=carywu@yahoo.cn
###################################
# backup directory,please change it
# - This is the path to the backup directory
###################################
BACKUPDIR=/svnroot/svnback
###################################
# svn repository directory, please change it.
# - This is the path to the directory you want to archive
###################################
SVNDIR=/svnroot/repository
###################################
# HOSTNAME and project list
# - This is also used for reporting
###################################
ProjectLst=$BACKUPDIR/projectlist.txt
###################################
# svn backup log path and date
# - This is used to log result
###################################
LogFile=$BACKUPDIR/svnback.log
DATE=`date +%Y%m%d-%T`
###################################
# svn command path
# - This is the path to the directory you want to archive
###################################
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# our actual rsyncing function
do_accounting()
{
echo " " >> $LogFile
echo " " >> $LogFile
echo "###########################" >> $LogFile
echo "$DATE" >> $LogFile
echo "###########################" >> $LogFile
cd $BackDir
}
do_svndump()
{
PROJECTLIST=`cat $ProjectLst`
cd $SVNDIR
for project in $PROJECTLIST
do
echo "begin to dump $project databases" >> $LogFile
if [ ! -f $BACKUPDIR/$project.dump ]
then
YOUNGEST=`svnlook youngest $project`
svnadmin dump $project > $BACKUPDIR/$project.dump
echo "OK,dump file successfully!!"
echo "$YOUNGEST" > $BACKUPDIR/$project.youngest
else
echo "$project.dump existed,will do increatment job" >> $LogFile
if [ ! -f $BACKUPDIR/$project.youngest ]
then
echo "error, no youngest check!" >> $LogFile
else
PREVYOUNGEST=`cat $BACKUPDIR/$project.youngest`
NEWYOUNGEST=`svnlook youngest $project`
if [ $PREVYOUNGEST -eq $NEWYOUNGEST ]
then
echo " no database updated!" >> $LogFile
else
LASTYOUNGEST=`expr $PREVYOUNGEST + 1`
echo "last youngest is $LASTYOUNGEST" >> $LogFile
svnadmin dump $project --revision $LASTYOUNGEST:$NEWYOUNGEST --incremental > $BACKUPDIR/$project-$LASTYOUNGET-$NEWYOUNGEST.$DATE
echo "$NEWYOUNGEST" > $BACKUPDIR/$project.youngest
fi
fi
fi
done
}
# our post rsync accounting function
do_mail()
{
mail $MAILADDR -s svn-back_log < $LogFile
}
# some error handling and/or run our backup and accounting
do_accounting && do_svndump && do_mail
#########################################################
# this script is used for svn repository backup
# written: carywu
# date: 2008/11/21
#########################################################
###################################
# mail address for status updates
# - This is used to email you a status report
###################################
MAILADDR=carywu@yahoo.cn
###################################
# backup directory,please change it
# - This is the path to the backup directory
###################################
BACKUPDIR=/svnroot/svnback
###################################
# svn repository directory, please change it.
# - This is the path to the directory you want to archive
###################################
SVNDIR=/svnroot/repository
###################################
# HOSTNAME and project list
# - This is also used for reporting
###################################
ProjectLst=$BACKUPDIR/projectlist.txt
###################################
# svn backup log path and date
# - This is used to log result
###################################
LogFile=$BACKUPDIR/svnback.log
DATE=`date +%Y%m%d-%T`
###################################
# svn command path
# - This is the path to the directory you want to archive
###################################
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# our actual rsyncing function
do_accounting()
{
echo " " >> $LogFile
echo " " >> $LogFile
echo "###########################" >> $LogFile
echo "$DATE" >> $LogFile
echo "###########################" >> $LogFile
cd $BackDir
}
do_svndump()
{
PROJECTLIST=`cat $ProjectLst`
cd $SVNDIR
for project in $PROJECTLIST
do
echo "begin to dump $project databases" >> $LogFile
if [ ! -f $BACKUPDIR/$project.dump ]
then
YOUNGEST=`svnlook youngest $project`
svnadmin dump $project > $BACKUPDIR/$project.dump
echo "OK,dump file successfully!!"
echo "$YOUNGEST" > $BACKUPDIR/$project.youngest
else
echo "$project.dump existed,will do increatment job" >> $LogFile
if [ ! -f $BACKUPDIR/$project.youngest ]
then
echo "error, no youngest check!" >> $LogFile
else
PREVYOUNGEST=`cat $BACKUPDIR/$project.youngest`
NEWYOUNGEST=`svnlook youngest $project`
if [ $PREVYOUNGEST -eq $NEWYOUNGEST ]
then
echo " no database updated!" >> $LogFile
else
LASTYOUNGEST=`expr $PREVYOUNGEST + 1`
echo "last youngest is $LASTYOUNGEST" >> $LogFile
svnadmin dump $project --revision $LASTYOUNGEST:$NEWYOUNGEST --incremental > $BACKUPDIR/$project-$LASTYOUNGET-$NEWYOUNGEST.$DATE
echo "$NEWYOUNGEST" > $BACKUPDIR/$project.youngest
fi
fi
fi
done
}
# our post rsync accounting function
do_mail()
{
mail $MAILADDR -s svn-back_log < $LogFile
}
# some error handling and/or run our backup and accounting
do_accounting && do_svndump && do_mail