linux centos7安装rsync+sersync实现文件实时自动同步功能

一、rsync+sersync介绍

(转载别人总结的)

1、介绍:

1).  sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;

2).  rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高

2、同步过程:

1).  在源数据服务器上开启sersync服务,sersync负责监控配置路径中的文件系统事件变化;

2).  调用rsync命令把更新的文件同步到目标服务器;

3).  需要在源数据服务器配置sersync,在同步目标服务器配置rsync server

3、同步原理:

1).  用户实时的往sersync服务器上写入更新文件数据;

2).  此时需要在源数据服务器上配置sersync服务;

3).  在另一台服务器开启rsync守护进程服务,以同步拉取来自sersync服务器上的数据;

通过rsync的守护进程服务后可以发现,实际上sersync就是监控本地的数据写入或更新事件;然后,在调用rsync客户端的命令,将写入或更新事件对应的文件通过rsync推送到目标服务器

二、rsync安装与配置

1、现有两台服务器:

A:10.1.1.198  端口:888   有  /www/html

B:10.1.1.198  端口:873   有 /www

C:10.1.1.198  端口:872   有 /www

服务器资源有限B、C服务器是使用docker运行的一个centos容器

docker相关知识请看这篇博客:https://blog.csdn.net/u011477914/article/details/86591452

B服务器 端口:873

docker run --restart=always -p 873:873 -d -e "container=docker" --privileged=true --name rsync centos /usr/sbin/init

C服务器 端口:872 

docker run --restart=always -p 872:873 -d -e "container=docker" --privileged=true --name rsync2 centos /usr/sbin/init

2、A、B、C服务器都安装rsync

#安装rsync
yum -y install rsync

#启动rsync服务
systemctl start rsyncd.service

#开机启动
systemctl enable rsyncd.service

#检查是否已经成功启动
netstat -lnp|grep 873

3、A、B、C服务器配置

vim /etc/rsyncd.conf

 (注意:【A服务器:port = 888】 【B、C服务器:port = 873】因为在docker里面所有可以为873)

#设置运行rsync 进程的用户
uid = root
#运行进程的组
gid = root
#端口,如888,870等等只要不被占用,防火墙开启就可以,这里需要修改
port = 873
#如果"use chroot"指定为true,那么rsync在传输文件以前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但是缺 点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true。
use chroot = yes
#最大连接数
max connections = 5
#CentOS7中yum安装不需指定pid file 否则报错
# pid file = /var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
#日志文件
log file = /var/log/rsyncd.log
#此文件定义完成后系统会自动创建
exclude = lost+found/
transfer logging = yes
#超时时间
timeout = 900
#同步时跳过没有权限的目录
ignore nonreadable = yes  
#传输时不压缩的文件
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2


#规则名称,作为测试用规则,直接用这个算了。
[helloRsync]
#同步的路径 提前创建好
path = /www
#规则描述
comment = 测试规则
#忽略错误
ignore errors
#是否可以pull 设置服务端文件读写权限
read only = no
#是否可以push
write only = no
#不显示服务端资源列表
list = false
#下面配置同步时候的身份,注意该身份是在rsync里面定义的,并非是本机实际用户。等下说说如何在rsync里面定义身份。
#客户端获取文件的身份此用户并不是本机中确实存在的用户
#该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块
auth users = admin
#用来认证客户端的秘钥文件 格式 USERNAME:PASSWD 此文件权
#限一定需要改为600,且属主必须与运行rsync的用户一致。
secrets file = /etc/rsync.password
#允许所有主机访问  *代表所有
hosts allow = *

4、A、B、C服务器给rsync定义身份,如下:

echo 'admin:123456'>/etc/rsync.password
echo '123456'>/etc/rsync.server.password

5、A、B、C服务器设置权限

chmod 600 /etc/rsync.password

6、A、B、C服务器修改完成后,重启

systemctl restart rsyncd.service

 7、在A服务器执行:把A服务器 /www/html  同步到B服务器  /www (服务器B开始没有html,同步后出现html)

rsync -vzrtopg --progress /www/html  [email protected]::helloRsync --password-file=/etc/rsync.server.password

这样表示同步成功,然后到B服务器中/www目录下执行ls发现多了一个html目录

先进入docker容器:docker exec -it rsync /bin/bash

8、在A服务器执行:把B服务器的 /www/html 同步到A服务器 /www/html

rsync -vzrtopg  --progress [email protected]::helloRsync /www/html --password-file=/etc/rsync.server.password

9、在A服务器执行,访问C服务器

rsync -vzrtopg --port 872 --progress  /www/html [email protected]::helloRsync --password-file=/etc/rsync.server.password

10、在B,C服务器执行,需要加上端口 --port 888

rsync -vzrtopg --port 888  --progress /www/html [email protected]::helloRsync --password-file=/etc/rsync.server.password

 

三、Sersync安装与配置(暂时安装在A服务器)

1、下载sersync

Git Hub 镜像地址用本地电脑浏览器打开下 : https://github.com/orangle/sersync

然后使用ftp工具XftpPortable上传到服务器上/home/soft。

2、安装和修改配置

cd /home/soft
unzip sersync-master.zip 
tar xf sersync-master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
cd /usr/local/
mv GNU-Linux-x86/ sersync
cd sersync/
cp confxml.xml confxml.xml.$(date +%F)
vim confxml.xml

同步流程 A->B->C->A

修改confxml.xml文件的标红部分:

A服务器:


 修改24、25行
    <sersync>
        <localpath watch="/www">  # 本地同步目录
            <remote ip="10.1.1.198" name="helloRsync"/> # rsync模块名 ??

# 修改31、32行
    <rsync>
         <commonParams params="-artuz"/>
            <auth start="true" users="admin" passwordfile="/etc/rsync.server.password"/>
            <userDefinedPort start="true" port="873"/><!-- port=874 -->


B服务器:


 修改24、25行
    <sersync>
        <localpath watch="/www">  # 本地同步目录
            <remote ip="10.1.1.198" name="helloRsync"/> # rsync模块名 ??

# 修改31、32行
    <rsync>
         <commonParams params="-artuz"/>
            <auth start="true" users="admin" passwordfile="/etc/rsync.server.password"/>
            <userDefinedPort start="true" port="872"/><!-- port=874 -->


A服务器:


修改24、25行
    <sersync>
        <localpath watch="/www">  # 本地同步目录
            <remote ip="10.1.1.198" name="helloRsync"/> # rsync模块名 ??

# 修改31、32行
    <rsync>
         <commonParams params="-artuz"/>
            <auth start="true" users="admin" passwordfile="/etc/rsync.server.password"/>
            <userDefinedPort start="true" port="888"/><!-- port=874 -->


confxml.xml文件修改后:

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
    </inotify>

    <sersync>
        <localpath watch="/www">
            <remote ip="10.1.1.198" name="helloRsync"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="admin" passwordfile="/etc/rsync.server.password"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <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>

3、开启sersync守护进程 同步数据

./sersync2 -d -r -o /usr/local/sersync/confxml.xml

注意:如果直接执行: sersync2 -d -r -o /usr/local/sersync/confxml.xml

会提示报错:bash: sersync2: command not found...

四、测试

最后在A服务器的/www/html目录下随意的添加和删除文件

然后在B服务器的/www/html目录ls会发现文件同步过来,删除的时候也会同步删除

如有问题欢迎留言沟通!!

猜你喜欢

转载自blog.csdn.net/u011477914/article/details/88555261