zoukankan      html  css  js  c++  java
  • linux rsync +inotify 实现 实时同步

    前言:
         rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

    一、基本环境

    系统:CentOS 2.6.32-220.el6.x86_64
    软件包版本:rsync-3.0.6-12.el6.x86_64 
                       inotify-tools-3.14 
    下载链接:百度   inotify-tools-3.14  

    服务端(server):172.16.1.1
    客服端(client1):172.16.1.2
             (client2):172.16.1.3

    二、客户端配置(172.16.1.2、172.16.1.3)

    1. client1 172.16.1.2配置

    安装rsync
    #yum install -y rsync xinetd

    在/etc/目录下建立rsyncd.conf配置文件进行编辑
    #vim /etc/rsyncd.conf 

    uid = nobody         //rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,
    gid = nobody         //rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,
    use chroot = no    //允许 chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下 #chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
    max connections = 10  //最大连接数
    strict modes = yes   //是否检查口令文件的权限
    pid file = /var/run/rsyncd.pid  //pid文件的存放位置
    lock file = /var/run/rsync.lock  //支持max connections参数的锁文件
    log file = /var/log/rsyncd.log   //日志文件位置,启动rsync后自动产生这个文件,无需提前创建
    [lixuan]   //自定义名称
    path = /data/lixuan/          #本地自定义路径
    comment = client file   //模块名称与自定义名称相同
    ignore errors             //忽略错误
    read only = no         //设置rsync服务端文件为读写权限
    write only = no        //设置rsync服务端文件为读写权限
    hosts allow = 172.16.1.1       #服务端IP地址
    hosts deny = *                    //止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    list = false                           //不显示rsync服务端资源列表
    uid = root                           //设置rsync运行权限为root
    gid = root                          //设置rsync运行权限为root
    auth users = root               //模块验证用户名称,可使用空格或者逗号隔开多个用户名
    secrets file = /etc/client1.pass     #自动同步密码文件

    新建/etc/client1.pass  文件
    #echo "root:123456" >>/etc/client1.pass
    #chmod -R 600 /etc/client1.pass   #这个 必须是 600权限  要不就会提示 找不到文件


    启动rsync服务
    #/etc/init.d/xinetd start

    #rsync --daemon --config=/etc/rsync.conf

    2. client2 172.16.1.3配置

    安装rsync
    #yum install -y rsync xinetd

    在/etc/目录下建立rsyncd.conf配置文件进行编辑
    #vim /etc/rsyncd.conf 

    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 10
    strict modes = yes
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    [lixuan]
    path = /data/lixuan/          #本地自定义路径
    comment = client file
    ignore errors
    read only = no
    write only = no
    hosts allow = 172.16.32.204      #服务端IP地址
    hosts deny = *
    list = false
    uid = root
    gid = root
    auth users = root
    secrets file = /etc/client2.pass     #自动同步密码文件

    保存退出!

    新建/etc/client2.pass  文件
    #echo "root:123456" >>/etc/client2.pass
    chmod -R 600 /etc/client2.pass #这个 必须是 600权限  要不就会提示 找不到文件


    启动rsync服务
    #/etc/init.d/xinetd start

    #rsync --daemon --config=/etc/rsync.conf

    三、服务端配置(172.16.1.1)

    1.安装rsync 

    #yum install -y rsync xinetd


    在/etc/目录下建立rsyncd.conf配置文件进行编辑
    #vim /etc/rsyncd.conf 

    uid = root
    gid = root
    use chroot = no
    max connections = 100
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    secrets file = /etc/server.pass
    [lixuan]
    path = /data/lixuan/
    auth users = root
    list = no
    read only = no
    secrets file = /etc/servers.pass
    comment = server directory

    保存退出!

    新建/etc/server.pass  文件
    #echo "root:123456" >>/etc/server.pass
    #chmod -R 600 /etc/server.pass #这个 必须是 600权限  要不就会提示 找不到文件


    启动rsync服务
    #/etc/init.d/xinetd start

    2. 安装inotify

    验证内核是否支持inotify
    #uname -r
    2.6.32-220.el6.x86_64

    #ll /proc/sys/fs/inotify
    total 0
    -rw-r--r-- 1 root root 0 Jun 11 10:15 max_queued_events    #表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值
    -rw-r--r-- 1 root root 0 Jun 11 10:15 max_user_instances     #表示每一个real user ID可创建的inotify instatnces的数量上限
    -rw-r--r-- 1 root root 0 Jun 11 10:15 max_user_watches      #表示每个inotify instatnces可监控的最大目录数量

     


    配置服务端内容发布脚本
    #vim /data/sh/inotifyrsync.sh

    #!/bin/bash
    client1=172.16.1.2
    client2=172.16.1.3
    src=/data/lixuan/
    dst=lixuan
    user=root

    /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib $src | while read files
            do
      /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client1::$dst
      /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/client.pass $src $user@$client2::$dst
                    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
             done

    保存退出!

    新建/etc/client.pass  文件
    #echo "123456" >>/etc/client.pass
    #chmod -R 600 /etc/client.pass #这个 必须是 600权限  要不就会提示 找不到文件

                                                                    
    赋予脚本执行权限
    #chmod 755 /data/sh/inotifyrsync.sh

    后台执行脚本
    #sh /data/sh/inotifyrsync.sh &

    将此脚本加入开机自启动文件中
    #echo "/data/sh/inotifyrsync.sh &"  >> /etc/rc.local

    #rsync --daemon --config=/etc/rsync.conf

    四、测试rsync+inotify数据实时同步

          在服务端(172.16.1.1)的/data/lixuan/目录中添加删除目录或文件,然后进入客户端(172.16.1.1、172.16.1.2)的/data/lixuan/目录中查看是否和服务端数据实时保持一致。

    参考文章 http://www.tuicool.com/articles/IB3I7rm

  • 相关阅读:
    Python界面常用GUI包
    ATSC/DVB/ISDB三大标准比较
    【MPEG】DVB / ATSC / ISDB区别
    Python判断字符串是否为字母或者数字
    MApp_ZUI_CTL_MarqueeTextWinProc字串滚动
    PWM原理及其在电源中的应用
    按键板的原理与实现 扩展GPIO
    按键板的原理与实现----ADC
    使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比
    SQL Server备份还原数据库中的小把戏
  • 原文地址:https://www.cnblogs.com/wspblog/p/4593701.html
Copyright © 2011-2022 走看看