Sersync服务器同步linux
系统环境: RHEL6 x86_64c++
主服务器:server1 192.168.2.68web
目标服务器:station1 192.168.2.27vim
条件:iptables关,selinux关api
软件下载:http://code.google.com/p/sersync/服务器
sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优势是:网络
● sersync是使用c++编写,并且对linux系统文 件系统产生的临时文件和重复的文件操做进行过滤(详细见附录,这个过滤脚本程序没有实现),因此在结合rsync同步的时候,节省了运行时耗和网络资源。 所以更快。多线程
●相比较上面两个项目,sersync配置起来很简单,其中bin目录下 已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用便可。socket
●另外本项目相比较其余脚本开源项目,使用多线程进行同步,尤为在同步较大文件时,可以保证多个服务器实时保持同步状 态。ui
●本项目有出错处理机制,经过失败队列对出错的文件从新同步,若是仍旧失败,则 每10个小时对同步失败的文件从新同步。
●本项目自带crontab功能,只需在 xml配置文件中开启,便可按您的要求,隔一段时间总体同步一次。无需再额外配置crontab功能。
●本项目socket与http插件扩展,知足您二次开发的须要。
1. 配置同步目标服务器rsync
yum install rsync -y
在目标服务器上配置以下:
vi /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
[rsync]
path=/opt/rsync
comment = test files
ignore errors = yes
read only = no
hosts allow = 192.168.2.68
hosts deny = *
只容许主服务器server1访问,同步模块名为[rsync],/opt/rsync为文件同步目录。
rsync --daemon
#后台运行rsync
使用xinetd管理rsync服务
yum install xinetd -y
chkconfig rsync on
service xinetd restart
2. 在主服务器上安装配置sresync
yum install rsync -y
tar zxf sersync2.5_64bit_binary_stable_final.tar.gz
cd GNU-Linux-x86/
vi confxml.xml, 修改以下行
…
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/opt/rsync"> #本地同步目录
<remote ip="192.168.2.27" name="rsync"/> #目标服务器列表和同步模块名
<remote ip="192.168.0.2" name="rsync"/>
</localpath>
...
3. 运行Sersync与Sersync启动参数
sersync会对目录进行监控,将变化的目录或文件同步到远程服务器。sersync是递归监控的,如
果有多级目录,都会同步到远程服务器。经常使用启动方式以下:
在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。
#cd GNU-Linux-x86/
#./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
daemon thread num: 10
parse xml config file
host ip : localhost
host port: 8008
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)
please according your cpu ,use -n param to adjust the cpu rate
run the sersync:
watch path is: /opt/rsync
四、(1)配置目标服务器(多目录同步)
cd /mnt/
mkdir tongbu
vim /etc/rsyncd.conf
......
[tongbu]
path=/mnt/tongbu
comment = test files
ignore errors = yes
read only = no
hosts allow = 192.168.2.68
hosts deny = *
service xinetd restart
(2)配置主服务器
cd GNU-Linux-x86/
cp confxml.xml confxml-1.xml
vim confxml-1.xml
<sersync>
<localpath watch="/opt/tongbu1">
<remote ip="192.168.2.27" name="tongbu"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
cd /opt/
mkdir tongbu1
cd GNU-Linux-x86/
./sersync2 -r -d -o confxml-1.xml
cd /opt/tongbu1
touch hello
在目标服务器的/mnt/tongbu下就会有被同步的文件hello
在开启实时监控的以前对主服务器目录与远程目标机目录进行一次总体同步
./sersync -r
若是须要将sersync运行前,已经存在的全部文件或目录所有同步到远程,要以-r参数运行sersync,将本地与远程总体同步一次。若是设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行总体同步。-r参数将会无效
指定配置文件
./sersync -o XXXX.xml
对于sersync使用可执行文件目录下的默认配置文件confxml.xml,若是须要使用另外一个配置文件,可使用-o参数指定其它配置文件。
指定默认的线程池的线程总数
./sersync -n num
例如 ./sersync -n 5 则指定线程总数为5,若是不指定,默认启动线程池数量是10,若是cpu使用太高,能够经过这个参数调低,若是机器配置较高,能够用-n跳高线程总数。
不进行同步,只运行插件
./sersync -m pluginName
例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。
多个参数能够配合使用
./sersync -n 8 -o abc.xml -r -d
表示,设置线程池工做线程为8个,指定abc.xml做为配置文件,在实时监控前做一次总体同步,以守护进程方式在后台运行。
一般状况下,对本地到远程总体同步一遍后,在后台运行实时同步。
./sersync -r -d
——leeypp@gmail.com