linux文件同步方案:rsync+sersync实现服务器间文件同步

拓扑图

如图,目的是将A中的指定文件实时同步到B和C的指定目录下。在图中我们将A成为source或client,将B和C称为target或server。

 

我们在source和target上都安装rsync以实现文件的远程传输;在source中安装sersync以实现对指定文件/文件夹的变化进行监听并执行rsync文件同步命令;在target中开启rsync daemon模式以监听其他client发起的rsync请求。

 

具体需求如下:

1.source   192.168.188.130  /data/source

2.target    192.168.188.131  /data/target

当source中/data/source的文件发生变化时,实时增量同步到target的/data/target目录下。

 

一、source和target上都安装rsync

1.若未安装rsync,则安装rsync

[root@target ~]# rsync --version

rsync  version 2.6.8  protocol version 29

Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.

<http://rsync.samba.org/>

Capabilities: 64-bit files, socketpairs, hard links, ACLs, xattrs, symlinks, batchfiles,

              inplace, IPv6, 64-bit system inums, 64-bit internal inums



rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you

are welcome to redistribute it under certain conditions.  See the GNU

General Public Licence for details.

可以看到已存在2.6.8版本,版本太低,将其升级为3.0.9。先卸载当前版本:

[root@target ~]# yum remove rsync

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

Setting up Remove Process

Resolving Dependencies

--> Running transaction check

---> Package rsync.i386 0:2.6.8-3.1 set to be erased

--> Finished Dependency Resolution

…

Is this ok [y/N]: y

…

Complete!

rsync-3.0.9.tar.gz上传到/tmp,然后安装3.0.9版本:

[root@target ~]# cd /tmp

[root@target tmp]# tar zxf rsync-3.0.9.tar.gz

[root@target tmp]# cd rsync-3.0.9

[root@target rsync-3.0.9]# ./configure

[root@target rsync-3.0.9]# make && make install

2.默认安装到了/usr/local/bin.rsync,配置环境变量:

[root@ localhost rsync-3.0.9]# vim /etc/profile.d/rsync.sh

#!/bin/sh

export PATH=$PATH:/usr/local/bin/rsync/bin

3.建立软连接:

[root@target rsync-3.0.9]# ln -s /usr/local/bin/rsync /usr/bin/rsync

 

4.安装完后查看版本,确定是否安装成功:

[root@target /]# rsync --version

rsync  version 3.0.9  protocol version 30

Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.

Web site: http://rsync.samba.org/

Capabilities:

    64-bit files, 64-bit inums, 32-bit timestamps, 64-bit long ints,

    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,

    append, no ACLs, xattrs, iconv, no symtimes



rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you

are welcome to redistribute it under certain conditions.  See the GNU

General Public Licence for details.

二、target上开启daemon模式

rsync daemon是"rsync --daemon"或再加上其他一些选项启动的,它会读取配置文件,默认是/etc/rsyncd.conf,并默认监听在873端口上,当外界有客户端对此端口发起连接请求,通过这个网络套接字就可以完成连接,以后与该客户端通信的所有数据都通过该网络套接字传输。

 

  1. 创建需要的目录和文件
[root@target /]# mkdir /etc/rsyncd/    #创建配置文佳的目录

[root@target /]# cd /etc/rsyncd/       

[root@target rsyncd]# touch rsyncd.conf   #创建rsync的配置文件

[root@target rsyncd]# touch rsyncd.pw    #创建rsync的密码文件

[root@target rsyncd]# chmod -R 600 .     #将文件权限设置为root

 

2.编辑配置文件(注意在复制时不要保留注释,此处仅为解释说明用)

[root@target rsyncd]# vim rsyncd.conf



uid = root       #rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid

gid = root       #rsync服务的运行组,默认是nobody,文件传输成功后属组将是这个gid

max connections = 100          #最大连接数

timeout = 300         # 确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待

use chroot = no

log file = /var/rsync/rsync.log       #指定同步日志

pid file = /var/rsync/rsyncd.pid     #指定运行时PID文件

lock file = /var/rsync/rsyncd.lock

dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  # 指定哪些文件不用进行压缩传输



###########下面指定模块,并设定模块配置参数,可以创建多个模块###########

[hr_syn]        # 模块ID

path = /data/target/ # 指定该模块的路径,该参数必须指定。启动rsync服务前该目录必须存在。rsync请求访问模块本质就是访问该路径。

ignore errors = true      # 忽略某些IO错误信息

read only = false  # 指定该模块是否可读写,即能否上传文件,false表示可读写,true表示可读不可写。所有模块默认不可上传

write only = false # 指定该模式是否支持下载,设置为true表示客户端不能下载。所有模块默认可下载

list = false       # 客户端请求显示模块列表时,该模块是否显示出来,设置为false则该模块为隐藏模块。默认true

hosts allow = 192.168.188.130/24 # 指定允许连接到该模块的机器,多个ip用空格隔开或者设置区间

hosts deny = 0.0.0.0/32   # 指定不允许连接到该模块的机器

auth users = rsync_user # 指定连接到该模块的用户列表,只有列表里的用户才能连接到模块,用户名和对应密码保存在secrts file中,

                          # 这里使用的不是系统用户,而是虚拟用户。不设置时,默认所有用户都能连接,但使用的是匿名连接

secrets file = /etc/rsyncd/rsyncd.pw # 保存auth users用户列表的用户名和密码,每行包含一个username:passwd。由于"strict modes"

                                  # 默认为true,所以此文件要求非rsync daemon用户不可读写。只有启用了auth users该选项才有效。
  1. 编辑密码文件
[root@target rsyncd]# vim rsyncd.pw

rsync_user:123456
  1. 启动rsync_daemon守护进程
[root@target rsyncd]# mkdir /var/rsync/

[root@target rsyncd]# rsync --daemon --config=/etc/rsyncd/rsyncd.conf

 

4.默认监听873端口,如果有启用iptables,则放行873端口:

[root@target rsyncd]# /sbin/iptables -I INPUT -p tcp --dport 873 -j ACCEPT

[root@target rsyncd]# /etc/rc.d/init.d/iptables save

[root@target rsyncd]# service iptables restart

 

  1. 查看iptables状态
[root@target rsyncd]# /etc/init.d/iptables status

 

三、source上安装sersync并启动sersync服务

1.安装sersync

先将sersync2.5.4_64bit_binary_stable_final.tar.gz上传到source的/tmp文件夹,然后进行安装:

[root@livedvd ~]# cd /tmp/

[root@livedvd tmp]# tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@livedvd tmp]# mv GNU-Linux-x86 /usr/local/bin/sersync

 

2.修改环境变量配置

[root@livedvd sersync]# vim /etc/profile.d/sersync.sh

#!/bin/sh

export PATH=$PATH:/usr/local/bin/sersync/

3.编辑sersync配置文件

