美文网首页
实时同步整理

实时同步整理

作者: linux_龍 | 来源:发表于2019-08-15 16:11 被阅读0次

1.Sersync 项目利用 Inotify 和 Rsync 技术实现对服务器数据实时同步的解决方案,其中 Inotify 用于监控 Sersync 所在服务器上文件系统的事件变化,而 Rsync 是目前广泛使用的本地以及异地数据同步工具,其优点是只对变化的目录数据操作,甚至是一个文件不同的部分进行同步,所以其优势大大超过使用挂接文件系统或 scp 等方式进行镜像同步。(国人周洋)

2.同步工具目前使用比较多的同步工具为 Inotify-tools 和 Openduckbill,sersync。Sersync 优于 Inotify-tools 和 Openduckbill

sersync服务要放到nfs的服务器上
nfs01客户端client
backup服务端service

1.测试之前一定要确认rsync服务都开启

[root@nfs01 /upload]# systemctl is-active rsyncd
active
[root@nfs01 /upload]# systemctl is-enabled rsyncd
disabled

1.在客户端创建俩个目录用来存放软件

mkdir -p /server/{scripts,tools}

下载安装此压缩包并解压
sersync_installdir_64bit.zip
https://pan.baidu.com/s/10N4LCjSdwpsSVr4PnrNsqw
提取码:dapn

[15:21 root@nfs01 /server/tools]# unzip sersync_installdir_64bit.zip
Archive:  sersync_installdir_64bit.zip
   creating: sersync_installdir_64bit/
   creating: sersync_installdir_64bit/sersync/
   creating: sersync_installdir_64bit/sersync/bin/
  inflating: sersync_installdir_64bit/sersync/bin/sersync  
   creating: sersync_installdir_64bit/sersync/conf/
  inflating: sersync_installdir_64bit/sersync/conf/confxml.xml  
   creating: sersync_installdir_64bit/sersync/logs/

查看一下

[15:21 root@nfs01 /server/tools]# tree
.
├── sersync_installdir_64bit
│   └── sersync
│       ├── bin
│       │   └── sersync
│       ├── conf
│       │   └── confxml.xml
│       └── logs
└── sersync_installdir_64bit.zip    \\此文件

2.在客户端创建目录,把文件移动到/app下
/app/目录在企业中一般用来放代码文件

[15:24 root@nfs01 ~]#mkdir -p /app
[15:24 root@nfs01 ~]#mv /server/tools/sersync_installdir_64bit/sersync /app/

查看一下

[15:27 root@nfs01 ~]# tree /app/
/app/
└── sersync
    ├── bin
    │   └── sersync
    ├── conf
    │   └── confxml.xml
    └── logs

3.在客户端添加执行权限,给此文件创建一个软链接
执行完成后试着敲一下sersync的命令
指定软连接使他变为命令(用全路径可以执行,为让他变的方便。)使他变为命令

[15:27 root@nfs01 ~]#chmod  +x /app/sersync/bin/sersync
[15:27 root@nfs01 ~]#
[15:27 root@nfs01 ~]# ln -s /app/sersync/bin/sersync  /sbin/
[15:29 root@nfs01 ~]# sersync 
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
daemon thread num: 10
parse xml config file
XML Parsing error inside file 'confxml.xml'.
Error: File not found
At line 0, column 0.

4.去服务端rsync的配置文件添加nfsbackup模块

[15:29 root@backup ~]#vim /etc/rsyncd.conf
....
#####################################
[nfsbackup]
comment = www by old0boy 13:14 2019-5-20
path = /nfsbackup
"/etc/rsyncd.conf" 31L, 732C written  

5.服务端创建目录并修改权限

[15:39 root@backup ~]# mkdir -p /nfsbackup
[15:40 root@backup ~]# chown rsync.rsync /nfsbackup/
[15:41 root@backup ~]# ll -d /nfsbackup/
drwxr-xr-x 2 rsync rsync 6 May 24 15:40 /nfsbackup/

6.在服务端本地测试推送一下

[15:41 root@backup ~]# rsync  -avz  /etc/hostname    rsync_backup@172.16.1.41::nfsbackup 
Password: 
sending incremental file list
hostname

