4.服务器间数据的实时同步服务

使用工具VMware17.0
Xshell 7.0
CentOS7.0
sersync2.5.4
参考资料
  1. 为什么要用到数据实时同步服务
    之前我们使用到的存储服务只存储在一台存储服务器, 也称为单点服务, 然而如果存储服务器发生故障就很容易造成数据丢失 (称为单点故障), 而解决这一问题的办法就是采用分布式存储, 而要实现分布式存储我们就需要进行实时同步

  2. 如何实现实时同步服务
    nfs + sersync (国产开源, 内置 inotify 和 rsync 命令, 一个命令+一个配置文件), 用于联调 (在web服务端入口处创建文件,查看backup服务器是否有文件)

  3. 实时同步整体实现思路框架图
    用户上传文件到 web 服务器, web 服务器挂载 nfs, nfs 实时同步到备份服务器上
    在这里插入图片描述

  4. 确认是否完成前置条件准备

  5. Xftp 上传 sersync 压缩包到 rsync 客户端 (10.0.0.31) 家目录下
    在这里插入图片描述
    在这里插入图片描述

    # 查看tar存档文件中包含的内容(tf选项表示显示tar包中的文件列表而不解压缩)
    tar tf sersync2.5.4_64bit_binary_stable_final.tar.gz
    

    注意点 : 获取 sersync 2.5.4 压缩包
    网盘链接:sersync2.5.4_64bit_binary_stable_final.tar
    提取码:0upz

  6. 在 rsync 客户端 (10.0.0.31) 新建目录并将sersync包解压到新目录中

    # 创建递归目录
    mkdir -p /app/tools/
    mkdir -p /app/tools/sersync/{bin,conf}
    # 解压sersync包
    tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
    # 将命令移入bin目录
    mv GNU-Linux-x86/sersync2 /app/tools/sersync/bin/
    # 将配置文件移入conf目录
    mv GNU-Linux-x86/confxml.xml /app/tools/sersync/conf/
    # 查看是否移动成功
    tree -F /app/tools/
    

    在这里插入图片描述

    注意点 : bin 目录和 conf 目录的作用
    ● bin 目录用于存放命令
    ● conf 目录用于存放配置文件

  7. 将剩余文件移入到 rsync 客户端 (10.0.0.31) 临时目录 (tmp) 中

     mv sersync2.5.4_64bit_binary_stable_final.tar.gz GNU-Linux-x86/ /tmp/
    

    在这里插入图片描述

  8. 在 rsync 服务端 (10.0.0.41) 上配置 rsyncd.conf 文件

    # 创建实时同步目录并修改所有者
    mkdir -p /nfsbackup/
    chown -R rsync.rsync /nfsbackup/
    
    # 在rsyncd.conf文件末尾追加内容
    vim /etc/rsyncd.conf
    ----------------------------------------
    [nfsbackup]
    comment = shi shi tong bu nfs
    path = /nfsbackup/
    ----------------------------------------
    :wq
    
    # 重启rsync服务
    systemctl restart rsyncd
    
  9. 在 rsync 客户端 (10.0.0.31) 测试 rsync 服务端 (10.0.0.41) 配置是否成功

    # (10.0.0.31) 客户端免密传输到服务端
    rsync -av /etc/hostname rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.client
    # (10.0.0.41) 服务端查看是否传输成功
    ll /nfsbackup/
    

    在这里插入图片描述

  10. 在 rsync 客户端 (10.0.0.31) 配置 confxml.xml 文件

    # 配置confxml.xml文件
    vim /app/tools/sersync/conf/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>
    	<!--修改1:将"/opt/tongb/"改为"/data/"-->
    	<localpath watch="/data/">
    	    <!--修改2:将"127.0.0.1"改为"172.16.1.41","tongbu1"改为"nfsbackup"-->
    	    <remote ip="172.16.1.41" name="nfsbackup"/>
    	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    	</localpath>
    	<rsync>
    	    <!--修改3:将"-artuz"改为"-az"-->
    	    <commonParams params="-artuz"/>
    	    <!--修改4:将"false"改为"true","root"改为"rsync_backup","/etc/rsync/pas"改为"/etc/rsync.client"-->
    	    <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.client"/>
    	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
    	    <timeout start="false" time="100"/><!-- timeout=100 -->
    	    <ssh start="false"/>
    	</rsync>
    	<!--修改5:将"/tmp/rsync_fail_log.sh"改为"/var/log/rsync_fail.log"-->
    	<failLog path="/var/log/rsync_fail.log" 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>
    
        <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>
    
    ----------------------------------------
    :wq
    

    在这里插入图片描述

  11. rsync 客户端 (10.0.0.31) 启动 sersync

    # 创建sersync2软连接
    ln -s /app/tools/sersync/bin/sersync2 /bin
    # 查看软连接是否创建成功
    sersync2 -h
    # 启动sersync服务
    sersync2 -rdo /app/tools/sersync/conf/confxml.xml
    # 查看进程是否运行
    ps -ef | grep sersync
    

    在这里插入图片描述

  12. rsync 客户端 (10.0.0.31) 测试 rsync 服务端 (10.0.0.41) 能否成功同步数据

    # (10.0.0.41)开启监控
    watch ls -l /nfsbackup/
    # (10.0.0.31)创建文件
    touch /data/{01..10}.txt
    

    在这里插入图片描述

  13. rsync web服务器 (10.0.0.7) 实现挂载

    # 创建upload目录并查看挂载情况
    mkdir -p /upload
    df -h
    
    # 卸载之前挂载的/mnt目录并查看
    umount /mnt
    df -h
    
    # 挂载sync客户端(172.16.1.31)的data目录到本地的/upload目录下 
    mount -t nfs 172.16.1.31:/data/ /upload/
    # 检查是否挂载成功
    df -h /upload
    

    在这里插入图片描述

  14. 检查rsync web服务器 (10.0.0.7) 是否挂载成功

    # (10.0.0.41)开启监控 (可选)
    watch ls -l /nfsbackup/
    
    # (10.0.0.7)创建文件
    cd /upload
    touch test-{1..10}.txt
    

    在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值