svn自动备份脚本和设置方法-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 752923
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1150
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-17 14:32
个人简介

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

文章分类

全部博文(144)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: 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=count1
  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
阅读(3056) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图