设为首页 收藏本站
查看: 542|回复: 0

[经验分享] SuSE下实时文件同步

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-24 08:33:07 | 显示全部楼层 |阅读模式
最近一次内部方案讨论,同事说需要开发一个实时的文件同步功能,哥想到了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代码


    #!/bin/sh
     #
     # Copyright (C) Tony!
     #
     #    Date: 2014-12-05 22:51:09 CST
     #    Version: 0.1
     #    Author:
     #           Tony <tingw.liu#gmail.com>
     # vim set nu; set cindent; set tabstop = 4 ;set softtabstop=4*/


    bluecolor()
    {
        echo -ne "\033[34m"
    }
    redcolor()
    {
        echo -ne "\033[31m"
    }
    greencolor()
    {
        echo -ne "\033[32m"
    }
    yellocolor()
    {
        echo -ne "\033[33m"
    }
    cleanstyle()
    {
        echo -ne "\033[0m"
    }

    help()
    {
        redcolor
        echo -n "Usage: "
        yellocolor
        echo "sourcedir destip[desthostname] destdir"
        cleanstyle
        exit 0
    }

    if [ $# -ne 3 ];then
        help
    fi
    srcdir=$1
    dest=$2
    destdir=$3

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

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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-38542-1-1.html 上篇帖子: OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播 下篇帖子: Linux网络流量控制工具—Netem
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表