sent 101 bytes  received 43 bytes  32.00 bytes/sec
total size is 7  speedup is 0.05

7.再回到客户端推送一下

[15:41 root@nfs01 ~]# rsync  -avz  /etc/hostname    rsync_backup@172.16.1.41::nfsbackup 
Password: 
sending incremental file list

sent 50 bytes  received 20 bytes  20.00 bytes/sec
total size is 7  speedup is 0.10

8.客户端查看文件下的xml文件

/app/sersync/conf/confxml.xml

https://www.processon.com/view/link/5bf911c0e4b006dc83a24929

image.png
[15:53 root@nfs01 ~]#vim /app/sersync/conf/confxml.xml 
  1 <?xml version="1.0" encoding="ISO-8859-1"?>
  2 <head version="2.5">
  3     <host hostip="localhost" port="8008"></host>
  4     <debug start="false"/>      
  5     <fileSystem xfs="false"/>   文件系统\\修改为true
  6     <filter start="false">      \\过滤
  7         <exclude expression="(.*)\.svn"></exclude>      \\排除--exclude
  8         <exclude expression="(.*)\.gz"></exclude>       \\排除--exclude
  9         <exclude expression="^info/*"></exclude>        \\排除--exclude
 10         <exclude expression="^static/*"></exclude>      \\排除--exclude
 11         <delete start="true"/>
 12     </filter>
 13     </inotify>              \\配置inotify的监控事件,实时监控目录变量 是否有变化
 14         <createFolder start="true"/>
 15         <createFile start="false"/>
 16         <closeWrite start="true"/>
 17         <moveFrom start="true"/>
 18         <moveTo start="true"/>
 19         <attrib start="false"/>         \\文件属性
 20         <modify start="false"/>         \\mtime修改时间
 21     </inotify>                  \\sersync什么时候用rsync推送
 22 
 23     <sersync>
 24             <localpath watch="/opt/tongbu">             \\要监控哪个目录 发生变量
 25             <remote ip="127.0.0.1" name="tongbu1"/>     \\rsync服务端ip name=模块名
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->   
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>                         \\rsync命令配置
 30             <commonParams params="-artuz"/>                 \\rsync的参数
 31             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>  \\认证哪个用户和密码文件
 32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>
 36         <failLog path="/var/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
                    \\rsync错误日志指定的路径
 37         <crontab start="false" schedule="600"><!--600mins-->    \\定时任务
 38             <crontabfilter start="false">
 39                 <exclude expression="*.php"></exclude>
 40                 <exclude expression="info/*"></exclude>
 41                 </crontabfilter>
 42         </crontab>
 43         <plugin start="false" name="command"/>
 44     </sersync>
 45 
 46     <plugin name="command">
 47         <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
 48         <filter start="false">
 49             <include expression="(.*)\.php"/>
 50             <include expression="(.*)\.sh"/>
 51         </filter>
 52     </plugin>
 53 
 54     <plugin name="socket">
 55         <localpath watch="/opt/tongbu">
 56             <deshost ip="192.168.138.20" port="8009"/>
 57         </localpath>
 58     </plugin>
 59     <plugin name="refreshCDN">
 60         <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
 61             <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
 62             <sendurl base="http://pic.xoyo.com/cms"/>
 63             <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
 64         </localpath>
 65     </plugin>
 66 </head>

修改前记得先拷贝一份备份

[16:17 root@nfs01 ~]# cp  /app/sersync/conf/confxml.xml /app/sersync/conf/confxml.xml.bak

9.在客户端修改此配置文件,如下内容即可

[16:17 root@nfs01 ~]# vim /app/sersync/conf/confxml.xml 
....
  5     <fileSystem xfs="true"/>  
....
 23     <sersync>
 24         <localpath watch="/upload">     
 25             <remote ip="172.16.1.41" name="nfsbackup"/>
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>
 30             <commonParams params="-az"/>
 31             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
 32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>
 36         <failLog path="/var/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

10.然后用diff对比一下修改的内容

