10.245.6.54 主 10.245.6.53从1 10.245.6.52 从2 想实时同步54/eshop/eshopres/目录下的文件或目录 1、检测分发服务器系统是否满足需求,要求内核大于2.3 uname -r #查看系统内核 ll /proc/sys/fs/inotify/ 如图则满足需求。 2、在分发服务器下载最新版并编译安装 54需要安装: inotify-tools-3.14.tar.gz cd #回到用户目录 wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz #下载最新版 tar zxvf inotify-tools-3.14.tar.gz #解压到当前目录 cd inotify-tools-3.14 #进入目录 ./configure --prefix=/alidata/server/inotify/ #编译配置,这里是指定位置(你的文件目录) make #编译 make install #安装 make clean #从源文件夹清除二进制对象等 cd /alidata/server/inotify/bin ./inotifywait --help #查看是否安装完成一般有错误上面几个步骤会有提示 ./inotifywait -mrq --format \'%Xe %w%f\' -e modify,create,delete,attrib /alidata/tmp/ #m是开启监测,r递归目录,/alidata/tmp监测的目录 touch /alidata/tmp/test1 #新开个tty创建个文件会有如下图输出<br><br> #为方便使用可以选择设置 #设置系统环境变量,添加软连接 #echo "PATH=$PATH:/alidata/server/inotify/bin" >>/etc/profile.d/inotify.sh #source /etc/profile.d/inotify.sh #使设置立即生效 #echo "/alidata/server/inotify/lib" >/etc/ld.so.conf.d/inotify.conf && ldconfig #ln -s /alidata/server/inotify/include /usr/include/inotify 正式配置: #!/bin/bash SRCDIR=/eshop/eshopres/ USER=backup DESTDIR=eshopres excludedir=/usr/local/inotify/exclude.list rsyncpass=/eshop/rsync/rsyncd.passwd dstip="10.245.6.53 10.245.6.52" dstip1=10.245.6.53 dstip2=10.245.6.52 datelog=`date -d today +"%Y-%m-%d"` dirpath = \\eshop\\rsync\\ filelogName=${dirpath}sync${datelog}.log echo ${filelogName} echo "the file name is $filelogName" if [ ! -f $filelogName ];then touch $filelogName echo ${filelogName}\' create ok.\' else echo file exist fi /alidata/server/inotify/bin/inotifywait -mrq --timefmt \'%d/%m/%y %H:%M\' --format \'%T %w%f%e\' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file do for ip in $dstip do /eshop/rsync/bin/rsync -avH --port=873 --progress --delete-before --exclude-from=$excludedir $SRCDIR $USER@$ip::$DESTDIR --password-file=$rsyncpass #echo " ${file} was rsynced " >> /eshop/rsync/rsync.log 2>&1 #/eshop/rsync/bin/rsync -avH --port=873 --progress --delete-before --exclude-from=$excludedir $SRCDIR $USER@$dstip1::$DESTDIR --password-file=$rsyncpass & #/eshop/rsync/bin/rsync -avH --port=873 --progress --delete-before --exclude-from=$excludedir $SRCDIR $USER@$dstip2::$DESTDIR --password-file=$rsyncpass #echo " ${file} was rsynced " >> /eshop/rsync/rsync.log 2>&1 echo " ${file} was rsynced " >> $filelogName 2>&1 done done 至此已经可以检测到分发服务器文件改动了。 在对一个大磁盘进行inotify监听时,爆出如下错误: Failed to watch /mnt/; upper limit on inotify watches reached! Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches’. cat一下这个文件,默认值是8192, echo 8192000 > /proc/sys/fs/inotify/max_user_watches 即可~ 3、编译安装rsync到内容分发服务器在这里充当客户端的角色 rsync-3.1.3.tar.gz wget https://download.samba.org/pub/rsync/src/rs tar zxvf rsync-3.1.2.tar.gz cd rsync-3.1.2 ./configure --prefix=/alidata/server/rsync ./configure --prefix=/eshop/rsync make make install 接下来就是文件同步,检测到文件变动后主动向目标服务器推送,详情后面说。 4、编译安装rsync到目标服务器并启用服务 添加主配置文件: vim /etc/rsyncd.conf uid = root gid = root port = 873 use chroot = no hosts allow = * max connections = 3 motd file = /alidata/passwd/rsync/rsyncd.motd #问候语 pid file = /alidata/server/rsync/rsyncd.pid lock file = /alidata/server/rsync/rsync.lock log file = /alidata/log/rsync/rsyncd.log transfer logging = yes #传输日志 ignore errors #忽略错误 [netho] path = /alidata/tmp/rsynctest #同步路径 auth users = netho #使用用户必须是服务器真实用户 secrets file = /alidata/passwd/rsync/rsyncd.pass #用户认证文件 list = no read only = no #只读 建立认证文件,我使用的是apache的认证工具生成的密码复杂一点嘛(嫌麻烦直接手动输入echo woshimima >/alidata/passwd/rsync/rsyncd.pass) mkdir -p /alidata/passwd/rsycn htpasswd -cb /alidata/passwd/rsync/rsyncd.pass netho Netho123456789<br>chmod 600 /alidata/passwd/rsync/rsyncd.pass #为什么要这一步呢?和strict modes =yes/no有关系么,没时间实验了,擦 设定rsyncd.motd 文件; vim /alidata/passwd/rsync/rsyncd.motd 复制以下代码 ++++++++++++++++++++++++++++++++++++++++++++++ Welcome to use the rsync services! 2018------2099 ++++++++++++++++++++++++++++++++++++++++++++++ 正式配置: uid = ftp gid = ftp use chroot = no max connections = 36000 strict modes = yes hosts allow = 10.245.6.0/24 #配置主机ip可以空格,允许多个 hosts deny = 0.0.0.0/32 port = 873 pid file = /eshop/rsync/rsyncd.pid lock file = /eshop/rsync/rsync.lock log file = /eshop/rsync/rsyncd.log [eshop] #test为模块名称后续同步会用到 path = /home/test ignore errors #read only = false #此目录需要读写全部权限 read only = no list = false auth users = backup secrets file = /eshop/rsync/rsyncd.passwd ignore errors = no transfer logging = yes log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. 启动rsync服务: cd /eshop/rsync/bin ./rsync --daemon --config=/eshop/rsync/rsyncd.conf & 打开指定端口 ufw allow 873/tcp 测试链接 ps -ef |grep rsync #进程 netstat -anop | grep rsync #端口信息 ./rsync -rdt rsync://127.0.0.1:873/ #看到下图我们设置的欢迎信息(mot)说明启动成功服务正常 为了方便我们使用脚本来管理将其加入service touch /etc/init.d/rsyncd chmod +x /etc/init.d/rsyncd vim /etc/init.d/rsyncd 复制以下代码 : #!/bin/bash # description: rsync server # processname: rsyncd status1=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v \'grep\') pidfile="/alidata/server/rsync/rsyncd.pid" rsync="/alidata/server/rsync/bin/rsync" start_rsync="${rsync} --daemon --config=/etc/rsyncd.conf" function rsyncstart() { if [ "${status1}X" == "X" ];then ${start_rsync} status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v \'grep\') if [ "${status2}X" != "X" ];then echo "rsync service start.......OK" fi else echo "rsync service is running !" fi } function rsyncstop() { if [ "${status1}X" != "X" ];then kill -9 $(cat $pidfile) rm -f $pidfile status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v \'grep\') if [ "${statusw2}X" == "X" ];then echo "rsync service stop.......OK" fi else echo "rsync service is not running !" fi } function rsyncstatus() { if [ "${status1}X" != "X" ];then echo "rsync service is running !" else echo "rsync service is not running !" fi } function rsyncrestart() { if [ "${status1}X" == "X" ];then echo "rsync service is not running..." rsyncstart else rsyncstop rsync --daemon --config=/usr/local/server/rsync/etc/rsyncd.conf status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v \'grep\') if [ "${status2}X" != "X" ];then echo "rsync service start.......OK" else echo "rsync service is not running !" fi fi } case "$1" in "start") rsyncstart ;; "stop") rsyncstop ;; "status") rsyncstatus ;; "restart") rsyncrestart ;; *) echo echo "Usage: $0 start|stop|restart|status" echo esac exit 0 设置开机启动 echo "/etc/init.d/rsyncd start" >>/etc/rc.local 5、目标服务器配好以后,在内容分发服务器上编写监控文件变化启动同步的脚本rstnc-inotify.sh 53、52需要安装 rsync-3.1.3.tar.gz wget https://download.samba.org/pub/rsync/src/rs tar zxvf rsync-3.1.2.tar.gz cd rsync-3.1.2 ./configure --prefix=/alidata/server/rsync make make install chmod +x ./rsync-inotify.sh #赋予执行权限 ./rsync-inotify.sh & #后台执行<br> echo "/alidata/netho/rsync-inotify.sh &">>/etc/rc.local 为防止脚本出现问题设置定时方案 crontab -e * */2 * * * rsync -avz --password-file=你的路径 /同步路径/ user@ip::方括号里的名称 正式配置: uid = root gid = root use chroot = no max connections = 200 strict modes = yes hosts allow = 10.245.6.54/24 #配置主机ip可以空格,允许多个 #hosts deny = 0.0.0.0/32 #禁止IP,可设置多个,用英文逗号隔开 port = 873 motd file = /eshop/rsync/rsyncd.motd pid file = /eshop/rsync/rsyncd.pid lock file = /eshop/rsync/rsync.lock log file = /eshop/rsync/rsyncd.log [eshopres] #eshop为模块名称后续同步会用到 path = /eshop/eshopres comment=eshopres #模块名称与[eshop]自定义名称相同 ignore errors #read only = false #此目录需要读写全部权限 read only = no list = yes auth users = backup secrets file = /eshop/rsync/rsyncd.passwd ignore errors = no transfer logging = yes log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. 开放端口的方法: 方法一:命令行方式 1. 开放端口命令: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT 2.保存:/etc/rc.d/init.d/iptables save 3.重启服务:/etc/init.d/iptables restart 4.查看端口是否开放:/sbin/iptables -L -n 方法二:直接编辑/etc/sysconfig/iptables文件 1.编辑/etc/sysconfig/iptables文件:vi /etc/sysconfig/iptables 加入内容并保存:-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT 2.重启服务:/etc/init.d/iptables restart 3.查看端口是否开放:/sbin/iptables -L -n 但是我用方法一一直保存不上,查阅网上发现直接修改文件不需要iptables save,重启下iptables 重新加载下配置。iptables save 是将当前的iptables写入到/etc/sysconfig/iptables。我不save直接restart也不行,所以还是方法二吧 查询端口是否有进程守护用如下命令grep对应端口,如80为端口号 例:netstat -nalp|grep 80