Sersync 实时同步

sersync+rsync实时同步

实时同步有两种方案: inotify + rsync ,sersync + rsync

  • inotify是一种文件系统的变化通知机制,它监控着文件系统的操作,比如读取、写入和创建,我们需要安装inotify-tools软件包,使用 inotifywait 命令,指定实时监听的目录
  • sersync是基于 inotify 和 rsync 开发的,可以记录监听目录下的发送的变化,然后触发 rsync 同步,只同步发生变化的文件或者目录

inotify + rsync 与 sersync + rsync 的区别

  1. inotify + rsync

    ① inotify-tools 只能记录被监听目录发生了变化,并没有把具体是哪个文件或者目录发生了变化记录下来

    ② rsync 在同步的时候,并不知道是哪个文件或者目录发生了变化,每次都是对整个目录同步(在整个目录下遍历查找对比文件),当数据量很大时,整个目录的同步会非常耗时,效率很低

  2. sersync + rsync

    ① sersync 可以记录下具体的哪个文件或者目录发生了变化

    ② rsync 在同步的时候,知道是哪个文件或者目录发生了变化,只同步发生变化的这个文件或者目录

当同步的目录数据量不大时,建议使用 inotify + rsync ,当同步的目录数据量很大,建议使用 sersync + rsync

sersync+rsync案例

主机名 外网IP 内网IP 角色 部署服务
web01 10.0.0.7 172.16.1.7 rsync的客户端,nfs的客户端,网站 rsync,nfs,apache,php
web02 10.0.0.8 172.16.1.8 rsync的客户端,nfs的客户端,网站 rsync,nfs,apache,php
nfs 10.0.0.31 172.16.1.31 rsync的客户端,nfs的服务端(共享存储) rsync,nfs(deamon),sersync(deamon)
backup 10.0.0.41 172.16.1.41 rsync的服务端 rsync(deamon)

部署NFS客户端

# 1.安装 nfs-utils 和 rpcbind(CentOS7自带)
[root@web01 ~]# yum install -y nfs-utils rpcbind
[root@web02 ~]# yum install -y nfs-utils rpcbind

# 2.安装 httpd 和 php 
[root@web01 ~]# yum install -y httpd php 
[root@web02 ~]# yum install -y httpd php 

# 3.将 HTML文件、PHP文件 放入站点目录下
[root@web01 ~]# ll /var/www/html/
total 52
-rw-r--r-- 1 root root 38772 Apr 27  2018 bg.jpg
-rw-r--r-- 1 root root  2633 May  4  2018 index.html
-rw-r--r-- 1 root root    52 May 10  2018 info.php
drwxr-xr-x 2 root root    140 May  9 15:24 upload
-rw-r--r-- 1 root root  1128 May  9 11:58 upload_file.php
[root@web02 ~]# ll /var/www/html/
total 52
-rw-r--r-- 1 root root 38772 Apr 27  2018 bg.jpg
-rw-r--r-- 1 root root  2633 May  4  2018 index.html
-rw-r--r-- 1 root root    52 May 10  2018 info.php
drwxr-xr-x 2 root root    140 May  9 15:24 upload
-rw-r--r-- 1 root root  1128 May  9 11:58 upload_file.php

