阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS7 Rsync服务搭建-Rsync+Inotify架构实现实时同步

235次阅读
没有评论

共计 4482 个字符,预计需要花费 12 分钟才能阅读完成。

关于 CentOS7 版本上面搭建 rsync 服务并且实现实时同步
之前一直是在 6 版本上面搭建 rsync 服务,在 7 版本上面折腾了半天。此处总结下
inotify 下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

环境说明:

centos7 版本上面已经默认安装了 rsync 和 xinetd 服务

[root@rh6 ~]# rpm -ql rsync           – 在 6 版本上面执行此命令可以看到 xinetd 服务配置文件下面有 rsync 的子配置文件

/etc/xinetd.d/rsync

[root@rh6 ~]# ls /etc/xinetd.d/sync

/etc/xinetd.d/rsync

[root@centos7_3 ~]# rpm -ql rsync          – 在 7 版本上面执行此命令却找不到这样的配置文件,ls 查看提示没有此文件

[root@centos7_3 ~]# ls /etc/xinetd.d/sync

ls: cannot access /etc/xinetd.d/rsync: No such file or directory

————————————————————————————————————————————————————————

实验环境:两台 centos7 版本的宿主机

  1. 客户端:172.16.27.25
  2. 服务端:172.16.27.26

具体步骤:

  • 服务端
  • [root@cc4 ~]# rpm -aq |grep xinetd
    xinetd-2.3.15-13.el7.x86_64
    [root@cc4 ~]# rpm -aq |grep rsync
    rsync-3.0.9-17.el7.x86_64
  • [root@cc4 ~]# vim /etc/xinetd.d/rsync   – 说明:修改 xinetd 服务下面的配置文件,将 rsync 交给 xinetd 管理。此配置文件在 7 版本默认上面是没有的,我是直接从 6 版本复制过来的,也可以手写下面内容
    # default: off
    # description: The rsync server is a good addition to an ftp server, as it \
    #      allows crc checksumming etc.
    service rsync
    {
            disable              = no      — 将原来的 yes 改为no
            flags                  = IPv6
            socket_type      = stream
            wait                  = no
            user                  = root
            server                = /usr/bin/rsync
            server_args      = –daemon
            log_on_failure  += USERID
    }
  • [root@cc4 ~]# vim /etc/rsyncd.conf      – 修改配置文件发布共享目录
    [test]
            path = /test                – 本地共享目录
            auth user = user1      – 指定用户同步
            secrets file = /etc/rsyncd.secrets      – 指定保存用户密码文件
  • [root@cc4 ~]# systemctl restart xinetd    – 启动服务
  • [root@cc4 ~]# vim /etc/rsyncd.secrets    – 创建安全用户
    user1:123
  • [root@cc4 ~]# chmod 600 /etc/rsyncd.secrets    – 设置安全用户文件的权限,不能被其他人查看
  • [root@cc4 ~]# mkdir /test     – 创建共享目录
    [root@cc4 ~]# touch /test/file{1..10}    – 此处为了测试,创建几个文件
    [root@cc4 ~]# ls /test/
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
  •  
  • 客户端测试:
  • [root@cc3 ~]# rsync -a 172.16.27.26::
    test   
  • [root@cc3 date]# ls
    [root@cc3 date]# rsync -av user1@172.16.27.26::test /date
    receiving incremental file list
    ./
    file1
    file10
    file2
    file3
    file4
    file5
    file6
    file7
    file8
    file9
    sent 219 bytes  received 524 bytes  1486.00 bytes/sec
    total size is 0  speedup is 0.00
    [root@cc3 date]# ls        – 可以看见成功从上面同步过来了。
    file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
  •  
  • 通过 inotify+rsync 架构实现实时同步
  • 环境说明:要求 node1 主机上面的 /node1 目录发生新的创建,删除,移动,以及文件属性信息改变时,自动同不到 node2 主机的 /node2 目录
  • node1 主机:IP  172.16.27.25
    node2 主机:IP  172.16.27.26
  • 具体步骤:
  • node1 端:
  • [root@cc3 tmp]# ls /tmp/inotify-tools-3.14.tar.gz
    /tmp/inotify-tools-3.14.tar.gz
  • [root@cc3 tmp]# tar -xf inotify-tools-3.14.tar.gz
    [root@cc3 tmp]# cd inotify-tools-3.14/
    [root@cc3 inotify-tools-3.14]# ls
    aclocal.m4  ChangeLog    config.h.in  configure    COPYING  INSTALL    libinotifytools  Makefile.am  man      NEWS    src
    AUTHORS    config.guess  config.sub  configure.ac  depcomp  install-sh  ltmain.sh        Makefile.in  missing  README
    [root@cc3 ~]# ./configure && make && make install    – 安装软件
  • [root@cc3 ~]# vim /tmp/1.sh
    #!/bin/bash
    /usr/local/bin/inotifywait -mrq -e modify,create,move,delete,attrib /node1 |while read events
            do
            rsync -a –delete /node1 172.16.27.26::test
            echo “`date +’%F %T’` 出现事件 $events” >>/tmp/rsync.log 2>&1
            done
    [root@cc3 ~]#mkdir /node1
    [root@cc3 ~]# touch /node1/testa{1..5}

  • node2 端:
  • [root@cc4 ~]# vim /etc/rsyncd.conf
    [test]
            path = /node2/
            read only = false
            uid = root
            gid = root
    [root@cc4 ~]# mkdir /node2
    [root@cc4 ~]# ls /node2/       – 此时查看,该目录里面什么文件都没有
    [root@cc4 ~]# systemctl restart xinetd
  • 测试验证:
    在 node1 端执行脚本并且放在后台执行, 并且创建文件,再看 tmp 下面是否有创建文件后脚本执行报的日志信息
    [root@cc3 ~]# nohup sh /tmp/1.sh &
    [1] 14720
    [root@cc3 ~]# nohup: ignoring input and appending output to‘nohup.out’  – 此处直接回车

    [root@cc3 ~]# echo haha >>/node1/file1
    [root@cc3 ~]# ls /tmp/rsync.log
    /tmp/rsync.log
    [root@cc3 ~]# cat /tmp/rsync.log
    2017-10-10 15:55:01 出现事件 /node1/ CREATE file1
    2017-10-10 15:55:01 出现事件 /node1/ MODIFY file1

    在 node2 端检查验证
    [root@cc4 ~]# ls /node2/
    file1  testa1  testa2  testa3  testa4  testa5
    [root@cc4 ~]# cat /node2/file1
    haha
    ——— 测试发现在 node1 端创建的文件实时同步过来到 node2 上面了。

  • 总结:其实在 6 系统和 7 系统上面搭建 rsync 服务是一样的,只是在 7 上面改版了,系统默认没有 /etc/xinetd.d/rsync 配置文件,需要自己手动创建。其余配置完全一样

 

inotify+rsync 实现数据实时同步  http://www.linuxidc.com/Linux/2017-10/147901.htm

sersync 实现数据实时同步  http://www.linuxidc.com/Linux/2017-10/147899.htm

CentOS 6.5 rsync+inotify 实现数据实时同步备份 http://www.linuxidc.com/Linux/2016-11/137655.htm

rsync+inotify 实现数据的实时同步 http://www.linuxidc.com/Linux/2017-01/139778.htm

rsync+inotify 实现服务器之间文件实时同步详解  http://www.linuxidc.com/Linux/2016-11/137659.htm

Rsync 结合 Inotify 实时同步配置  http://www.linuxidc.com/Linux/2017-02/140877.htm

RSync 实现数据备份  http://www.linuxidc.com/Linux/2017-06/144913.htm

rsync+inotify 实现数据的实时备份  http://www.linuxidc.com/Linux/2016-11/137630.htm

rsync+inotify 实现数据自动同步  http://www.linuxidc.com/Linux/2017-03/141717.htm

使用 rsync 实现数据实时同步备份  http://www.linuxidc.com/Linux/2017-05/143462.htm

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-11/148661.htm

 

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4482字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中