Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6885407
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类: LINUX

2014-12-10 18:10:50

原文地址:SuSE下实时文件同步 作者:瀚海书香

转载请注明出处:http://forever.blog.chinaunix.net
最近一次内部方案讨论,同事说需要开发一个实时的文件同步功能,哥想到了rsync,于是就有了这篇文章

1. What’s rsync
    Rsync is a fast and extraordinarily versatile file copying tool.  It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon.  It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its  delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.
    Rsync finds files that need to be transferred using a "quick check" algorithm (by default) that looks for files that have changed in size or in last-modified time.  Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file's data does not need to be updated.
2. How to use rsync
    使用rsync需要在同步的两台服务器上同时安装rsync安装包。
    2.1 Rsync同步目录
        rsync --compress --recursive --delete --links --times --perms --owner --group --verbose --progress --stats --rsh="ssh"  /root/testdir/    hadoop_name:/root/tonytest
        --compress   开启压缩模式
        --recursive   对子目录中的文件进行同步
        --delete 同步本地的删除信息(本地删除文件后,也会删除对端的文件)
        --links       对链接文件进行同步
        --times 同步文件的修改时间
        --perms 同步文件的权限
        --owner 同步文件的属主
        --group 同步文件的属组
        --verbose 打印更多的同步信息
        --progress 打印出同步的进度信息
        --rsh 指定对端shell
        --stats 打印出同步的统计信息
        /root/testdir/ 同步的本地源目录
        hadoop_name:/root/tonytest 同步的目的主机和目录
    2.2 Rsync的问题
        Rsync同步目录时会比对所有的文件,再目录非常大的时候,性能低下。
        Rsync不能实时同步,通过cron来触发的机制,会有延迟。
3. What’s Inotify and inotify-tools?
    Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。
Inotify-tools 是对linux特性进行封装的开源工具包。
4. RSYNC&Inotify-tools
    利用Inotify-tools的实时监测文件改动和rsync的同步,可以实现一个实时的目录文件同步系统
5. How to use on SUSE Linux?
    5.1 源站安装相关工具
        安装rsync
            #zypper install rsync
        安装inotify-tools
            #tar zxf inotify-tools-3.14.tar.gz【inotify-tools-3.14.tar.gz见附件】
            #cd inotify-tools-3.14
            #./configure
            #make;make install;ldconfig
        设置无密码ssh登录到目标站
            # ssh-keygen -t rsa
            将~/.ssh/ id_dsa.pub内容追加到目标站~/.ssh/ authorized_keys文件中
    5.2 目标站安装相关工具
        #zypper install rsync
    5.3 使用实例
        # nohup  realsync.sh “sourcedir” “destip[desthostname]” “destdir” &
6. realsync.sh代码

点击(此处)折叠或打开

  1. #!/bin/sh
  2.  #
  3.  # Copyright (C) Tony!
  4.  #
  5.  #    Date: 2014-12-05 22:51:09 CST
  6.  #    Version: 0.1
  7.  #    Author:
  8.  #           Tony
  9.  # vim set nu; set cindent; set tabstop = 4 ;set softtabstop=4*/


  10. bluecolor()
  11. {
  12.     echo -ne "\033[34m"
  13. }
  14. redcolor()
  15. {
  16.     echo -ne "\033[31m"
  17. }
  18. greencolor()
  19. {
  20.     echo -ne "\033[32m"
  21. }
  22. yellocolor()
  23. {
  24.     echo -ne "\033[33m"
  25. }
  26. cleanstyle()
  27. {
  28.     echo -ne "\033[0m"
  29. }

  30. help()
  31. {
  32.     redcolor
  33.     echo -n "Usage: "
  34.     yellocolor
  35.     echo "sourcedir destip[desthostname] destdir"
  36.     cleanstyle
  37.     exit 0
  38. }

  39. if [ $# -ne 3 ];then
  40.     help
  41. fi
  42. srcdir=$1
  43. dest=$2
  44. destdir=$3

  45. #inotifywait -m -r -q --timefmt '%F %T' --format '%T %w%f %e' -e close_write /root/rsynctestdir//|while read file

  46. inotifywait -m -r -q --timefmt '%F %T' --format '%w%f' -e close_write ${srcdir}/|while read file
  47. do
  48.     time=`date +'%F %T %Z'`
  49.     echo "$time RSYNC $file from \"$srcdir\" to \"${dest}:${destdir}\""
  50.     rsync --compress --recursive --links --times --perms --owner --group --verbose --progress --stats --rsh="ssh" $file ${dest}:${destdir}/    
  51. done


阅读(759) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~