CentOS7.X通过rsync+sersync实现文件实时同步

本文介绍如何使用rsync和sersync实现服务器间文件的双向及多向同步。内容涵盖从安装配置到测试验证的全过程,并提供开机启动设置等实用技巧。

服务器文件双向、多向同步rsync+sersync

  1. 原文地址https://note.so1234.top
  2. 由于要关闭SELinux,文档内容仅作参考
  3. 安装前的准备(各同步服务器都配置)

    # 因为这会降低服务器异常报警的级别,所以记得添加报警机制
    # SELINUX=disabled(完全不验证)
    vim /etc/sysconfig/selinux
    
    SELINUX=permissive
    
    ESC
    :wq
    
    getenforce
    setenforce 0
    getenforce
  4. 安装rsync(各同步服务器都装)

    yum install rsync -y
  5. 配置rsyncd.conf(各服务器都配置)

    # uid、gid决定被同步过来的文件所属用户所属组
    # []中是一个自己指定的同步计划的名字(字母数字下划线)
    # path是被同步文件的存放位置
    # hosts allow这里注意,每一台服务器都只写其它服务器IP
    # hosts allow、hosts deny格式
        # *(所有IP)
        # 192.168.0.2/24(IP段)
        # 192.168.0.2 192.168.0.3(指定多个IP)
    
    vim /etc/rsyncd.conf
    
    uid = www
    gid = www
    use chroot = yes
    max connections = 0
    log file = /tmp/rsyncd.log
    motd file = /tmp/rsyncd.motd
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    secrets file = /etc/rsyncd.pswd
    
    [tongbu1]
        path = /www/file1
        comment = xoyo video files
        ignore errors = yes
        read only = no
        hosts allow = 192.168.0.x 192.168.0.x
        hosts deny = *
    
    [tongbu2]
        path = /www/file2
        comment = xoyo video files
        ignore errors = yes
        read only = no
        hosts allow = 192.168.0.x 192.168.0.x
        hosts deny = *
    
    ESC
    :wq
  6. 配置密码文件

    vim /etc/rsyncd.pswd
    
    root:**************
    
    ESC
    :wq
    
    chmod 600 /etc/rsyncd.pswd
    
    vim /etc/sersyncd.pswd
    
    **************
    
    ESC
    :wq
  7. 创建同步目录(各服务器都创建)

    useradd www
    mkdir /www
    mkdir /www/file1
    mkdir /www/file2
    chown -R www:www /www/file1
    chown -R www:www /www/file2
  8. 启动rsync(各服务器都启动)

    rsync --daemon
  9. 查看占用的端口号

    netstat –apn|grep rsync
  10. 开启端口

    yum install firewalld -y
    systemctl enable firewalld
    systemctl start firewalld
    firewall-cmd --zone=public --permanent --add-port=873/tcp
    firewall-cmd --reload
  11. 创建单元文件(如果是用yum装的的rsync,则跳过该步骤)

    # 进入单元文件目录,创建rsync单元文件
    cd /etc/systemd/system
    vim rsyncd.service
    
    [Unit]
    Description=Start rsync on boot.
    After=default.target network.target
    
    [Service]
    User=root
    Group=root
    Type=forking
    PIDFile=/var/run/rsyncd.pid
    ExecStart=/usr/bin/rsync --daemon
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=false
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    ESC
    :wq
    
    # 修改文件权限为只有root用户可以编辑该文件
    chown -R root:root /etc/systemd/system/rsyncd.service
    chmod -R 644 /etc/systemd/system/rsyncd.service
    systemctl daemon-reload
  12. 启动并设置开机启动

    systemctl enable rsyncd
    systemctl start rsyncd
  13. 安装sersync(各服务器都装)

    # 源码
    https://code.google.com/archive/p/sersync/
    # 百度云
    https://pan.baidu.com/s/1qrwTM40aJ8ckmT84PpiNjg
    # 64位最新
    https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5.4_64bit_binary_stable_final.tar.gz
    # 32位最新
    https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5_32bit_binary_stable_final.tar.gz
    # 下载安装
    wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5.4_64bit_binary_stable_final.tar.gz
    tar -zxvf sertongbu2.5.4_64bit_binary_stable_final.tar.gz
    mv GNU-Linux-x86/ /usr/local/sersync
  14. 配置sersync(各服务器都执行)

    cd /usr/local/sersync
    cp /usr/local/sersync/confxml.xml /usr/local/sersync/tongbu1.xml
    cp /usr/local/sersync/confxml.xml /usr/local/sersync/tongbu2.xml
    
    vim /usr/local/sersync/tongbu1.xml
    
    <sersync>
        <localpath watch="/www/file1">
            <!--这里注意,每一台服务器都只写其它服务器IP-->
            <remote ip="192.168.0.2" name="tongbu1"/>
            <remote ip="192.168.0.3" name="tongbu1"/>
            <remote ip="192.168.0.4" name="tongbu1"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <!--如果设置了密码,则密码文件的路径、内容必须正确 start="true"-->
            <auth start="true" users="root" passwordfile="/etc/sersyncd.pswd"/>
            <userDefinedPort start="false" port="874"/>
            <timeout start="false" time="100"/>
            <ssh start="false"/>
        </rsync>
        <!--60分钟进行一次同步失败文件重新同步-->
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
        <!--600分钟进行一次全目录同步-->
        <crontab start="true" schedule="600">
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <!--因为这里start="false" 所以下面plugin的配置无需关心-->
        <plugin start="false" name="command"/>
    </sersync>
    
    ESC
    :wq
    
    vim /usr/local/sersync/tongbu2.xml
    
    <sersync>
        <localpath watch="/www/file2">
            <!--这里注意,每一台服务器都只写其它服务器IP-->
            <remote ip="192.168.0.2" name="tongbu2"/>
            <remote ip="192.168.0.3" name="tongbu2"/>
            <remote ip="192.168.0.4" name="tongbu2"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <!--如果设置了密码,则密码文件的路径、内容必须正确 start="true"-->
            <auth start="true" users="root" passwordfile="/etc/sersyncd.pswd"/>
            <userDefinedPort start="false" port="874"/>
            <timeout start="false" time="100"/>
            <ssh start="false"/>
        </rsync>
        <!--60分钟进行一次同步失败文件重新同步-->
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
        <!--600分钟进行一次全目录同步-->
        <crontab start="true" schedule="600">
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <!--因为这里start="false" 所以下面plugin的配置无需关心-->
        <plugin start="false" name="command"/>
    </sersync>
    
    ESC
    :wq
  15. 启动sersync(各服务器都执行)

    /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu1.xml
    /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu2.xml
  16. 测试

    # 在任意一台服务器执行
    su www
    cd /www/file1
    echo test > test.txt
    ls
    # 然后在其他每台服务器执行
    cd /www/file1
    ls
    # 如果结果中包含
    test.txt
    # 并且文件内容为'test'
    # 即表示安装成功
  17. 配置开机启动rsync、sersync

    # 开机启动rsync
    systemctl enable rsyncd
    systemctl start rsyncd
    
    # 开机启动sersync
    vim /etc/rc.local
    
    /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu1.xml
    /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu2.xml
    
    ESC
    :wq
    
    chmod +x /etc/rc.d/rc.local
  18. 关于日志中部分提示的说明

    # 文件同步时会有如下提示
    # connect from UNKNOWN (192.168.0.3)
    # rsync to tongbu1/ from unknown (192.168.0.3)
    # connect from UNKNOWN (192.168.0.4)
    # rsync to tongbu1/ from unknown (192.168.0.4)
    
    # 虽然提示连接到未知ip,实际上只是因为没有加
    # 虚拟域名映射,完全不会影响rsync功能的使用。
    # 如果一定要处理的话,可以修改/etc/hosts文件
    # 这样unknown就会变成你设置的虚拟域名了
    vim /etc/hosts
    
    192.168.0.2 file2
    192.168.0.3 file3
    192.168.0.4 file4
    
    ESC
    :wq
