部署rsync+sersync实现多机数据实时同步

简介:

源服务器:192.168.21.129


目标服务器:192.168.21.127,192.168.21.128


目的:把源服务器上/home/www.osyunwei.com目录实时同步到目标服务器的/home/www.osyunwei.com下


系统运维  www.osyunwei.com  温馨提醒:qihang01原创内容 版权所有,转载请注明出处及原文链接


具体操作:


第一部分:分别在两台目标服务器192.168.21.127,192.168.21.128上操作


一、分别在两台目标服务器安装rsync服务端


1、关闭SELINUX


#编辑防火墙配置文件

vi /etc/selinux/config 


#SELINUX=enforcing #注释掉


#SELINUXTYPE=targeted #注释掉


SELINUX=disabled #增加


:wq! #保存,退出


setenforce 0 #立即生效


2、开启防火墙tcp 873端口(Rsync默认端口)


vi /etc/sysconfig/iptables #编辑防火墙配置文件


-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT


:wq! #保存退出


/etc/init.d/iptables restart #最后重启防火墙使配置生效


3、安装rsync服务端软件


yum install rsync xinetd #安装


vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync


disable = no #修改为no


:wq! #保存退出


/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)


4、创建rsyncd.conf配置文件


vi /etc/rsyncd.conf #创建配置文件,添加以下代码


log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建


pidfile = /var/run/rsyncd.pid  #pid文件的存放位置


lock file = /var/run/rsync.lock  #支持max connections参数的锁文件


secrets file = /etc/rsync.pass  #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件


motd file = /etc/rsyncd.motd  #rsync启动时欢迎信息页面文件位置(文件内容自定义)


[home_www.osyunwei.com] #自定义名称


path = /home/www.osyunwei.com/ #rsync服务端数据目录路径


comment = home_www.osyunwei.com #模块名称与[home_www.osyunwei.com]自定义名称相同


uid = root #设置rsync运行权限为root


gid = root #设置rsync运行权限为root


port=873  #默认端口


use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份


read only = no  #设置rsync服务端文件为读写权限


list = no #不显示rsync服务端资源列表


max connections = 200 #最大连接数


timeout = 600  #设置超时时间


auth users = home_www.osyunwei.com_user #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开


hosts allow = 192.168.21.129  #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开


hosts deny = 192.168.21.254 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开


:wq!  #保存,退出


5、创建用户认证文件


vi /etc/rsync.pass #配置文件,添加以下内容


home_www.osyunwei.com_user:123456  #格式,用户名:密码,可以设置多个,每行一个用户名:密码


:wq!  #保存退出


6、设置文件权限



chmod 600 /etc/rsync.pass  #设置文件所有者读取、写入权限


7、启动rsync


/etc/init.d/xinetd start  #启动


service xinetd stop   #停止


service xinetd restart  #重新启动


8、创建[home_www.osyunwei.com]模块中path变量提到的目录


mkdir /home/www.osyunwei.com/



第二部分:在数据源服务器192.168.21.129上操作


一、安装Rsync客户端


1、关闭SELINUX


vi /etc/selinux/config  #编辑防火墙配置文件


#SELINUX=enforcing  #注释掉


#SELINUXTYPE=targeted  #注释掉


SELINUX=disabled  #增加


:wq!  #保存退出


setenforce 0   #立即生效


2、开启防火墙tcp 873端口(Rsync默认端口,做为客户端的Rsync可以不用开启873端口)


vi /etc/sysconfig/iptables  #编辑防火墙配置文件


-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT


:wq! #保存退出


/etc/init.d/iptables restart #最后重启防火墙使配置生效


3、安装Rsync客户端端软件


whereis rsync   #查看系统是否已安装rsync,出现下面的提示,说明已经安装


rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz


yum install  xinetd  #只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的


yum install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd


vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync


disable = no #修改为no


/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理rsync服务的)


4、创建认证密码文件


vi /etc/passwd.txt  #编辑文件,添加以下内容


123456 #密码


:wq! #保存退出


chmod 600 /etc/passwd.txt  #设置文件权限,只设置文件所有者具有读取、写入权限即可


5、测试源服务器192.168.21.129到两台目标服务器192.168.21.127,192.168.21.128之间的数据同步


mkdir /home/www.osyunwei.com/ceshi  #在源服务器上创建测试文件夹,然后在源服务器运行下面2行命令


rsync -avH --port=873 --progress --delete  /home/www.osyunwei.com/  home_www.osyunwei.com_user@192.168.21.127::home_www.osyunwei.com --password-file=/etc/passwd.txt


rsync -avH --port=873 --progress --delete  /home/www.osyunwei.com/  home_www.osyunwei.com_user@192.168.21.128::home_www.osyunwei.com --password-file=/etc/passwd.txt