diff /app/sersync/conf/confxml.xml /app/sersync/conf/confxml.xml.bak 
5c5
<     <fileSystem xfs="true"/>
---
>     <fileSystem xfs="false"/>
24,25c24,25
<   <localpath watch="/upload">
<       <remote ip="172.16.1.41" name="nfsbackup"/>
---
>   <localpath watch="/opt/tongbu">
>       <remote ip="127.0.0.1" name="tongbu1"/>
30,31c30,31
<       <commonParams params="-az"/>
<       <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
---
>       <commonParams params="-artuz"/>
>       <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
36c36
<   <failLog path="/var/log/rsync_faii_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
---
>   <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

11.接着在客户端配置如下的环境

[16:24 root@nfs01 ~]# echo '123456' >/etc/rsync.password 
[16:24 root@nfs01 ~]# chmod 600 /etc/rsync.password 
[16:24 root@nfs01 ~]# mkdir -p /upload
[16:25 root@nfs01 ~]# chown nfsnobody.nfsnobody /upload/
[16:25 root@nfs01 ~]# ll -d /upload/
drwxr-xr-x 2 nfsnobody nfsnobody 21 May 23 08:42 /upload/

12.然后查看sersync配置参数

[16:25 root@nfs01 ~]# sersync -h
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
```
13.在客户端创建文件测试一下
```
[16:50 root@nfs01 ~]# touch /upload/oldboy{01..10}.txt
[19:32 root@nfs01 ~]# ll /upload/
total 0
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy01.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy02.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy03.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy04.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy05.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy06.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy07.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy08.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy09.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy10.txt
```
14.推送到服务端:
```
sersync  -rd  -o  /app/sersync/conf/confxml.xml
```

我们要把这条命令放到 /etc/rc.d/rc.local 开机自启动里,不然重启后就失效了。

```
[16:52 root@nfs01 ~]# tail -1 /etc/rc.d/rc.local 
sersync -rd -o /app/sersync/conf/confxml.xml

[16:52 root@nfs01 ~]# sersync  -rd  -o  /app/sersync/conf/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:  /app/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
WARNING XFS FILE SYSTEM WORK
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 /upload && rsync -az -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /upload
```

15.在backup服务端查看:实时同步了
```
[16:51 root@backup ~]# ll /nfsbackup/
total 0
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy01.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy02.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy03.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy04.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy05.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy06.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy07.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy08.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy09.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy10.txt
```

相关文章

  • 实时同步整理

    1.Sersync 项目利用 Inotify 和 Rsync 技术实现对服务器数据实时同步的解决方案,其中 Ino...

  • Linux sersync day35

    什么是实时同步为什么要实时同步实时同步的原理实时同步的场景实时同步工具选择实时同步案例演示 一、什么是实时同步 实...

  • 备份服务之实时同步备份(sersync)

    实时同步备份方法1)利用脚本实现实时同步2)利用软件实现实时同步 实时同步备份原理 存储服务器 --...

  • 综合架构实时同步详解

    day35 综合架构实时同步 课程介绍: 1.实时同步原理概念2.实现实时同步方式3.实现实时同步方式1)利用脚本...

  • 实现实时同步备份总结

    实现实时同步备份总结 \ 一. 实时同步备份原理1.inotify实时监控2.rsync实时传输3.sersy...

  • 实时同步

    预备知识 实时同步概述 什么是实时同步 实时同步是一种只要当前目录发生变化则会触发一个事件,事件触发后会将变化的目...

  • Linux_295_Sersync工具介绍

    ftp:效率低,不支持差异化,实时同步实时数据同步:rsync + inotify-toolssersync:还能...

  • sersync 对网站数据实时同步备份

    sersync 实现实时同步数据 1 sersync实时同步原理 rsync守护进程服务,实现数据传输 inoti...

  • 第三十五天 实现sersync自动同步

    第三十五天 实现sersync自动同步 今日内容 sersync 1.什么是实时同步? 2.为什么要实时同步? 3...

  • ES同步数据使用切面进行实时更新

    以前公司ES使用是直接定时同步,对数据实时性要求不是很高。当前公司使用sass平台要求实时同步数据,这样才能实时搜...

网友评论

      本文标题:实时同步整理

      本文链接:https://www.haomeiwen.com/subject/exbzjctx.html