免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: flycms
打印 上一主题 下一主题

ssh+tar做备份 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2010-08-18 16:28 |只看该作者
你仔细看 都看得懂。。。

论坛徽章:
0
12 [报告]
发表于 2010-08-18 20:18 |只看该作者
带注释版
  1. #!/bin/bash   
  2. #Copyright(c) 2003-2008 Wang Pengcheng(qianjigui@gmail.com)   
  3. #Backup the important files to the remove-disk   
  4. #Version 0.1.0  
  5. #Support the mtime change, and using the 'find' to increase the behaviour.   
  6. #version 0.0.1  
  7. #basic method   
  8.   
  9. ##############Print the version and the usage#############   
  10. echo 'BackupInfo version 0.1.0'  
  11. echo 'Copyright 2001-2008 by Wang Pengcheng(qianjigui@gmail.com)'  
  12. echo 'Usage: backupInfo [mtime]'  
  13. ###################################################   
  14.   
  15. #the arguments for the rsync   
  16. opts="-a --progress --delete"  
  17. #######################   
  18.   
  19. ######Get the src,dist root Dir##########################   
  20. srcRoot='/home/wpc'    //源路径
  21. distRoot='/media/WPC_/backup' //目标路径  
  22. ##################################################   
  23.   
  24. #########Get backup file List##########################   
  25. runPath='/home/wpc/workspace/MyLib/Shell/bin' //应该是作者自己的一个shell脚本文件路径  
  26. dicListPath="$runPath/dicList.txt"              //
  27. dicList=`cat $dicListPath`                      //需要备份的路径
  28. lastBackTime="$runPath/backupTime.log"              //最近一次备份的时间
  29. if test -e $lastBackTime -a $# -lt 1              //判断一储存最近一次备份时间的文件存在并且参数个数小于1
  30. then   
  31.   mMin=`cat $lastBackTime`                    //获取上次备份时间
  32.   nowTimeS=`date +%s`                         //获取当前时间自unix纪元起的总秒数
  33.   mMin=`expr $nowTimeS - $mMin`                      //获取时间差
  34.   mMin=`expr $mMin / 60`                      //换成分钟
  35.   mMin=`expr $mMin + 10`                      //加十分钟
  36.   findOpts="-amin +$mMin"                     //用作find判断的比较时间,距现在mMin分钟前
  37. else                                              //开始判断二$#等于1
  38.   if [ $# -eq 1 ]            
  39.   then   
  40.     mDay=$1                                      //mDay等于第一个参数  
  41.   else                                              //       
  42.     mDay=3                                      //mDay等于3               
  43.   fi                                              //结束判断二
  44.   findOpts="-mtime +$mDay"                    //用作find判断的比较时间,距现在mDay天前
  45. fi                                              //结束判断一        这一段主要是判断要备哪段时间的数据       
  46. findOpts="$findOpts -type f"                      //find -type f          
  47. echo "The exclude files FIND Pattern: $findOpts"   
  48.   
  49. #################Backup the file#####   
  50. for dirName in $dicList                              //遍历所有需要备份的目录   
  51. do   
  52.   echo   
  53.     echo "***Backup the dictionary $dirName***"  
  54.   srcD="$srcRoot/$dirName/"                   //拼凑需备份的绝对目录名
  55.   distD="$distRoot/$dirName"                  //拼凑目标绝对目录名
  56.   
  57.   if [  -e $distD ]                           //判断目标文件夹是否已经存在
  58.   then   
  59.     echo "The $distD exists."  
  60.   else   
  61.     echo "Create the dictionary: $distD..."  
  62.     mkdir -p $distD                             //递归建立目录
  63.   fi   
  64.      
  65.   ##Create the exclude patterns   
  66.   timeC=`date +%s`                           //时间,格式同前
  67.   regPatternFile="/tmp/regPatterFile$timeC.txt"  //定义储存备份文件名的文件
  68.   
  69.   find $srcD $findOpts |  sed "s|$srcD||" > $regPatternFile  //取出要备份的文件名部分,看不懂得可以参考sed的用法
  70.   excludePattern="--exclude-from=$regPatternFile"  //拼凑参数
  71.      
  72.   #echo "rsync $opts $excludePattern $srcD $distD"  //这种就不用说了
  73.   rsync $opts $excludePattern $srcD $distD | grep -n '[^/]  //备份并顺便输出行号
  74.      
  75.   #delete tmp files                                    //
  76.   rm $regPatternFile                                    //删除临时文件
  77.   echo "***Backup Finish***"     
  78. done   
  79.   
  80. #Write information to the log file   
  81. echo   
  82. echo "Write Log to the $lastBackTime ..."        //更新最后一次备份时间
  83. date +%s > $lastBackTime   
  84. echo "Save Log finished."  
复制代码

论坛徽章:
0
13 [报告]
发表于 2010-08-19 07:59 |只看该作者
为啥不用rsync

论坛徽章:
0
14 [报告]
发表于 2010-08-19 11:48 |只看该作者
不要沉底继续讨论
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP