免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4178 | 回复: 13
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-18 13:49 |只看该作者 |倒序浏览
本帖最后由 flycms 于 2010-08-19 11:47 编辑

今天看到一个同事的博客写了ssh+tar做备份,有点意思就贴出来吧
  1. 利用 ssh + tar 來作备份,不仅安全,也兼顾了部分的效率.

  2. $target 表示目的机器,指远端机器
  3. $backup_server 表示备份机器,同样也是指远端机器
  4. $save 则是指备份目录

  5. 要求远端备份并拉回 local
  6. ssh $target tar -cf - / --exclude /mnt | bzip2 -9 | cat > $target.tar.bz2

  7. 解开本地备份并回存到远端去
  8. bunzip2 -dc $target.tar.bz2 | ssh $target "cd /;tar -pxkf -"

  9. 备份 local 资料,丟到远端去压缩并存档
  10. tar cf - / --exclude xxx | ssh $backup_server "bzip2 -9 > $save/test.tar.bz2"

  11. 把远端资料拉回 local,作解压缩动作
  12. ssh $backup_server "cat $save/test.tar.bz2" | bunzip2 | tar -xpkf -
复制代码
但是如果网络有原因导致操作中断结果会如何呢,重新备份,还是可以再原基础上继续备份,我想其实是不是可以把先压缩压缩,然后程序判断压缩已经成功刚结束然后自动传到远程主机呢,等待ing...
不能沉底,继续讨论

论坛徽章:
0
2 [报告]
发表于 2010-08-18 13:52 |只看该作者
先顶再看!

论坛徽章:
0
3 [报告]
发表于 2010-08-18 13:55 |只看该作者
scp 也可以呀!

论坛徽章:
0
4 [报告]
发表于 2010-08-18 14:02 |只看该作者
那么网络中断会不会出错呢

论坛徽章:
0
5 [报告]
发表于 2010-08-18 14:11 |只看该作者
丢人,我竟然看不懂

哪位逐条命令解释下啊?

论坛徽章:
1
射手座
日期:2014-03-10 14:24:52
6 [报告]
发表于 2010-08-18 14:31 |只看该作者
不知道ssh还有这功能,据说scp可能能满足需要,是不是就不用tar来tar去的了。

论坛徽章:
0
7 [报告]
发表于 2010-08-18 14:43 |只看该作者
备份放到cron里比较好

论坛徽章:
2
技术图书徽章
日期:2013-09-04 15:21:51酉鸡
日期:2013-11-01 21:20:20
8 [报告]
发表于 2010-08-18 14:47 |只看该作者
备份还是ssh+rsync为好

论坛徽章:
0
9 [报告]
发表于 2010-08-18 15:40 |只看该作者
回复 8# mirnshi 确实是

论坛徽章:
0
10 [报告]
发表于 2010-08-18 16:18 |只看该作者
回复 5# welcome008


    那就贴一段我也看不懂的吧:
  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'  
  26. dicListPath="$runPath/dicList.txt"  
  27. dicList=`cat $dicListPath`   
  28. lastBackTime="$runPath/backupTime.log"  
  29. if test -e $lastBackTime -a $# -lt 1  
  30. then   
  31.   mMin=`cat $lastBackTime`   
  32.   nowTimeS=`date +%s`   
  33.   mMin=`expr $nowTimeS - $mMin`   
  34.   mMin=`expr $mMin / 60`   
  35.   mMin=`expr $mMin + 10`   
  36.   findOpts="-amin +$mMin"  
  37. else   
  38.   if [ $# -eq 1 ]   
  39.   then   
  40.     mDay=$1  
  41.   else   
  42.     mDay=3  
  43.   fi   
  44.   findOpts="-mtime +$mDay"  
  45. fi   
  46. findOpts="$findOpts -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   
  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."  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP