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

简介: CentOS7.X通过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
相关文章
|
1月前
|
Linux 应用服务中间件 nginx
【PUSDN】centos查看日志文件内容,包含某个关键字的前后5行日志内容,centos查看日志的几种方法
【PUSDN】centos查看日志文件内容,包含某个关键字的前后5行日志内容,centos查看日志的几种方法
48 0
|
6月前
|
JSON Linux 数据格式
百度搜索:蓝易云【Centos 7 通过 targz 文件安装 Elastic Search 服务教程!】
请注意,本教程提供了基本的安装步骤,并且可以根据实际需求进行定制和配置。如果需要更深入的了解和配置,请参考Elasticsearch官方文档或其他权威资源。
283 0
|
4月前
|
网络协议 Unix Linux
Centos下nfs+rpcbind实现服务器之间的文件共享
Centos下nfs+rpcbind实现服务器之间的文件共享
100 0
|
2月前
|
Java Shell Linux
解决 centos下执行sh文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录” 问题
解决 centos下执行sh文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录” 问题
|
Linux 测试技术 Docker
Linux系统:第十三章:centos误删文件如何恢复文件数据
Linux系统:第十三章:centos误删文件如何恢复文件数据
362 0
Linux系统:第十三章:centos误删文件如何恢复文件数据
|
15天前
|
网络协议 Linux 网络安全
centos7下最简单的 unison实现文件双向同步图文详解
centos7下最简单的 unison实现文件双向同步图文详解
12 0
|
2月前
|
Linux
centos7实现磁盘挂载,解挂,开机自动挂载,解决挂载文件覆盖问题
centos7实现磁盘挂载,解挂,开机自动挂载,解决挂载文件覆盖问题
114 0
|
8月前
|
SQL 关系型数据库 MySQL
CentOS部署JAVA程序、安装Tomcat以及安装导入mysql文件的方法
CentOS部署JAVA程序、安装Tomcat以及安装导入mysql文件的方法
|
4月前
|
Prometheus 监控 Cloud Native
Linux|centos7 Prometheus的自动服务发现 一(文件发现机制)
Linux|centos7 Prometheus的自动服务发现 一(文件发现机制)
52 0
|
4月前
|
Linux Shell
百度搜索:蓝易云【Linux(centos7)缺失.bashrc文件登录出现bash-4.2解决教程。】
或者你可以注销并重新登录系统,也会加载新的 `.bashrc` 文件。现在,你应该能够成功解决 "bash-4.2" 错误并登录到 CentOS 7 系统中。
58 0