Chinaunix首页 | 论坛 | 博客
  • 博客访问: 739883
  • 博文数量: 141
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1115
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-17 14:32
个人简介

小公司研发总监,既当司令也当兵!

文章分类

全部博文(141)

分类: LINUX

2015-12-30 12:00:23

脚本:

点击(此处)折叠或打开

  1. # FILE_NAME : svn_daily_bak.sh
  2. #
  3. # DESCRIPTION: this script is write to backup svn every day
  4. #
  5. # AUTHOR : khls27
  6. #
  7. # DATE : 2015-12-30


  8. #PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  9. #export PATH

  10. SVN_PATH=/svn/repository
  11. SVN_BAK_PATH=/opt/data/SVNbackup/svndumps
  12. BAK_CONFIG=/opt/data/SVNbackup/config.inf

  13. # log file, ring buf. svnbackup2 is current log file and svnbackup1 is the history log file
  14. LOG_FILE_R1=/opt/data/SVNbackup/svnbackup1.log
  15. LOG_FILE_R2=/opt/data/SVNbackup/svnbackup2.log


  16. function print_log()
  17. {
  18.     if [ ! -f $LOG_FILE_R2 ] ;then
  19.         touch $LOG_FILE_R2
  20.     fi

  21.     size=$(ls -l $LOG_FILE_R2 | awk '{ print $5 }')

  22.     if [ $size -gt 20480 ] ;then
  23.         mv $LOG_FILE_R2 $LOG_FILE_R1
  24.         echo "" > $$LOG_FILE_R2
  25.     fi

  26.     echo "[$(date +%Y%m%d%H%M)] $1" >> $$LOG_FILE_R2
  27. }


  28. function do_full_backup()
  29. {
  30.     bakdir="$SVN_BAK_PATH/$(date +%Y%m%d)-full"
  31.     mkdir -p $bakdir
  32.     newest_rev=$(svnlook youngest $SVN_PATH)

  33.     print_log "start to do a full svn backup"
  34.     svnadmin dump $SVN_PATH | bzip2 | tee $bakdir/fulldump$(date +%Y%m%d).bz2 | md5sum > $bakdir/fulldump$(date +%Y%m%d).md5
  35.     print_log "full svn backup DONE    !"
  36.     
  37.     if [ -f $BAK_CONFIG ] ;then
  38.         sed -i "s/^LAST_BACKED_REV.*/LAST_BACKED_REV=$newest_rev/g" $BAK_CONFIG
  39.     else
  40.         echo "LAST_BACKED_REV=$newest_rev" > $BAK_CONFIG
  41.     fi
  42. }

  43. function do_incremental_backup()
  44. {
  45.     last_rev=0
  46.     newest_rev=$(svnlook youngest $SVN_PATH)
  47.     
  48.     if [ -f $BAK_CONFIG ] ;then
  49.         last_rev=$(grep "LAST_BACKED_REV" $BAK_CONFIG | awk -F '=' '{print $2}')
  50.         
  51.         if [ $last_rev -eq $newest_rev ] ;then
  52.             print_log "last backuped reversion is equal to newest"
  53.         fi
  54.     fi
  55.     
  56.     bakdir="$SVN_BAK_PATH/$(date +%Y%m%d)-r$last_rev-r$newest_rev"
  57.     mkdir -p $bakdir

  58.     print_log "start to do a incremental svn backup from $last_rev to $newest_rev"
  59.     svnadmin dump $SVN_PATH -r $last_rev:$newest_rev --incremental | bzip2 | tee $bakdir/incdump$(date +%Y%m%d).bz2 | md5sum > $bakdir/incdump$(date +%Y%m%d).md5
  60.     print_log "incremental svn backup from $last_rev to $newest_rev DONE!"

  61.     print_log "try update LAST_BACKED_REV to $newest_rev"

  62.     if [ -f $BAK_CONFIG ] ;then
  63.         sed -i "s/^LAST_BACKED_REV.*/LAST_BACKED_REV=$newest_rev/g" $BAK_CONFIG
  64.     else
  65.         echo "LAST_BACKED_REV=$newest_rev" > $BAK_CONFIG
  66.     fi
  67. }

  68. # main start
  69. week=$(date | awk '{print $1}')

  70. # only do full backup on Sat.
  71. if [ $week == "Sat" ] ;then
  72.     do_full_backup
  73. else
  74.     do_incremental_backup
  75. fi


  76. # delete 30-days-ago svndump backup
  77. folder=`ls -ltr $SVN_BAK_PATH | grep -v total | awk {'print $9'}`
  78. declare -i linenum=`ls -ltr $SVN_BAK_PATH | grep -v total | wc -l`
  79. declare -i c=$linenum-30
  80. declare -i count=0

  81. if [ "$c" -gt "0" ]
  82. then
  83. for file in $folder
  84. do
  85.     count=count+1
  86.     if [ "$count" -le "$c" ]
  87.     then
  88.     print_log "$file, 30 days before, will be deleted now!"
  89.     rm -rf $SVN_BAK_PATH/$file
  90.     fi
  91. done
  92. fi

  93. # upload svndump file to ftp server, redundancy backup
  94. print_log "start to upload newest backup file to ftp server"
  95. lftp -u ftpuser,private 192.168.26.74 -e "mirror -R --only-newer $SVN_BAK_PATH svn-bk ; quit"
  96. print_log "newest backup file upload to ftp server DONE!"

  97. print_log "all backup DONE!"
  98. print_log ""
  99. exit 0
脚本开始部分,配置SVN库目录,备份目录和日志目录,请修改为自己的目录。

设置定期启动任务,采用ubuntu自带的crontab
crontab -e
0 2 * * * * bash /home/xxx/bin/svn_daily_bak.sh
阅读(2992) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~