centos7.9 安装sersync+rsync 服务器数据实时同步

背景说明: 要把源数据服务器的 /data/file 目录实时同步到目标服务上。

1、环境准备

角色 IP 名称 安装软件
sersync服务器 192.168.99.9 源数据服务器 sersync + rsync
rsync服务器 192.168.99.123 目标服务器 rsync

2、配置rsync服务器

  1. 关闭SELINUX && 关闭防火墙
# 禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service 
  1. 安装rsync服务
yum install rsync -y

3、部署rsync服务器

  1. 创建目录
mkdir /opt/rsync
chmod 777 /opt/rsync
# 重要 目标机器须要先创建同步目录
mkdir /data/file
  1. 创建认证文件
echo pusher:rsync > /opt/rsync/server.pass
chmod 600 /opt/rsync/server.pass
  1. 配置rsync
    vim /etc/rsyncd.conf
uid = root
gid = root
port = 873
max connections = 2000
pid file = /opt/rsync/rsyncd.pid
log file = /opt/rsync/rsyncd.log
lock file = /opt/rsync/rsync.lock

[file]
path = /data/file
comment = file
read only = no
auth users = pusher
secrets file = /opt/rsync/server.pass
hosts allow = 192.168.99.0/24
  1. 启动rsync服务
systemctl restart rsyncd.service
# 
systemctl status rsyncd.service

4、配置sersync服务器

  1. 关闭SELINUX && 关闭防火墙
# 禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service 
  1. 安装rsync服务
yum install rsync -y
  1. 创建认证文件
echo rsync > /etc/rsync.pass
chmod 600 /etc/rsync.pass
  1. 手工测试rsync同步情况
# 创建目录和文件
mkdir -p /data/file
cd /data/file
touch {
   1..10}

# 手工执行同步(只有手工同步成功下面的自动同步才不会有问题)
cd /data/file && rsync -artuz -R --delete ./ pusher@192.168.99.123::file --password-file=/etc/rsync.pass

5、部署sersync服务器

  1. 安装sersync
    软件下载地址:https://code.google.com/archive/p/sersync/downloads
    (须要魔法) 下载最新文件 sersync2.5.4_64bit_binary_stable_final.tar.gz
# 解压文件
tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
mv /usr/local/GNU-Linux-x86 /usr/local/sersync
cd /usr/local/sersync
  1. 配置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="true"/><!--如果系统linux7以上,需要将xfs="false"修改成xfs="true",否则sersync无法递归监控-->
    <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="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="true"/>
    </inotify>

    <sersync>
        <localpath watch="/data/file">
            <remote ip="192.168.99.123" name="file"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="pusher" passwordfile="/etc/rsync.pass"/>
            <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>

    <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>
  1. 启动sersync服务
[root@backup]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d      run as a daemon
option: -r      rsync all the local files to the remote servers before the sersync work
option: -o      config xml name:  /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost     host port: 8008
will ignore the inotify createFile event 
daemon start,sersync run behind the console 
use rsync password-file :
user is pusher
passwordfile is         /etc/rsync.pass
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/file && rsync -artuz -R --delete ./ pusher@192.168.99.123::file --password-file=/etc/rsync.pass >/dev/null 2>&1 
run the sersync: 
watch path is: /data/file
  1. 开机启动

6、验证

源服务器创建文件,查看目标服务器是否同步。

注:/tmp/rsync_fail_log.sh 文件可以查看异常文件。

参考文章: https://zhuanlan.zhihu.com/p/489165901

相关推荐

  1. centos7.9 安装sersync+rsync 服务器数据实时同步

    2023-12-09 09:02:01       45 阅读
  2. CentOS系统rsync文件同步 安装配置

    2023-12-09 09:02:01       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 09:02:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 09:02:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 09:02:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 09:02:01       20 阅读

热门阅读

  1. vue 批量下载文件,不走后端接口的方法

    2023-12-09 09:02:01       41 阅读
  2. nginx 的概念、高并发处理及详细参数配置

    2023-12-09 09:02:01       39 阅读
  3. Axios

    Axios

    2023-12-09 09:02:01      29 阅读
  4. es6 相关面试总结

    2023-12-09 09:02:01       38 阅读
  5. elasticsearch中LessThen遇到的坑

    2023-12-09 09:02:01       23 阅读
  6. python写数据进es中

    2023-12-09 09:02:01       33 阅读
  7. 代码随想录 62. 不同路径

    2023-12-09 09:02:01       37 阅读
  8. 给鼠标描述符打上注释防止忘记

    2023-12-09 09:02:01       32 阅读