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 && 关闭防火墙
bash 复制代码
# 禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service 
  1. 安装rsync服务
bash 复制代码
yum install rsync -y

3、部署rsync服务器

  1. 创建目录
bash 复制代码
mkdir /opt/rsync
chmod 777 /opt/rsync
# 重要 目标机器须要先创建同步目录
mkdir /data/file
  1. 创建认证文件
bash 复制代码
echo pusher:rsync > /opt/rsync/server.pass
chmod 600 /opt/rsync/server.pass
  1. 配置rsync
    vim /etc/rsyncd.conf
bash 复制代码
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服务
bash 复制代码
systemctl restart rsyncd.service
# 
systemctl status rsyncd.service

4、配置sersync服务器

  1. 关闭SELINUX && 关闭防火墙
bash 复制代码
# 禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service 
  1. 安装rsync服务
bash 复制代码
yum install rsync -y
  1. 创建认证文件
bash 复制代码
echo rsync > /etc/rsync.pass
chmod 600 /etc/rsync.pass
  1. 手工测试rsync同步情况
bash 复制代码
# 创建目录和文件
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
bash 复制代码
# 解压文件
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 复制代码
<?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服务
bash 复制代码
[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. 开机启动
bash 复制代码

6、验证

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

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

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

相关推荐
远游客07133 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
jingyu飞鸟3 小时前
centos-stream9系统安装docker
linux·docker·centos
LIKEYYLL5 小时前
GNU Octave:特性、使用案例、工具箱、环境与界面
服务器·gnu
云云3215 小时前
搭建云手机平台的技术要求?
服务器·线性代数·安全·智能手机·矩阵
云云3215 小时前
云手机有哪些用途?云手机选择推荐
服务器·线性代数·安全·智能手机·矩阵
cominglately6 小时前
centos单机部署seata
linux·运维·centos
魏 无羡6 小时前
linux CentOS系统上卸载docker
linux·kubernetes·centos
CircleMouse6 小时前
Centos7, 使用yum工具,出现 Could not resolve host: mirrorlist.centos.org
linux·运维·服务器·centos
木子Linux7 小时前
【Linux打怪升级记 | 问题01】安装Linux系统忘记设置时区怎么办?3个方法教你回到东八区
linux·运维·服务器·centos·云计算
不惑_7 小时前
小白入门 · 腾讯云轻量服务器部署 Hadoop 3.3.6
服务器·hadoop·腾讯云