实时同步概述
实时同步概念说明
实时同步的概念:是一种只要当前目录发生变化则会触发一个事件,事件出发后会将变化的目录同步至远程服务器。实时同步的作用:
保证数据的连续性;
减少人力维护成本。数据备份的方法:
定时任务备份:主要是内部人员备份数据,备份周期最短是1分钟。
实时任务备份:主要是外部人员备份数据,一般是用户保存数据,当用户在网站提交保存数据时,没有同步等待时间。实时同步的原理:
1.监控存储服务器,查看数据的信息变化,主要用到的工具为inotify;
2.利用同步传输数据软件,将变化的数据增量传输给远程服务器;
3.实现实时同步。
实时同步的工具:sersync+rsync,inotify+rsync
Inotify是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件异动告警,增量备份功能,并针对目录或文件的变化及时作出响应。Inotify+rsync可以实出发式实时同步增量备份。
inotify的作用:监控目录中数据信息变化;
sersync是国人基于rsync+inotify-tools开发的工具,不仅保留了inotify的优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高了运行效率,节省时间和网络资源。
sersync服务安装部署
- 第一步:安装软件,将软件推送到指定的目录
[root@nfs01 ~]# yum install rsync inotify -y
[root@nfs01-server ~]# cd /server/tools/
[root@nfs01-server tools]# ls
sersync_installdir_64bit.zip
[root@nfs01-server tools]# mv sersync_installdir_64bit.zip /usr/local/
[root@nfs01-server tools]# unzip /usr/local/sersync_installdir_64bit.zip
Archive: /usr/local/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/
- 第二步:修改配置文件信息
<inotify>
13 <delete start="true"/>
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"/>
21 </inotify>
<sersync>
24 <localpath watch="/backup"> ## 目录信息
25 <remote ip="172.16.1.41" name="backup"/> ## 备份服务器IP,backup模块信息
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="-avz"/> ## 选项参数,默认有-delete
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>
[root@nfs01-server local]# ll /usr/local/sersync_installdir_64bit/sersync/bin/sersync
-rwxr--r-- 1 root root 1810128 Oct 26 2011 /usr/local/sersync_installdir_64bit/sersync/bin/sersync
[root@nfs01-server bin]# ./sersync -dro /usr/local/sersync_installdir_64bit/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: -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_installdir_64bit/sersync/conf/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 /backup && rsync -avz -R --delete ./ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /backup ## 成功!!
实现实时同步的脚本
[root@nfs01 scripts]# cat sersync.sh
#!/bin/bash
#1. jiankong date
inotifywait -mrq /backup --format "%w%f" -e create,delete,close_write,move | while read line
#2. push data
do
rsync -avz --delete /backup rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
done