首页 技术 正文
技术 2022年11月16日
0 收藏 433 点赞 4,414 浏览 9615 个字

1、sersync项目:sersync项目利用inotify与rsync技术实现对服务器数据实时同步到解决方案,其中inotify用于监控sersync所在服务器上文件系统的事件变化,rsync是目前广泛使用的本地及异地数据同步工具,其优点是只对变化的目录数据操作。sersync项目的优点:1)使用C++编写,对linux系统文件产生的临时文件和重复的文件操作会进行过滤,再结合rsync同步到时候,会减少网络资源,因此速度更快。2)sersync配置起来很简单,在http://code.google.com/p/sersync/downloads/list处下载源码。其中bin目录下是已经编译好的二进制文件,配合bin目录下的xml文件直接使用即可。3)使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。4)sersync自带出错处理机制,通过失败队列对出错的文件重新同步,如果扔失败,则每10个小时对同步失败的文件再重新同步。5)sersync自带crontab功能,只需在xml配置文件中开启,即可按预先的配置,隔一段时间整理同步一次。6)sersync自带socket与http的协议扩展,可以满足有特需要去掉公司二次开发。inotify识别事件参考:http://www.ibm.com/developerworks/cn/linux/1-inotifynewhttp://blog.johntechinfo.com/?p=1222、配置前准备1)环境准备说明:192.168.233.129 rsync server(rsync服务端)192.168.233.132 sersync2)配置前检查cat /etc/redhat-releaseCentOS release 6.6 (Final)uname -r2.6.32-504.el6.x86_64uname -mx86_643)配置rsync服务端#yum安装:yum install -y rsync# 下载编译安装很简单,tar==>configure –ferfix=/usr/local/rsync ==>make && make install 不详细列出步骤了,我这选择的是YUM安装,注意在编译安装后注意路径环境变量就可以了,其他都一样。wget http://pkgs.repoforge.org/rsync/rsync-3.0.9-2.el6.rfx.x86_64.rpm#检查安装rpm -qa rsyncrsync-3.0.6-12.el6.x86_64#建立密码文件,并修改权限echo “rsyncback:123.com” >/etc/rsync.passwordchmod 600 /etc/rsync.password#建立配置文件:cat >>/etc/rsyncd.conf<<EOF#/etc/rsyncd.confuid = rootgid = rootuse chroot = nomax connections = 200hosts allow = *timeout = 600log file = /var/log/rsyncd.logpid file = /var/run/rsyncd.pidlock file = /var/run/rsync.lockignore errorsread only = falselist = falsehost deny = 0.0.0.0/32auth users = rsyncbacksecrets file = /etc/rsync.passwordtransfer logging = yes#Module definitions[data]comment = data by pjypath = /data/####################################[www]comment = www by pjypath = /data/www/###################################[bbs]comment = bbs by pjypath = /data/bbs/##################################[blog]comment = blog by pjypath = /data/blog/##################################EOF#创建模块监控目录,我这用的是root用户所以没有配置权限,如果指定的用户不同时,需要给目录配置用户权限。mkdir -p /data/{www,bbs,blog}tree /data/data├── bbs├── blog└── www#启动rsync服务:/usr/bin/rsync –damonps -ef|grep rsync|grep -v greproot 7858 1 0 20:09 ? 00:00:00 rsync –daemonnetstat -lntp|grep 873tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7858/rsynctcp 0 0 :::873 :::* LISTEN 7858/rsynclsof -i :873COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMErsync 7858 root 3u IPv4 224418 0t0 TCP *:rsync (LISTEN)rsync 7858 root 5u IPv6 224419 0t0 TCP *:rsync (LISTEN#在sersync端建立密码文件,并测试同步是否正常echo “123.com” >/etc/rsync.passwordchmod 600 /etc/rsync.passwordrsync -zvaP –password-file=/etc/rsync.password rsyncback@192.168.233.129::data /data/tree data/./├── bbs├── blog└── www3、安装sersync服务wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gztar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gzGNU-Linux-x86/GNU-Linux-x86/sersync2GNU-Linux-x86/confxml.xmlcp -rf GNU-Linux-x86/ /usr/local/sersync/cd /usr/local/sersync/mkdir bin logs confmv confxml.xml conf/mv sersync2 bin/sersynctree.├── bin│   └── sersync├── conf│   └── confxml.xml└── logs#备份配置文件cp conf/confxml.xml conf/confxml.xml.bak$(date +%F)#配置环境变量echo “export PATH=$PATH:/usr/local/sersync/bin” >>/etc/profiletail -1 /etc/profilesource /etc/profilewhich sersync1)单实例配置:#修改sersync配置文件:(有注释说明的地方是需要修改的)<?xml version=”1.0″ encoding=”ISO-8859-1″?><head version=”2.5″><host hostip=”localhost” port=”8008″></host><debug start=”false”/><fileSystem xfs=”false”/><filter start=”false”><exclude expression=”(.*)\.svn”></exclude><exclude expression=”(.*)\.gz”></exclude><exclude expression=”^info/*”></exclude><exclude expression=”^static/*”></exclude><delete start=”true”/><createFolder start=”true”/><createFile start=”false”/><closeWrite start=”true”/><moveFrom start=”true”/><modify start=”false”/></inotify><sersync><localpath watch=”/data”> #指定本地要同步到目录<remote ip=”192.168.233.129″ name=”data”/> #指定rsync服务端的ip地址和模块名</localpath><rsync><commonParams params=”-aruz”/> #指定rsync同步选项<auth start=”true” users=”rsyncback” passwordfile=”/etc/rsync.password”/> #开启指定密码,对应rsync同时的用户名和密码文件#如:rsync -zvaP –password-file=/etc/rsync.password rsyncback@192.168.233.129::data /data/<userDefinedPort start=”false” port=”874″/><!– port=874 –><timeout start=”false” time=”100″/><!– timeout=100 –> #指定超时100秒<ssh start=”false”/></rsync><failLog path=”/usr/local/sersync/logs/www_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><plugin name=”command”><param prefix=”/bin/sh” suffix=”” ignoreError=”true”/> <!–prefix /opt/tongbu/mmm.sh suffix–><filter start=”false”><include expression=”(.*)\.php”/><include expression=”(.*)\.sh”/></filter></plugin><plugin name=”socket”><localpath watch=”/opt/tongbu”><deshost ip=”192.168.138.20″ port=”8009″/></localpath></plugin><plugin name=”refreshCDN”><localpath watch=”/data0/htdocs/cms.xoyo.com/site/“><cdninfo domainname=”ccms.chinacache.com” port=”80″ username=”xxxx” passwd=”xxxx”/><sendurl base=”http://pic.xoyo.com/cms”/><regexurl regex=”false” match=”cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images”/></localpath></plugin></head>#配置英文变量,以防乱码:LANG=EN#启动监控服务(参数说明在后面)sersync -r -d -o /usr/local/sersync/conf/confxml.xmlset the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command paramoption: -r rsync all the local files to the remote servers before the sersync workoption: -d run as a daemonoption: -o config xml name: /usr/local/sersync/conf/confxml.xmldaemon thread num: 10parse xml config filehost ip : localhost host port: 8008daemon start,sersync run behind the consoleuse rsync password-file :user is rsyncbackpasswordfile is /etc/rsync.passwordconfig xml parse successplease set /etc/rsyncd.conf max connections=0 Manuallysersync 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 onceworking please wait…execute command: cd /data/www && rsync -aruz -R –delete ./ rsyncback@192.168.233.129::www –password-file=/etc/rsync.password >/dev/null 2>&1run the sersync:watch path is: /data/www#单实例监控完成,测试成功!2)多实例配置(此次只用了一台rsync服务器模拟多台rsync服务端实时同步配置,换成多台服务器只需修改ip地址和模块即可)#靠谱3个配置文件,模拟www,bbs,blog代码同步分发cd /usr/local/sersync/confcp confxml.xml www_confxml.xmlcp confxml.xml bbs_confxml.xmlcp confxml.xml blog_confxml.xml#修改配置文件:(这里只列出修改部分)cat www_confxml.xml<sersync><localpath watch=”/data/www”><remote ip=”192.168.233.129″ name=”www”/></localpath><rsync><commonParams params=”-aruz”/><auth start=”true” users=”rsyncback” passwordfile=”/etc/rsync.password”/><userDefinedPort start=”false” port=”874″/><!– port=874 –><timeout start=”false” time=”100″/><!– timeout=100 –><ssh start=”false”/></rsync><failLog path=”/usr/local/sersync/logs/www_rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>cat bbs_confxml.xml<sersync><localpath watch=”/data/bbs”><remote ip=”192.168.233.129″ name=”bbs”/></localpath><rsync><commonParams params=”-aruz”/><auth start=”true” users=”rsyncback” passwordfile=”/etc/rsync.password”/><userDefinedPort start=”false” port=”874″/><!– port=874 –><timeout start=”false” time=”100″/><!– timeout=100 –><ssh start=”false”/></rsync><failLog path=”/usr/local/sersync/logs/bbs_rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>cat blog_confxml.xml<sersync><localpath watch=”/data/blog”><remote ip=”192.168.233.129″ name=”blog”/></localpath><rsync><commonParams params=”-aruz”/><auth start=”true” users=”rsyncback” passwordfile=”/etc/rsync.password”/><userDefinedPort start=”false” port=”874″/><!– port=874 –><timeout start=”false” time=”100″/><!– timeout=100 –><ssh start=”false”/></rsync><failLog path=”/usr/local/sersync/logs/blog_rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>#修改注明:(版本号为sersync2.5.4_64)1.修改默认配置的23到28行,指定本地同步目录,对端ip和模块2.修改默认配置的30到33行,指定rsync同步时的参数,开启密码文件,指定用户,密码文件路径。3.修改默认配置的36行,指定消息队列文件路径。#重启动sersync服务:ps -ef|grep sersyncroot 14270 1 0 21:44 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/confxml.xmlroot 14445 13674 0 22:00 pts/0 00:00:00 grep sersyncpkill sersyncps -ef|grep sersyncroot 14459 13674 0 22:01 pts/0 00:00:00 grep sersyncsersync -d -o /usr/local/sersync/conf/www_confxml.xmlsersync -d -o /usr/local/sersync/conf/bbs_confxml.xmlsersync -d -o /usr/local/sersync/conf/blog_confxml.xml#查看进程ps -ef|grep sersyncroot 14464 1 0 22:02 ? 00:00:00 sersync -d -o /usr/local/sersync/conf/www_confxml.xmlroot 14482 1 0 22:03 ? 00:00:00 sersync -d -o /usr/local/sersync/conf/bbs_confxml.xmlroot 14500 1 0 22:03 ? 00:00:00 sersync -d -o /usr/local/sersync/conf/blog_confxml.xml#测试文件同步cd /datafor i in `ls`;do echo ddd >>$i/$i$i.sh;donetree /./├── bbs│   ├── bbsbbs.sh│   └── bbs.log├── blog│   ├── blogblog.sh│   └── blog.log└── www├── www.log└── wwwwww.sh#rsync服务端,三个模块实时同步成功!#将sersync服务加入到开启自启动cat >>/etc/rc.local<<EOF#sync data to 192.168.233.192sersync -d -o /usr/local/sersync/conf/www_confxml.xmlsersync -d -o /usr/local/sersync/conf/bbs_confxml.xmlsersync -d -o /usr/local/sersync/conf/blog_confxml.xmlEOFtail -4 /etc/rc.local#sync data to 192.168.233.192sersync -d -o /usr/local/sersync/conf/www_confxml.xmlsersync -d -o /usr/local/sersync/conf/bbs_confxml.xmlsersync -d -o /usr/local/sersync/conf/blog_confxml.xml#查看实时监控进程,应该是三个sersync守护进程,ps -ef |grep rsyncroot 14464 1 0 22:02 ? 00:00:02 sersync -d -o /usr/local/sersync/conf/www_confxml.xmlroot 14482 1 0 22:03 ? 00:00:02 sersync -d -o /usr/local/sersync/conf/bbs_confxml.xmlroot 14500 1 0 22:03 ? 00:00:02 sersync -d -o /usr/local/sersync/conf/blog_confxml.xml参数说明:-r 开启实时监控之前对主服务器目录与远程目录目录进行一次整体同步,如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步.-o 指定配置文件,如不指定sersync会使用sersync可执行文件目录下的默认配置文件confxml.xml.-n 指定默认的线程池的线程总数,如不指定默认启动线程池数量是10个-d 后台启动服务-m 不进行同步,只运行插件,如:sersync -m command 则在监控到事件后,不对远程目录服务器进行同步,而是直接运行command插件#后续配置文件改进更新。。。。。。。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290