多功能SVN备份脚本

这个脚本个人觉得比较完美了,SVN完全备份,加增量备份,并将备份出来的数据,同步到另外一台服务器上,而且可以实现实时备份并发邮件通知,历史备份记录想查就查。

这个脚本参考了部分网上的资源。

稍作修改就可以应用到你的实际生产环境了,有问题的话,可以联系我(*^__^*) ……

具体脚本内容如下:

  1. #!/bin/sh  
  2. #by 崔元荣
  3. #QQ:295749093 
  4. #svn多功能备份脚本! 
  5. svnbak_remote_dir=/data/192.168.1.7_svn/   
  6.  
  7. ###################################  
  8. BACKUPDIR=/var/svnbackup  
  9.  
  10. ###################################  
  11. SVNDIR=/var/svn  
  12.  
  13. ###################################  
  14. ProjectLst=$BACKUPDIR/projectlist.txt  
  15.  
  16. ###################################  
  17. LogFile=$BACKUPDIR/svnback.log  
  18. History_LogFile=$BACKUPDIR/history_svnback.log  
  19. DATE=`date +%Y%m%d-%T`  
  20.  
  21. # - This is the path to the directory you want to archive  
  22. ###################################  
  23. export PATH=$PATH:/bin:/usr/bin:/usr/local/bin  
  24.  
  25. # our actual rsyncing function 
  26. do_accounting()  
  27. {  
  28.     echo " " > $LogFile  
  29.         echo " " >> $LogFile  
  30.         echo " " >> $LogFile  
  31.         echo "###########################" >> $LogFile  
  32.         echo "$DATE" >> $LogFile  
  33.         echo "###########################" >> $LogFile  
  34.  
  35.         echo " " >> $History_LogFile  
  36.         echo " " >> $History_LogFile  
  37.         echo "###########################" >> $History_LogFile  
  38.         echo "$DATE" >> $History_LogFile  
  39.         echo "###########################" >> $History_LogFile  
  40.  
  41.         cd $BackDir  
  42. }  
  43.  
  44. do_svndump()  
  45. {  
  46.    PROJECTLIST=`cat $ProjectLst`  
  47.    cd $SVNDIR  
  48.    for project in $PROJECTLIST  
  49.           do  
  50.           echo "begin to dump $project databases" >> $LogFile  
  51.           echo "begin to dump $project databases" >> $History_LogFile  
  52.                 if [ ! -f $BACKUPDIR/$project.dump ]  
  53.                 then 
  54.                 YOUNGEST=`svnlook youngest $project`  
  55.                 svnadmin dump $project > $BACKUPDIR/$project.dump  
  56.                   if [ $? != 0 ];then 
  57.                 echo "full-backup faild" >> $LogFile  
  58.                 echo "full-backup faild" >> $History_LogFile   
  59.                     else 
  60.                         echo "OK,dump file successfully!!" >> $LogFile  
  61.                         echo "OK,dump file successfully!!" >> $History_LogFile   
  62.                       fi  
  63.                 echo "$YOUNGEST" > $BACKUPDIR/$project.youngest  
  64.                 else 
  65.                         echo "$project.dump existed,will do increatment job" >> $LogFile  
  66.                         echo "$project.dump existed,will do increatment job" >> $History_LogFile   
  67.                         if [ ! -f $BACKUPDIR/$project.youngest ]  
  68.                         then 
  69.                         echo "error, no youngest check!" >> $LogFile  
  70.                         echo "error, no youngest check!" >> $History_LogFile  
  71.                         else 
  72.                                 PREVYOUNGEST=`cat $BACKUPDIR/$project.youngest`  
  73.                                 NEWYOUNGEST=`svnlook youngest $project`  
  74.                                 if [ $PREVYOUNGEST -eq $NEWYOUNGEST ]  
  75.                                 then 
  76.                                         echo " no database updated!" >> $LogFile  
  77.                                         echo " no database updated!" >> $History_LogFile  
  78.                                 else 
  79.                                         LASTYOUNGEST=`expr $PREVYOUNGEST + 1`  
  80.                                         echo "last youngest is $LASTYOUNGEST" >> $LogFile  
  81.                                         echo "last youngest is $LASTYOUNGEST" >> $History_LogFile  
  82.                                         svnadmin dump $project --revision $LASTYOUNGEST:$NEWYOUNGEST --incremental > $BACKUPDIR/$project-$LASTYOUNGET-$NEWYOUNGEST.$DATE  
  83.                                           if [ $? != 0 ];then 
  84.                                              echo "zhengliang-backup faild" >> $LogFile  
  85.                                              echo "zhengliang-backup faild" >> $History_LogFile  
  86.                                                else 
  87.                                                     echo "$NEWYOUNGEST" > $BACKUPDIR/$project.youngest  
  88.                                                     echo "zhengliang-backup sucess!" >> $LogFile  
  89.                                                     echo "zhengliang-backup sucess!" >> $History_LogFile  
  90.                                                    fi  
  91.                                 fi  
  92.                         fi  
  93.   fi  
  94.         done  
  95. }  
  96.  
  97.  
  98. do_rsync()  
  99. {  
  100. rsync -avz --partial --progress --delete $BACKUPDIR/* [email protected]:$svnbak_remote_dir  
  101. if [ $? != 0 ]; then 
  102.     echo "svnbak 192.168.1.7 rsync to 192.168.1.6 faild!!!" >> $LogFile  
  103.     echo "svnbak 192.168.1.7 rsync to 192.168.1.6 faild!!!" >> $History_LogFile   
  104. else 
  105.     echo "svnbak 192.168.1.7 rsync to 192.168.1.6 successfully!!" >> $LogFile  
  106.     echo "svnbak 192.168.1.7 rsync to 192.168.1.6 successfully!!" >> $History_LogFile   
  107. fi  
  108. }  
  109.  
  110. do_empty()  
  111. {  
  112. #echo "" > $LogFile  
  113. for project in $PROJECTLIST  
  114. do  
  115.     rm -f $BACKUPDIR/$project*  
  116. done  
  117. }  
  118.  
  119. do_mail()  
  120. {  
  121. cat $LogFile | mutt -s "SVN Backup status!" [email protected]  
  122. }  
  123. #################################################  
  124.  
  125. case "$1" in 
  126.     dump_full)  
  127.         do_empty && do_accounting && do_svndump && do_rsync && do_mail  
  128.         ;;  
  129.     dump_incremental)  
  130.         do_accounting && do_svndump && do_rsync && do_mail  
  131.         ;;  
  132.     *)  
  133.         echo $"Usage: $0 {dump_full|dump_incremental}" 
  134.         exit 2  
  135. esac  

svn

相关推荐