1、要实现的目的:
把主机A中 /data 目录中的数据实时同步到主机B中 /data 目录中
2、下载所需要的软件
sersync2.5.4
https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
3、配置
在主机A上:
解压后只有两个文件,sersync2,命令执行文件; confxml.xml,配置文件
[[email protected] sersync]# pwd
/usr/local/sersync
[[email protected] sersync]# ls
confxml.xml sersync2
查看命令执行文件的帮助信息;
[[email protected] sersync]# ./sersync2 -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参数,则默认执行同步程序
________________________________________________________________
通过命令帮助信息就知道要实现同步功能,只要执行以下命令就可以做到。
./sersync2 -d -r
如果有多个目录需要同步,那只要起多个进程然后用-o选项指定不同的配置文件。例如线上的配置:
/usr/local/sersync/bin/sersync -d -o /usr/local/sersync/conf/download.xml
/usr/local/sersync/bin/sersync -d -o /usr/local/sersync/conf/ddbill.xml
/usr/local/sersync/bin/sersync -d -o /usr/local/sersync/conf/dinpay.xml
配置文件confxml.xml参数配置
实现同步主要配置23-44行, <sersync>.....</sersync> 中的内容
默认配置文件中的内容如下:
<sersync> <localpath watch="/opt/tongbu"> <remote ip="127.0.0.1" name="tongbu1"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> <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>
简单实现功能的话其实只要配置
<localpath watch="/opt/tongbu"> <remote ip="127.0.0.1" name="tongbu1"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath>
这几行就可以了
watch后面跟的就是本地要同步到远程的目录
ip后面跟的就是远程主机的ip地址; name后面跟的是远程主机中rsync daemon模式中rsyncd.conf文件中的模块名
如果要同步到多台机器,就把每台机器写一个<remote> 标签
主机A中的配置文件修改如下:
[[email protected] sersync]# sed -n ‘24,28p‘ confxml.xml
<localpath watch="/data">
<remote ip="192.168.2.175" name="data"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
[[email protected] sersync]#
在主机B上:
配置/data目录为rsync daemon中的模块名和目录。
rsyncd.conf简单配置如下
[[email protected] data]# cat /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[data]
path=/data
#ignore errors (开启会在日志中报错误)
read only = no
hosts allow = 192.168.2.0/24
hosts deny = *
配置文件修改好了后就可以启动rsync daemon模式
[[email protected] data]# rsync --daemon
[[email protected] data]# ss -tlnp|grep 873
0 5 :::873 :::* users:(("rsync",10042,5))
0 5 *:873 *:* users:(("rsync",10042,4))
[[email protected] data]#
这样主机B就配置完毕了~
现在再回到主机A中执行 ./sersync2 -d -r 命令启动实时同步.
[[email protected] sersync]# ./sersync2 -d -r
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
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
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 -artuz -R --delete ./ 192.168.2.175::data >/dev/null 2>&1
run the sersync:
watch path is: /data
[[email protected] sersync]# ps -ef|grep sersync2
root 21640 1 0 03:30 ? 00:00:00 ./sersync2 -d -r
4、演示
现在我们进入主机A和主机B中的/data目录,做一些简单演示操作
[[email protected] /-19:38:31]#ls /data
[[email protected] /-19:38:38]#
[[email protected] /-19:38:32]#ls /data
[[email protected] /-19:38:42]#
增
[[email protected] /-19:39:48]#cd data
[[email protected] data-19:39:53]#touch 1 2 3 4 5
[[email protected] data-19:40:15]#ls
1 2 3 4 5
[[email protected] data-19:40:21]#
[[email protected] /-19:39:49]#cd data
[[email protected] data-19:39:56]#ls
1 2 3 4 5
[[email protected] data-19:40:23]#
删
[[email protected] data-19:41:43]#rm 1 2 3 -f
[[email protected] data-19:41:49]#ls
4 5
[[email protected] data-19:41:50]#
[[email protected] data-19:40:23]#ls
4 5
改
[[email protected] data-19:41:50]#echo 44444444 > 4
[[email protected] data-19:48:38]#echo 55555555 > 5
[[email protected] data-19:48:46]#cat 4 5
44444444
55555555
[[email protected] data-19:48:53]#
[[email protected] data-19:49:14]#cat 4
44444444
[[email protected] data-19:49:16]#cat 5
55555555
[[email protected] data-19:49:18]#