大佬教程收集整理的这篇文章主要介绍了CentOS6.8使用Rsync+Inotify-tools实现数据实时同步,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
[root@localhostsrc]#yum-yinstallrsyncxinetd [root@localhostsrc]#cp/etc/xinetd.d/rsync{,default} [root@localhostsrc]#vim/etc/xinetd.d/rsync servicersync { disable=no#修改为no flags=IPv6 socket_type=stream wait=no user=root server=/usr/bin/rsync server_args=--daemon log_on_failure+=USERID } [root@localhostsrc]#/etc/init.d/xinetdstart#CentOS中是以xinetd来管理Rsync服务的 [root@localhostsrc]#vim/etc/rsyncd.conf#创建配置文件 logfile=/var/log/rsyncd.log#日志文件位置,启动rsync后自动产生这个文件,无需提前创建 pidfile=/var/run/rsyncd.pid#pid文件的存放位置 lockfile=/var/run/rsync.lock#@L_192_6@maxconnections参数的锁文件 secretsfile=/etc/rsync.pass#用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件 motdfile=/etc/rsyncd.Motd#rsync启动时欢迎信息页面文件位置(文件内容自定义) [app_rsync_client]#自定义名称 path=/app/rsync_client/#rsync服务端数据目录路径 comment=app_rsync_client#模块名称与[app_rsync_client]自定义名称相同 uid=root#设置rsync运行权限为root gid=root#设置rsync运行权限为root port=873 usechroot=no#默认为true,修改为no,增加对目录文件软连接的备份 readonly=no设置rsync服务端文件为读写权限 list=no#不显示rsync服务端资源列表 macconnections=200 timeout=600 authusers=rsync#执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开 hostsallow=10.15.43.100#允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开 hostsdeny=10.10.2.84#禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开 #先允许后拒绝 [root@localhostsrc]#vim/etc/rsync.pass#配置文件,添加以下内容 rsync:123456#格式,用户名:密码,可以设置多个,每行一个用户名:密码 [root@localhostsrc]#chmod600/etc/rsyncd.conf [root@localhostsrc]#chmod600/etc/rsync.pass [root@localhostsrc]#/etc/init.d/xinetdrestart@H_944_14@
[root@localhostrsync_server]#whereisrsync#查看系统是否已安装rsync rsync:/usr/bin/rsync/usr/share/man/man1/rsync.1.gz#说明已经安装 [root@localhostrsync_server]#@H_944_14@yum install xinetd #已安装rsync只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的 @H_944_14@yum install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd
[root@localhostrsync_server]#vim/etc/xinetd.d/rsync servicersync { disable=no#修改为no flags=IPv6 socket_type=stream wait=no user=root server=/usr/bin/rsync server_args=--daemon log_on_failure+=USERID } [root@localhostrsync_server]#/etc/init.d/xinetdrestart [root@localhostrsync_server]#vim/etc/passwd.txt 123456 [root@localhostrsync_server]#chmod600/etc/passwd.txt@H_944_14@
[root@localhostsrc]#mkdir/app/rsync_client/test [root@localhostsrc]#touch/app/rsync_client/test/file [root@localhostrsync_server]#rsync-avH--port=873--progress--delete/app/rsync_server/rsync@10.15.43.228::app_rsync_client--password-file=/etc/passwd.txt sendingincrementalfilelist ./ file 0100%0.00kB/s0:00:00(xfer#1,to-check=0/2) sent81bytesreceived30bytes222.00bytes/sec @R_161_10586@lsizeis0speedupis0.00 [root@localhostrsync_server]#@H_944_14@/app/rsync_server/数据源的目录 @H_944_14@-password-file=/etc/passwd.txt数据源的密码文件 @H_944_14@rsync@10.15.43.228::app_rsync_client rsync目标端rsync服务端配置的用户名,app_rsync_client目标端rsync服务端配置的模块名称 @H_944_14@
[root@localhostrsync_client]#ls file [root@localhostrsync_client]#@H_944_14@
[root@localhostsrc]#ll/proc/sys/fs/inotify#查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify @R_161_10586@l0 -rw-r--r--1rootroot0Jul2710:32max_queued_events -rw-r--r--1rootroot0Jul2710:32max_user_instances -rw-r--r--1rootroot0Jul2710:32max_user_watches [root@localhostsrc]#uname-r#Linux下支持inotify的内核最小为2.6.13 2.6.32-642.el6.x86_64 [root@localhostsrc]#tarzxvfinotify-tools-3.14.tar.gz [root@localhostsrc]#cdinotify-tools-3.14 [root@localhosTinotify-tools-3.14]#./configure--prefix=/app/inotify [root@localhosTinotify-tools-3.14]#make&&makeinstall [root@localhosTinotify-tools-3.14]#vim/etc/profile#设置系统环境变量 exportPATH=/app/inotify/bin:$PATH [root@localhosTinotify-tools-3.14]#source/etc/profile [root@localhosTinotify-tools-3.14]#echo"/app/inotify/lib">/etc/ld.so.conf.d/inotify.conf [root@localhosTinotify-tools-3.14]#ln-s/app/inotify/include/usr/include/inotify [root@localhosTinotify-tools-3.14]#sysctl-a|egrep-i"max_queued_events|max_user_watches|max_user_instances"#修改inotify默认参数(inotify默认内核参数值太小) fs.inotify.max_user_instances=128 fs.inotify.max_user_watches=8192 fs.inotify.max_queued_events=16384 fs.epoll.max_user_watches=201420 [root@localhosTinotify-tools-3.14]#vim/etc/sysctl.conf fs.inotify.max_user_instances=65535 fs.inotify.max_user_watches=99999999 fs.inotify.max_queued_events=99999999 [root@localhosTinotify-tools-3.14]#cat/proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events} 65535 99999999 99999999 [root@localhosTinotify-tools-3.14]#@H_944_14@max_queued_events: @H_944_14@inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确 @H_944_14@max_user_watches: @H_944_14@要同步的文件包含多少目录,可以用:find /app/rsync_server/ -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/app/rsync_server/为同步文件目录) @H_944_14@max_user_instances: @H_944_14@每个用户创建inotify实例最大值 @H_944_14@
[root@localhosTinotify]#catrsync.sh #!/bin/bash src_dir="/app/rsync_server/" dst_dir="app_rsync_client" exclude_dir="/app/inotify/exclude.list" rsync_user="rsync" rsync_passwd="/etc/passwd.txt" dst_ip="10.15.43.22810.10.2.84" rsync_command(){ rsync-avH--port=873--progress--delete--exclude-from=$exclude_dir$src_dir$rsync_user@$ip::$dst_dir--password-file=$rsync_passwd } foripin$dst_ip;do rsync_command done /app/inotify/bin/inotifywait-mrq--timefmt'%d/%m/%y%H:%M'--format'%T%w%f%e'-eclose_write,modify,delete,create,attrib,move$src_dir\ |whilereadfile;do foripin$dst_ip;do rsync_command echo"${filE}wasrsynced">>/tmp/rsync.log2>&1 done done [root@localhosTinotify]#chmod+xrsync.sh [root@localhosTinotify]#touch/app/inotify/exclude.list [root@localhosTinotify]#vim/etc/rc.d/rc.local nohup/bin/sh/app/inotify/rsync.sh& [root@localhosTinotify]#nohup/bin/sh/app/inotify/rsync.sh&@H_944_14@src_dir="/app/rsync_server/"#源服务器同步目录 @H_944_14@dst_dir="app_rsync_client"#目标服务器rsync同步目录模块名称 @H_944_14@exclude_dir="/app/inotify/exclude.list"#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径; @H_944_14@例如:不需要同步/app/rsync_server/"目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写 @H_944_14@a/ @H_944_14@b/b1/ @H_944_14@
[root@localhostrsync_server]#touchtest{1..9} [root@localhostrsync_server]#touchtest{a..j} [root@localhostrsync_server]#ls test1test2test3test4test5test6test7test8test9testaTESTBTESTCtestdtestetestftestgtesthtestitestj [root@localhostrsync_server]#pwd /app/rsync_server [root@localhostrsync_server]#@H_944_14@在rsync_client (目标端)10.15.43.228、10.10.2.84上查看已经同步
[root@localhostrsync_client]#ls test1test2test3test4test5test6test7test8test9testaTESTBTESTCtestdtestetestftestgtesthtestitestj [root@localhostrsync_client]#pwd /app/rsync_client [root@localhostrsync_client]#@H_944_14@如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。 @H_944_14@至此,Linux下Rsync+Inotify-tools实现数据实时同步完成。如果要双向同步可以把以上反过来部署次。 @H_944_14@
以上是大佬教程为你收集整理的CentOS6.8使用Rsync+Inotify-tools实现数据实时同步全部内容,希望文章能够帮你解决CentOS6.8使用Rsync+Inotify-tools实现数据实时同步所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。