运行完成后,分别在两台目标服务器192.168.21.127,192.168.21.128上查看,在/home/www.osyunwei.com目录下有ceshi文件夹,说明数据同步成功。


系统运维  www.osyunwei.com  温馨提醒:qihang01原创内容 版权所有,转载请注明出处及原文链接



二、安装sersync工具,实时触发rsync进行同步


1、查看服务器内核是否支持inotify


ll /proc/sys/fs/inotify   #列出文件目录,出现下面的内容,说明服务器内核支持inotify


-rw-r--r-- 1 root root 0 Mar  7 02:17 max_queued_events


-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_instances


-rw-r--r-- 1 root root 0 Mar  7 02:17 max_user_watches


备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核


CentOS 5.X 内核为2.6.18,默认已经支持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! #保存退出


参数说明:


max_queued_events:


inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确


max_user_watches:


要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)


max_user_instances:


每个用户创建inotify实例最大值


3、安装sersync


sersync下载地址:

①https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

②http://xz5.jb51.net:81/201111/tools/sersync_64bit_binary_stable_final.tar.gz


wget sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目录下


cd /usr/local/src


tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  #解压


mv GNU-Linux-x86  /usr/local/sersync  #移动目录到/usr/local/sersync


4、配置sersync


cd  /usr/local/sersync #进入sersync安装目录


cp confxml.xml confxml.xml-bak  #备份原文件


vi confxml.xml  #编辑,修改下面的代码


<?xml version="1.0" encoding="ISO-8859-1"?>

...

...


<sersync>


<localpath watch="/home/www.osyunwei.com">


<remote ip="192.168.21.127" name="home_www.osyunwei.com"/>


<remote ip="192.168.21.128" name="home_www.osyunwei.com"/>


<!--<remote ip="192.168.8.40" name="tongbu"/>-->


</localpath>


<rsync>


<commonParams params="-artuz"/>


<auth start="true" users="home_www.osyunwei.com_user" passwordfile="/etc/passwd.txt"/>


<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>


...

...



:wq!  #保存退出


参数说明:


localpath watch="/home/www.osyunwei.com":#源服务器同步目录


192.168.21.127,192.168.21.128:#目标服务器IP地址


name="home_www.osyunwei.com": #目标服务器rsync同步目录模块名称


users="home_www.osyunwei.com_user": #目标服务器rsync同步用户名


passwordfile="/etc/passwd.txt": #目标服务器rsync同步用户的密码在源服务器的存放路径


remote ip="192.168.21.127":  #目标服务器ip,每行一个


remote ip="192.168.21.128":  #目标服务器ip,每行一个


failLog path="/tmp/rsync_fail_log.sh"  #脚本运行失败日志记录


start="true"  #设置为true,每隔600分钟执行一次全盘同步


启动服务

/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml 



5、设置sersync监控开机自动执行


vi /etc/rc.d/rc.local  #编辑,在最后添加一行


/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #设置开机自动运行脚本


:wq!  #保存退出


6、用脚本定时监控sersync是否正常运行


vi  /home/crontab/check_sersync.sh  #编辑,添加以下代码


#!/bin/sh


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


:wq!  #保存退出


chmod +x /home/crontab/check_sersync.sh #添加脚本执行权限


vi /etc/crontab #编辑,在最后添加下面一行


*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1  #每隔5分钟执行一次脚本


service crond reload  #重新加载服务


6、测试sersync实时触发rsync同步脚本是否正常运行


在源服务器192.168.21.129上创建文件inotify_rsync_ceshi


mkdir /home/www.osyunwei.com/inotify_rsync_ceshi


重新启动源服务器:192.168.21.129


等系统启动之后,查看两台目标服务器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi文件夹


然后再在源服务器192.168.21.129创建文件夹inotify_rsync_ceshi_new


mkdir /home/www.osyunwei.com/inotify_rsync_ceshi_new


继续查看两台目标服务器192.168.21.127,192.168.21.128的/home/www.osyunwei.com下是否有inotify_rsync_ceshi_new文件夹


如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。




本文转自 朱科强 51CTO博客,原文链接:http://blog.51cto.com/zhukeqiang/1903624,如需转载请自行联系原作者

相关文章
|
12月前
|
安全 Unix Linux
【Linux网络服务】Rsync数据同步
【Linux网络服务】Rsync数据同步
|
存储 监控 安全
|
监控 网络协议 网络安全
rynsc +sersync实时数据同步
转载:https://www.cnblogs.com/liangml/p/5959571.html 一.为什么要用Rsync+sersync架构 1.sersync是基于Inotify开发的,类似于Inotify-tools的工具 2.sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。
1286 0
|
算法 数据安全/隐私保护 Linux
|
数据安全/隐私保护 存储 安全
|
监控 Shell 网络安全
|
监控 网络安全 数据安全/隐私保护
|
网络协议 Shell 应用服务中间件
|
监控 数据安全/隐私保护 Shell