<think>好的,我需要帮助用户了解如何使用rsyncsersync实现文件实时同步。首先,用户的问题是关于配置和使用的教程,所以我应该分步骤讲解安装和配置过程。 首先,我需要确认rsyncsersync各自的角色。rsync负责文件同步,而sersync用于监控文件变化并触发同步。根据用户提供的引用,当数据量大时推荐使用rsync+sersync,所以需要强调这一点。 接下来,我应该按照引用中的步骤来整理教程。引用2提到了安装sersync的具体命令,比如下载、解压、创建符号链接等步骤。这部分需要详细说明,但要注意检查命令的正确性,比如解压后的目录名和符号链接的名称是否一致。例如,用户提供的引用中解压后的目录是GNU-Linux-x86,重命名为sersync-2.5.4,但符号链接却是到sersync-2.5.3,这可能是笔误,需要指出或更正。 然后是配置sersync的XML文件,这部分需要指导用户如何修改目标服务器的IP、路径以及同步模块名称。需要提醒用户根据实际环境修改这些参数,并测试rsync配置是否正确。 接着是启动sersync服务,并设置开机自启。这部分需要给出具体的命令,并说明如何验证服务是否正常运行。此外,故障排查部分也很重要,需要包括检查进程、日志、配置文件以及权限问题。 最后,生成相关问题时要围绕用户可能遇到的疑问展开,比如与其他工具的对比、增量同步、参数调优、监控目录排除和安全性等。这些能帮助用户更全面地理解整个方案。 需要注意的是,用户提供的引用中有一些不一致的地方,比如版本号和目录名的问题,需要在回答中明确指出以避免混淆。同时,确保所有命令行操作正确无误,并提醒用户替换示例中的占位符为实际的值。 总结来说,回答需要结构清晰,步骤详细,涵盖安装、配置、启动、测试和常见问题处理,同时根据引用内容补充实际案例和注意事项,确保用户能够顺利配置并使用rsyncsersync实现实时同步。</think>### Rsync + Sersync 实时文件同步配置教程 #### 一、工具原理说明 Rsync 用于实现高效的文件增量同步Sersync 基于 Inotify 机制监控目录变化并触发 Rsync 同步操作,两者结合可满足大规模数据实时同步需求[^1]。 #### 二、环境准备 1. **服务端**(接收文件):安装 Rsync 2. **客户端**(发送文件):安装 Rsync + Sersync #### 三、服务端配置(以 CentOS 为例) ```bash # 安装rsync yum install rsync -y # 创建配置文件 vim /etc/rsyncd.conf # 内容示例: uid = root gid = root use chroot = no max connections = 200 pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log [backup] # 模块名称 path = /data/backup # 同步目标路径 read only = no auth users = rsync_user secrets file = /etc/rsync.password # 创建密码文件 echo "rsync_user:123456" > /etc/rsync.password chmod 600 /etc/rsync.password # 启动服务 systemctl start rsyncd ``` #### 四、客户端配置(发送端) 1. **安装 Sersync** ```bash wget https://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz mv GNU-Linux-x86/ /opt/sersync ln -s /opt/sersync/sersync2 /usr/bin/sersync ``` 2. **修改配置文件** ```xml <!-- 编辑 /opt/sersync/confxml.xml --> <localpath watch="/data/www"> <!-- 被监控目录 --> <remote ip="192.168.1.100" name="backup"/> <!-- 服务端IP与模块名 --> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_user" passwordfile="/etc/rsync.password"/> </rsync> ``` 3. **创建密码文件** ```bash echo "123456" > /etc/rsync.password chmod 600 /etc/rsync.password ``` #### 五、启动与验证 ```bash # 启动sersync(前台模式) sersync -d -r -o /opt/sersync/confxml.xml # 后台守护模式 sersync -dro /opt/sersync/confxml.xml # 验证进程 ps aux | grep sersync ``` #### 六、高级配置技巧 1. **排除特定文件**:在 `<filter>` 标签中添加 `<exclude expression="*.log"></exclude>` 2. **多目录监控**:复制多个 `confxml.xml` 文件并指定不同路径 3. **性能调优**:调整 `<inotify>` 标签中的 `max_queued_events` 数值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值