[root@livedvd /]# vim /usr/local/bin/sersync/confxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<head version="2.5">

    <host hostip="localhost" port="8999"></host>

    <debug start="false"/>           # 是否开启调试模式,下面所有出现false和true的地方都分别表示关闭和开启的开关

    <fileSystem xfs="false"/>        # 监控的是否是xfs文件系统

    <filter start="false">           # 是否启用监控的筛选功能,筛选的文件将不被监控

        <exclude expression="(.*)\.svn"></exclude>

        <exclude expression="(.*)\.gz"></exclude>

        <exclude expression="^info/*"></exclude>

        <exclude expression="^static/*"></exclude>

    </filter>

    <inotify>                         # 监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder

        <delete start="false"/>  #不监控文件或目录的删除

        <createFolder start="true"/>

        <createFile start="false"/> #不监控文件的创建

        <closeWrite start="true"/> # 通过监听文件的关闭,实现监听文件的增改

        <moveFrom start="true"/>

        <moveTo start="true"/>

        <attrib start="false"/>

        <modify start="false"/>

    </inotify>



    <sersync>                       # rsync命令的配置段

        <localpath watch="/data/source">    # 同步的目录或文件,同inotify+rsync一样,建议同步目录

            <remote ip="192.168.188.131" name="hr_syn"/>  # 目标地址和rsync daemon的模块名,所以远端要以daemon模式先运行好rsync

            <!--remote ip="IPADDR" name="module"-->     # 除非下面开启了ssh start,此时name为远程shell方式运行时的目标目录

        </localpath>

        <rsync>                      # 指定rsync选项

            <commonParams params="-az"/>

            <auth start="true" users="rsync_user" passwordfile="/etc/rsyncd/rsyncd.pw"/> #此处填写target对应的rsync用户名,指定密码文件,密码文件中的密码为target端指定的密码

            <userDefinedPort start="false" port="874"/><!-- port=874 -->

            <timeout start="false" time="100"/><!-- timeout=100 -->

            <ssh start="false"/>      # 是否使用远程shell模式而非rsync daemon运行rsync命令

        </rsync>

        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  # 错误重传

        <crontab start="false" schedule="600"><!--600mins-->    # 是否开启crontab功能

            <crontabfilter start="false">       # crontab定时传输的筛选功能

                <exclude expression="*.php"></exclude>

                <exclude expression="info/*"></exclude>

            </crontabfilter>

        </crontab>

        <plugin start="false" name="command"/>

    </sersync>



    <plugin name="command">

        <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->

        <filter start="false">

            <include expression="(.*)\.php"/>

            <include expression="(.*)\.sh"/>

        </filter>

    </plugin>



    <plugin name="socket">

        <localpath watch="/opt/tongbu">

            <deshost ip="192.168.138.20" port="8009"/>

        </localpath>

    </plugin>

    <plugin name="refreshCDN">

        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">

            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>

            <sendurl base="http://pic.xoyo.com/cms"/>

            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>

        </localpath>

    </plugin>

</head>

4.编辑密码文件

[root@livedvd /]# mkdir /etc/rsyncd/

[root@livedvd /]# touch /etc/rsyncd/rsyncd.pw

[root@livedvd /]# vim /etc/rsyncd/rsyncd.pw

123456

[root@livedvd /]#chmod 600 /etc/rsyncd/rsyncd.pw

5.创建日志文件

[root@livedvd /]# mkdir /var/rsync

[root@livedvd /]# touch /var/rsync/rsync_error.log

6.开启sersync服务

[root@livedvd sersync]# /usr/local/bin/sersync/sersync2 -r -d -o /usr/local/bin/sersync/confxml.xml

 

四、测试

1. 向source的监听目录中新建一个文件

[root@livedvd ~]# cd /data/source/

[root@livedvd source]# touch test1.txt

2.查看target的/data/target,发现test1.txt并没有同步过来。查看同步日志:

[root@localhost rsync]# cd /var/rsync/

[root@localhost rsync]# tail -f rsync.log

2019/10/07 17:06:58 [18614] name lookup failed for 192.168.188.130: Name or service not known

2019/10/07 17:06:58 [18614] connect from UNKNOWN (192.168.188.130)

2019/10/07 17:06:58 [18614] rsync: connection unexpectedly closed (21 bytes received so far) [Receiver]

2019/10/07 17:06:58 [18614] rsync error: error in rsync protocol data stream (code 12) at io.c(605) [Receiver=3.0.9]

3.需要将192.168.188.130添加到/etc/hosts中:

[root@localhost rsync]# vim /etc/hosts

192.168.188.130  192.168.188.130

4.再次修改source中的test1.txt文件

[root@livedvd source]# touch test1.txt

 

5.此时再查看target的/data/target路径,test1.txt成功同步

[root@localhost rsync]# cd /data/target/

[root@localhost target]# ls

test1.txt

附件:

rsync-3.0.9.tar.gz:https://download.csdn.net/download/jc_workspace/11834690

sersync2.5.4_64bit_binary_stable_final.tar.gz:https://download.csdn.net/download/jc_workspace/11834691

特别感谢:

https://www.cnblogs.com/f-ck-need-u/p/7220009.html#auto_id_8

https://www.cnblogs.com/cnsanshao/p/4024126.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值