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

简介:

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

  1. 安装前的准备(各同步服务器都配置)

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

    yum install rsync -y
  3. 配置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
  4. 配置密码文件

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

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

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

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

    yum install firewalld -y
    systemctl enable firewalld
    systemctl start firewalld
    firewall-cmd --zone=public --permanent --add-port=873/tcp
    firewall-cmd --reload
  9. 创建单元文件(如果是用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
  10. 启动并设置开机启动

    systemctl enable rsyncd
    systemctl start rsyncd
  11. 安装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
  12. 配置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="false" 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
  13. 启动sersync(各服务器都执行)

    /usr/local/sersync/sertongbu2 -d -r -o /usr/local/sersync/tongbu1.xml
    /usr/local/sersync/sertongbu2 -d -r -o /usr/local/sersync/tongbu2.xml
  14. 测试

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

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

    # 文件同步时会有如下提示
    # 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
目录
相关文章
|
29天前
|
定位技术
GPS北斗卫星同步时钟(时间同步服务器)建设施工部署方案
GPS北斗卫星同步时钟(时间同步服务器)建设施工部署方案
GPS北斗卫星同步时钟(时间同步服务器)建设施工部署方案
|
1月前
|
存储 弹性计算 数据可视化
要将ECS中的文件直接传输到阿里云网盘与相册(
【2月更文挑战第31天】要将ECS中的文件直接传输到阿里云网盘与相册(
415 4
|
2月前
|
数据可视化 Shell Linux
shell+crontab+gitlab实现ecs服务器文件的web展示
本文通过把ecs服务器上的文件定时上传至gitlab,实现文件的页面可视化和修改历史。技术点:shell、crontab、gitlab。
50 3
|
3月前
|
弹性计算 数据可视化 安全
云服务器ECS里文件的URL,如何查到呢?
云服务器ECS里文件的URL,如何查到呢?
52 0
|
1月前
|
Linux 网络安全 Python
如何在服务器上运行python文件
如何在服务器上运行python文件
|
29天前
|
安全 数据处理 C#
C# Post数据或文件到指定的服务器进行接收
C# Post数据或文件到指定的服务器进行接收
|
1月前
|
消息中间件 关系型数据库 MySQL
Flink CDC产品常见问题之把flink cdc同步的数据写入到目标服务器失败如何解决
Flink CDC(Change Data Capture)是一个基于Apache Flink的实时数据变更捕获库,用于实现数据库的实时同步和变更流的处理;在本汇总中,我们组织了关于Flink CDC产品在实践中用户经常提出的问题及其解答,目的是辅助用户更好地理解和应用这一技术,优化实时数据处理流程。
|
1月前
|
存储 数据挖掘
服务器数据恢复—raid5热备盘同步失败的数据恢复案例
一台存储上有一组由多块硬盘组建的raid5阵列,该raid5阵列中的一块硬盘掉线,热备盘自动上线同步数据的过程中,raid阵列中又有一块硬盘掉线,热备盘的数据同步被中断,raid5阵列失效,卷挂载不上,存储瘫痪。 这类raid故障比较常见,服务器raid中的硬盘大多数情况下都是一个批次的同品牌同型号的硬盘,一旦有硬盘出现故障掉线,那么其他硬盘也随时有出故障掉线的可能。
|
1月前
|
存储 网络协议 数据可视化
如何使用Synology Drive作为文件同步服务器实现云同步Obsidian笔记
如何使用Synology Drive作为文件同步服务器实现云同步Obsidian笔记
|
2月前
|
Java
java上传、下载、预览、删除ftp服务器上的文件
java上传、下载、预览、删除ftp服务器上的文件