# 4.检查 NFS服务端是否配置好 "房源"
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
[root@web02 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24

# 5. 挂载站点目录 /var/www/html/upload 到 NFS服务端的 共享存储目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /var/www/html/upload
[root@web02 ~]# mount -t nfs 172.16.1.31:/data /var/www/html/upload

# 6.重启 httpd 服务
[root@web01 ~]# systemctl restart httpd
[root@web02 ~]# systemctl restart httpd

部署NFS服务端

# 1.安装 nfs-utils 和 rpcbind(CentOS7自带)
[root@nfs ~]# yum install -y nfs-utils rpcbind

# 2.编辑 NFS服务端 配置文件
[root@nfs ~]# vi /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

# 3.将 共享存储目录 /data 的属主和属组改变为 UID=666 GID=666 的用户
[root@nfs ~]# id wqh
uid=666(wqh) gid=666(wqh) groups=666(wqh)
[root@nfs ~]# ll /data/ -d
drwxr-xr-x 2 wqh wqh 140 May  9 15:24 /data/

# 4.启动/开机自启 nfs-server 和 rpcbind(CentOS7已开机自启)
[root@nfs ~]# systemctl start nfs-server
[root@nfs ~]# systemctl enable nfs-server

# 5.检查 配置是否有问题
[root@nfs ~]# showmount -e
Export list for nfs:
/data 172.16.1.0/24
[root@nfs ~]# cat /var/lib/nfs/etab
/data 172.16.1.0/24(rw,sync,wdelay,hide,all_squash,anonuid=666,anongid=666,etc...)

部署Rsync客户端

## web01和web02客户端中的重要文件 暂时不做备份
# 1.安装 rsync 和 inotify-tools,因为是 sersync 依赖
[root@nfs ~]# yum install -y rsync inotify-tools

# 2.下载 sersync 软件包
[root@nfs ~]# wget https://files.cnblogs.com/files/zzzwqh/sersync2.5.4_64bit_binary_stable_final.tar.gz

# 3.解压 sersync 软件包
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/

# 4.解压后有 xml文件 和 二进制执行文件
[root@nfs sersync]# ll
total 1772
-rwxr-xr-x 1 root root    2461 May  9 12:41 confxml.xml
-rwxr-xr-x 1 root root 1810128 Oct 26  2011 sersync2

# 5.修改 xml 配置文件部分内容
[root@nfs sersync]# vi confxml.xml
   <filter start="false">
        <!-- 自定义过滤的文件名规则,已自动过滤临时文件(过滤的文件无需同步) -->
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
        <!-- inotify 监控参数 -->
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>
    <sersync>
        <!-- Rsync客户端监控目录 -->
        <localpath watch="/data">
            <!-- Rsync服务端IP 和 Rsync服务端模块  -->
            <remote ip="172.16.1.41" name="backup"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <!-- 指定rsync命令选项   -->
            <commonParams params="-az"/>
            <!-- 是否开启认证模式(认证用户),指定Rrync服务端认证用户,指定密码文件路径 -->
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>

# 6.创建 /etc/rsync.password 密码文件,指定权限 600/700
[root@nfs ~]# ll /etc/rsync.password 
-rw------- 1 root root 4 May  9 12:37 /etc/rsync.password
[root@nfs ~]# cat /etc/rsync.password
123

# 7.启动 sersync 服务
[root@nfs sersync]# /usr/local/sersync/sersync2 -rdo /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: -r 	rsync all the local files to the remote servers before the sersync work
option: -d 	run as a daemon
option: -o 	config xml name:  /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
daemon start,sersync run behind the console 
use rsync password-file :
user is	rsync_backup
passwordfile is 	/etc/rsync.password
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 && rsync -az -R --delete ./ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data

部署Rsync服务端

# 1.安装 rsync 即可
[root@backup ~]# yum -y install rsync

# 2.修改 /etc/rsyncd.conf 配置文件
[root@backup ~]# vi /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = NFS's backup
path = /backup

# 3.创建认证用户密码文件
[root@backup ~]# vi /etc/rsyncd.conf
[root@backup ~]# ll /etc/rsync.passwd 
-rw------- 1 root root 17 May  6 19:43 /etc/rsync.passwd
[root@backup ~]# cat /etc/rsync.passwd
rsync_backup:123

# 4.重启服务,并设置开机自启
[root@backup ~]# systemctl restart rsyncd
[root@backup ~]# systemctl enable rsyncd

# 5.可以检查 873 端口 是否开启
posted @ 2020-05-11 08:20  拨云见日z  阅读(265)  评论(0编辑  收藏  举报