实现目标:将主机A(192.168.239.128)下的一个目录,同步到主机B(192.168.239.130)下的一个目录,并对A主机进行监控,当A主机该目录下的文件发生变化立即同步至主机B中。
1.安装rsync
rsync安装很简单,不管是同步端机器还是被同步端机器,都只需安装rsync包既可,如果配置了yum源,可直接使用yum安装,命令如下:
# yum -y install rsync |
2.启动rsync
启动rsync的方法很简单,有以下两种方法,推荐使用第二种方法:
方法一:--daemon参数方式,是让rsync以服务器模式运行,命令如下:
启动 # /usr/bin/rsync --daemon --config=/etc/rsyncd.conf 查看端口 # netstat -tulnp | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27064/rsync tcp 0 0 :::873 :::* LISTEN 27064/rsync
注释:--config用于指定rsyncd.conf的位置,如果默认在/etc目录下默认可以不用指定 |
方法二:xinetd监管,只需将/etc/xinetd.d/rsync文件中的disable = yes 改为no即可,如下:
# default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { disable = no #将yes改为no flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID } |
然后启动xinetd服务
# service xinetd start Starting xinetd: [ OK ] |
查看端口,会发现rsync并没有监听,但是xinetd监听了873端口
# netstat -tulnp | grep rsync # netstat -tulnp | grep 873 tcp 0 0 :::873 :::* LISTEN 28245/xinetd |
修改/etc/xinetd.d/rsync主要是要打开rsync这个daemon, 一旦有rsync client要连接时, xinetd会把它转移给 rsyncd(port 873)
第一部分:B服务器端
1.配置文件参数说明
全局参数 | 说明 |
motd file | 定义服务器信息,需自己编辑,默认没有,也可不配置 |
pid file | PID文件,默认没有,一般指定为:/var/run/rsyncd.pid |
port | 端口,默认指定为873 |
address | 指定服务器IP地址 |
模块参数 | 说明 |
comment | 描述信息,自定义 |
path | 需要同步的目录路径 |
use chroot | 如果为yes,rsync进程将chroot 到文件系统中的目录中,好处是保护系统被安装漏洞侵袭的可能。缺点是需要超级用户权限。另外对符号链接文件,将会排除在外。 |
max connetions | 允许客户端最大链接数,0表示无限制 |
log file | 日志文件,一般设定为:/var/log/rsync.log |
lock file | 锁文件,用来记录最大连接数,默认是/var/lock/rsyncd.lock |
read only | 只读,默认为yes,表示不让客户端上传文件到服务器 |
write only | 只写,默认为no,表示客户端可以下载文件,yes表示不能下载 |
list | 列出服务器上提供同步的数据目录,默认为yes |
uid | 服务器传输文件时使用哪个用户执行,默认是nobody,如果遇到权限文件,可能需要root用户 |
gid | 服务器传输文件时使用哪个用户组执行,默认是nobody |
exclude | 排除不需要同步的目录或文件,多个用空格隔开 |
auth users | 认证用户,必须是服务器上存在的用户 |
secrets file | 指定密码文件路径 |
strict modes | 是否检查密码文件权限,拥有组和其他人必须为0 |
hosts allow | 允许哪些地址可以同步,可以是IP或网段,多个用空格隔开 |
hosts deny | 拒绝哪些地址的同步,可以是IP或网段,多个用空格隔开 |
ignore errors | 忽略IO错误 |
log format | 日志格式 |
timeout | 超时时间 |
2.B服务器端操作步骤
(1)准备步骤 mkdri /data/ #创建一个目录,接受来自A主机的文件或目录 useradd rsync #增加一个用户,用于启动进程传输文件,也可以使用root chown -R rsync:rsync /data #修改目录属性 (2)创建配置文件 vim /etc/rsyncd.conf ##rsync.conf config start uid = rsync gid = rsync use chroot = no max connetctions = 200 timeout = 100 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [backup] path = /data/ ignore errors read only = no list = no hosts allow = 192.168.239.128 auth users = rsync_backup secrets file = /etc/rsync.password ##rsync config end chmod 600 /etc/rsyncd.conf (3)创建密码文件: echo "rsync_backup:123456">/etc/rsync.password chmod 600 /etc/rsync.password (4)启动服务: rsync --daemon (5)设置开机自启rsync服务: vim /etc/rc.local 添加 # rsync server progress /usr/bin/rsync --daemon
第二部分:A客户端操作步骤
(1)配置密码文件: echo "123456">/etc/rsync.password chmod 600 /etc/rsync.password (2)手工测试数据推送: rsync -avzP /etc/hosts rsync_backup@192.168.239.130::backup --password-file=/etc/rsync.password #传输一个文件到B上,看是否成功! 在B服务器端进行查看,客户端的/etc/hosts文件已经同步至备份的服务器中。
(3)在A客户端部署sersync服务
1、查看服务器内核是否支持inotify ll /proc/sys/fs/inotify 2、修改inotify默认参数(inotify默认内核参数值太小) 查看系统默认参数值: sysctl -a | grep max_queued_events 结果是:fs.inotify.max_queued_events = 16384 sysctl -a | grep max_user_watches 结果是:fs.inotify.max_user_watches = 8192 sysctl -a | grep max_user_instances 结果是:fs.inotify.max_user_instances = 128 修改参数: sysctl -w fs.inotify.max_queued_events="99999999" sysctl -w fs.inotify.max_user_watches="99999999" sysctl -w fs.inotify.max_user_instances="65535" vi /etc/sysctl.conf #添加以下代码 fs.inotify.max_queued_events=99999999 fs.inotify.max_user_watches=99999999 fs.inotify.max_user_instances=65535 :wq! #保存退出 3.下载sersync 下载地址:https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz 4.配置sersync #上传软件至A服务器上: sftp> put D:/软件/常用软件包/sersync2.5.4_64bit_binary_stable_final.tar.gz #解压文件到对应目录: tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/ #进入对应目录修改目录名: cd /usr/local/ mv GNU-Linux-x86 sersync #复制配置文件 cd sersync/ cp -a confxml.xml{,.`date +%F`} #修改配置文件部分 #第1部分:设置本地监控的同步目录、远端服务器、备份模块(备份模块在远端服务器/etc/rsyncd.conf定义) <localpath watch="/data/"> <remote ip="192.168.239.130" name="backup"/> #<!--<remote ip="192.168.8.39" name="tongbu"/>--> #<!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> #第2部分:设置认证部分、服务器端的认证用户与密码文件存放位置(在远端服务器/etc/rsyncd.conf定义) <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="true" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> 类似于rsync -avzP /etc/hosts rsync_backup@192.168.239.130::backup --password-file=/etc/rsync.password #第3部分:设置同步失败日志存放位置,当同步失败时记录下来,并且每60分钟对失败的log进行重新同步 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> <crontab start="true" schedule="600"><!--600mins-->
修改完成后,完整的配置文件如下:
cat /usr/local/sersync/confxml.xml <?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> </filter> <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="false"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="false"/> </inotify> <sersync> <localpath watch="/data/"> <remote ip="192.168.239.130" name="backup"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> <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="true" 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/p_w_picpaths"/> </localpath> </plugin> </head>
(4)设置sersync监控开机自动执行
vi /etc/rc.d/rc.local #编辑,在最后添加一行 /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #设置开机自动运行脚本
(5)添加脚本监控sersync是否正常运行
vi /root/check_sersync.sh #编辑,添加以下代码 #!/bin/bash sersync="/usr/local/sersync/sersync2" confxml="/usr/local/sersync/confxml.xml" status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l) if [ $status -eq 0 ]; then $sersync -d -r -o $confxml & else exit 0; fi chmod +x /root/check_sersync.sh #添加脚本执行权限 vi /etc/crontab #编辑,在最后添加下面一行 */5 * * * * root /root/check_sersync.sh > /dev/null 2>&1 #每隔5分钟执行一次脚本 service crond reload #重新加载服务
(6)客户端进行验证,在同步的服务器上进行查看
cd /data/ for i in `seq 100`;do mkdir $i;done
补充:sersync命令参数详解
1.命令参数说明
Sersync参数 | 说明 |
./sersync -r | -r参数作用是:开启实时监控的之前对主服务器目录与远程目标机器的目录进行一次整体同步;如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则要以 -r参数运行sersync,将本地与远程整体同步一次; 提别说明:如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步; |
./sersync -o xx.xml | 不指定 -o参数: sersync使用sersync可执行文件目录下的默认配置文件confxml.xml 指定 -o 参数:可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步 |
./sersync -n num | -n参数为:指定默认的线程池的线程总数; 例如: ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过该参数调低,如果机器配置较高,可以调高默认的线程总数,提升同步效率; |
./sersync -d | -d参数为:后台服务,通常情况下使用 -r参数对本地到远端整体同步一遍后,在后台运行此参数启动守护进程实时同步;在第一次整体同步时,-d 和 -r参数经常会联合使用; |
./sersync -m pluginName | -m参数:不进行同步,只运行插件 ./sersync -m pluginName 例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件 |
组合命令使用说明: | |
-n 8 -o liubl.xml -r -d | 多个参数可以配合使用,例如:./sersync -n 16 -o config.xml -r -d 表示设置线程池工作线程为16个,指定liubl.xml作为配置文件,在实时监控前 做一次整体同步,以守护进程方式在后台运行; |
./sersync --help | 很遗憾,它没有查看帮助(需要的话2条路,要么看源代码,要么自测求验证) |
2.sersync服务配置文件参数详解:
1.xml配置文件的注释不用“#”,而是<!-- 中间是注释内容 --> 2.Debug开启开关:<debug start="false"/> 设置为true,表示开启debug模式,会在sersync正在运行的控制台打印inotify时间与rsync同步命令; 3.XFS文件系统开关:<fileSystem xfs="false"/> 对于xfs文件系统的用户,需要将这个选项开启,才能使用sersync正常工作; 4.filter文件过滤功能 <filter start="false"> <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> 排除一些文件,不需要 5.inotify的状态 <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="false"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="false"/> </inotify> 对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为false来提高性能,减少rsync通讯; 因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的时间close_write,同样可以实现文件完整同步; 注意:强将creatFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控;所以除非特殊需要,请开启; 默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete参数设置为false,则不对删除事件进行监控;
参考:http://www.osyunwei.com/archives/7447.html
转载于:https://blog.51cto.com/nxyboy/1940413