Rsync+Serync多主多从文件同步
环境:Centos 6.9
软件:Rsync+Sersync
要求:三台服务器能够形成多主多从文件架构,任意一台服务器文件变更都会同步到其它服务器
配置:三台服务器:
A:192.168.1.2
B:192.168.1.3
C:192.168.1.4
同步目录/text
1.安装rsync
#yum安装rsync
yum install -y rsync
#关闭selinux(可选)
setenforce 0
2.配置rsync
vim /etc/rsyncd.conf
motd file = /etc/rsyncd.motd
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
log format = %t %a %m %f %b
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
timeout = 300
[text] #模块名称,很重要;
path=/text #同步目录;
list=yes
comment = this is comment
ignore errors = yes
read only = no
hosts allow = 192.168.1.0/24 ~~#很重要,必须同一个地址段,本人用的阿里云内网IP地址,外网地址未试验;~~
auth users = backup ~~#也很重要,相当于rsync的用户名,一定记得~~
secrets file = /etc/rsync.pas #名字随便起,什么rsync.password,rsyncd.pas等等,但一定要记住,很重要
3.配置rsync.pas
鲜血的教训啊
rsync主动端和被动端rsync.pas格式要求不同
主动端实例“123456”
被动端示例“backup:123456”
所以考虑到多主多从直接融合两种配置文件
vim /etc/rsync.pas #一定和前面配置的secrets file的文件名完全相同
123456
backup:123456
保存退出,然后记得linux绝大多数密匙文件要求权限为600
chmod 600 /etc/rsync.pas
配置其它两台服务器和这台服务器配置完全相同,尤其记得密匙文件名称和权限
4.启动rsync
#启动rsync以daemon模式运行
rsync --daemon --config /etc/rsyncd.conf
#检查端口启动情况
netstat -antp|grep rsync
#rsync默认端口是873,云服务器记得开放端口,自有服务器iptables配置请自行百度
5.测试rsync
这一步很重要,因为sersync基于rsync同步,如果这一步成功基本就完成99%,否则检查配置文件,尤其记得密匙600权限和配置文件的
secrets file选项,还有记得提前创建文件夹。(PS:千万不要作死用现成的,可能会丢失数据!!!!)
rsync -avz --delete /text/ backup@192.168.1.3::tongbu --password-file=/etc/rsync.pas
#/text/ 同步目录
#backup 用户名
#192.168.1.3 目标服务器IP地址
#tongbu 模块名称
#--password-file=/etc/rsync.pas 密匙文件
三台服务器互相测试保证完全成功,自行创建些测试文件同步
如果全部成功那么恭喜你完成了90%
6.安装sersync
#下载安装包
yum install git -y
#github下载
git clone https://github.com/JohnsonCydia/sersync.git
#移动压缩包到/usr/local
mv sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz /usr/local/
#进入/usr/local/
cd / usr/local/
#解压缩
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
#重命名(方便后期启动,非必须步骤)
mv GNU-Linux-x86/ sersync
7.配置sersync/confxml.xml
#备份原来配置文件
mv /usr/local/sersync/confxml.xml /usr/local/sersync/confxml.xml.bak
#写入新配置文件
vim /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"> ~~#文件过滤配置,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="/phpstudy/www">
<!--<remote <remote ip="127.0.0.1" name="tongbu"/>--> ~~#这样为不启用~~
<remote ip="192.168.1.2" name="tongbu"/> ~~# <remote ip="服务器IP地址" name="模块名称"/>~~
<remote ip="192.168.1.3" name="tongbu"/> ~~# 有几台服务器填写几个~~
</localpath>
<rsync>
<commonParams params="-artuz"/> ~~#rsync连接配置模块~~
<auth start="true" users="backup" passwordfile="/etc/rsync.pas"/> #users:rsync用户名 passwordfile=/etc/rsync.pas 指定密匙文件,绝对路径
<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="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>
8.配置检查三个服务器配置文件并启动
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
启动成功的界面
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /ll/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is backup
passwordfile is /etc/rsync.pas
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) + 20(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /phpstudy/www && rsync -artuz -R --delete ./ backup@172.18.160.135::tongbu --password-file=/etc/rsync.pas >/dev/null 2>&1
PS:如果启动出现问题请检查配置文件和密匙文件,记得三台服务器IP地址设置正确且都正确,进行测试
9.创建守护脚本
#!/bin/bash
server_file="/usr/local/sersync/sersync2"
conf_file="/usr/local/sersync/confxml.xml"
options="-d -r -o"
proc_num=$(ps -ef|grep -i sersync2|grep -v "grep"|wc -l)
if [ $proc_num -lt 1 ];then
cd $(dirname $server_file)
nohup $server_file $options $conf_file &
else
exit 0;
fi
*/5 * * * * /bin/sh /sh/sersync.sh /dev/null 2>&1
请自行crontab -e添加进入自动执行任务
完成,时间仓促有不对的地方欢迎私信指正,欢迎转载,出处不出处就算了,毕竟也参考很多大神的意见,只是做归纳整理