一、Sersync安装
(1)Sersync同步需求逻辑图
Sersync依赖于rsync进行数据同步,需在主服务器master上开启Sersync,Sersync负责监控配置文件
中的文件系统的事件变化,然后调用rsync命令把更新的文件同步到目标服务器slave上。
结论:需要在主服务器上配置Sersync,在同步目标服务器上安装rsync-server。
Sersync同步示意图
user----}Sersync--------}S1-rsync服务器
服务器--------}S2-rsync服务器
用户user会实时的往Sersync服务器写入或更新数据文件,此时,需要在左边的同步服务器上配置Sersync,在右边的同步目标服务器S1和S2上配置rsync守护进程服务,这样在主服务器写入或更新数据文件时,就会被
Sersync实时同步到slave服务器S1和S2。
结论: Sersync就是监控本地的数据写入或更新事件,然后调用rsync客户端命令,将写入或更新的文件实时同步到slave服务器S1和S2上。
(2)检查版本---安装--升级版本 yum -y install rsync 也可编译安装
(3)在目录服务器slave服务器S1和S2上配置。
################配置客户端S1-S2######################
[[email protected] ~]# cat /etc/rsyncd.conf
#rsync_config_______________start
#create by tangbo 2015-12-02 13:25
#QQ7313760
#########rsyncd.conf______start#########
uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
#write only = no
hosts allow = 192.168.11.0/24
hosts deny = 0.0.0.0/32
list = false
auth users = rsync_backup
secrets file = /etc/rsync.password
#####################################
[www]
path=/data/www/www/
comment = www by tangbo 13:40 2015-11-20
#####################################
[bbs]
path=/data/www/bbs/
comment = www by tangbo 13:40 2015-11-20
#####################################
[blog]
path=/data/www/blog/
comment = www by tangbo 13:40 2015-11-20
###################################
#########rsyncd.conf______end#########
[[email protected] ~]#
上面的rsync服务的配置文件,允许Sersync主服务器访问,rsync同步的模块名为[www] [bbs][blog]
将同步过来的文件分别放入对应path指定的目录/data/www/www/ /data/www/bbs/ /data/www/blog/下,如果有多台目标服务器,则每台都需要类似配置,uid,gid可根据实际情况变化。该用户必须对这三个目录有写入权限。
(4)创建相关目录:
[[email protected] ~]# mkdir -p /data/www/www/ /data/www/bbs/ /data/www/blog/
[[email protected] ~]# ls /data/www/
bbs blog www
[[email protected] ~]#
权限相关配置:
[[email protected] ~]# vim /etc/rsync.password
rsync_backup:tangbo (这个用户和密码需要去创建)
[[email protected] ~]# chmod 600 /etc/rsync.password
检查:
[[email protected] ~]# cat /etc/rsync.password
rsync_backup:tangbo
[[email protected] ~]# ll /etc/rsync.password
-rw-------. 1 root root 20 Dec 8 21:54 /etc/rsync.password
[[email protected] ~]#
配置完成后,以守护进程方式启动rsync服务。
[[email protected] ~]# pkill rsync
[[email protected] ~]# rsync --daemon
[[email protected] ~]# netstat -tlnp | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 888/rsync
tcp 0 0 :::873 :::* LISTEN 888/rsync
[[email protected] ~]#
(5)设置开机自启动
[[email protected] ~]# echo "#rsync service daemon by tangbo 20151207" >>/etc/rc.local
[[email protected] ~]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local
[[email protected] ~]# grep daemon /etc/rc.local
#rsync service daemon by tangbo 20151207
/usr/bin/rsync --daemon
[[email protected] ~]#
重启命令:
[[email protected] ~]# pkill rsync
[[email protected] ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 22616/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1258/master
tcp 0 0 :::22 :::* LISTEN 22616/sshd
tcp 0 0 ::1:25 :::* LISTEN 1258/master
tcp 0 0 :::3306 :::* LISTEN 5210/mysqld
[[email protected] ~]# /usr/bin/rsync --daemon
自己可以写一个正常的rsync启动脚本。
三、Master端安装rsync客户端并且测试 yum -y install rsync 也可编译安装
(1)在Master上配置相关权限认证
[[email protected] ~]# echo "tangbo" >/etc/rsysn.password
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
tangbo
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Dec 8 22:09 /etc/rsync.password
[[email protected] ~]#
启动服务:
[[email protected] ~]# touch /etc/rsyncd.conf
[[email protected] ~]# rsync --daemon
[[email protected] ~]# netstat -tlnp
(2)master 上手工测试同步情况:此步骤非常关键,一定要测试。如果测试同步不成功,后面的sersync配置后也不会同步数据。
[[email protected] ~]# mkdir -p /data/www/www/ /data/www/bbs/ /data/www/blog/
[[email protected] ~]# touch /data/www/www/www.log /data/www/bbs/bbs.log /data/www/blog/blog.log
(3)执行同步命令
[[email protected] /]# rsync -avzP /data/www/www/ [email protected]::www/ --password-file=/etc/rsync.password
[[email protected] /]# rsync -avzP /data/www/www/ [email protected]::www/ --password-file=/etc/rsync.password
[[email protected] /]# rsync -avzP /data/www/bbs/ [email protected]::bbs/ --password-file=/etc/rsync.password
[[email protected] /]# rsync -avzP /data/www/bbs/ [email protected]::bbs/ --password-file=/etc/rsync.password
[[email protected] /]# rsync -avzP /data/www/blog/ [email protected]::blog/ --password-file=/etc/rsync.password
[[email protected] /]# rsync -avzP /data/www/blog/ [email protected]::blog/ --password-file=/etc/rsync.password
上述目录都可以同步成功则可以实现下一步的工作,安装sersync
四、安装Sersync---单实例
(1)下载源码且解压到/usr/local/下
[[email protected] tools]# tar -zxvf sersync_64bit_binary_stable_final.tar.gz -C /usr/local/
重命名GNU-Linux-x86/ 为sersync
[[email protected] local]# mv GNU-Linux-x86/ sersync
[[email protected] local]# ls -R sersync/
sersync/:
confxml.xml sersync2
[[email protected] local]#
(2)规范sersync目录结构
[[email protected] local]# cd sersync/
[[email protected] sersync]# mkdir -p conf bin logs
[[email protected] sersync]# mv confxml.xml conf
[[email protected] sersync]# mv sersync2 bin
[[email protected] local]# ls -R sersync/
sersync/:
bin conf logs
sersync/bin:
sersync2
sersync/conf:
confxml.xml
sersync/logs:
[[email protected] local]#
(3)配置sersync
首先备份配置文件: [[email protected] conf]# cp confxml.xml confxml.xml.backup.$(date +%F)
(4)更改优化sersync配置
修改第24行
[[email protected] conf]# vim +24 confxml.xm
<sersync>
<localpath watch="/data/www/www">
<remote ip="192.168.11.34" name="www"/>
<remote ip="192.168.11.35" name="www"/>
<localpath watch="/data/www/bbs"> 只建立一个目录,三个目录要修改配置文件
<remote ip="192.168.11.34" name="bbs"/>
<remote ip="192.168.11.35" name="bbs"/>
<localpath watch="/data/www/blog">
<remote ip="192.168.11.34" name="blog"/>
<remote ip="192.168.11.35" name="blog"/>
提示:此步骤<localpath watch="/data/www/blog">就是定义服务端待同步的目录和目标服务器的模块name="blog"名。
(5)修改第34行
<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 /data/www/www/ [email protected]::www/ --password-file=/etc/rsync.password这个同步命令而已。
(6)修改第37行--当同步失败后,错误日志存放路径。并且没60分钟对失败的log进行重新同步。
<failLog path="//usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
#############开启sersync守护进程同步数据######################
(7)配置sersync 环境变量
[[email protected] conf]# echo ‘export PATH=$PATH:/usr/localsersync/bin‘>>/etc/profile
[[email protected] conf]# tail -l /etc/pro
profile profile.d/ protocols
[[email protected] conf]# tail -l /etc/profile
. "$i"
else
. "$i" >/dev/null 2>&1
fi
fi
done
unset i
unset -f pathmunge
export PATH=$PATH:/usr/local/sersync/bin
[[email protected] conf]# source /etc/profile
(8)测试结果:发现问题,仅仅能同步一个模块的路径,其它模块的路径不能同步。
五、多实例
(1)复制配置文件
[[email protected] conf]# cp confxml.xml www_confxml.xml
[[email protected] conf]# cp confxml.xml bbs_confxml.xml
[[email protected] conf]# cp confxml.xml blog_confxml.xml
(2)修改配置文件
[[email protected] conf]# diff www_confxml.xml bbs_confxml.xml
24,26c24,26
< <localpath watch="/data/www/www">
< <remote ip="192.168.11.34" name="www"/>
< <remote ip="192.168.11.35" name="www"/>
---
> <localpath watch="/data/www/bbs">
> <remote ip="192.168.11.34" name="bbs"/>
> <remote ip="192.168.11.35" name="bbs"/>
37c37
< <failLog path="/usr/local/sersync/logs/www_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
---
> <failLog path="/usr/local/sersync/logs/bbs_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
[[email protected] conf]#
[[email protected] conf]# diff www_confxml.xml blog_confxml.xml
24,26c24,26
< <localpath watch="/data/www/www">
< <remote ip="192.168.11.34" name="www"/>
< <remote ip="192.168.11.35" name="www"/>
---
> <localpath watch="/data/www/blog">
> <remote ip="192.168.11.34" name="blog"/>
> <remote ip="192.168.11.35" name="blog"/>
37c37
< <failLog path="/usr/local/sersync/logs/www_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
---
> <failLog path="/usr/local/sersync/logs/blog_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
[[email protected] conf]#
(3)多实例初始化同步命令:
sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml
(4)检查配置
[[email protected] conf]# ps -ef | grep sersync
root 18118 1 0 20:51 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
root 18141 1 0 20:51 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml
root 18165 1 0 20:51 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml
root 18187 29986 0 20:51 pts/0 00:00:00 grep sersync
root 32204 29986 0 01:49 pts/0 00:00:00 vim sersync2
[[email protected] conf]#
(5)到测试目录下面去测试是否同步成功,同时建立一千个文件
[[email protected] tmp]# cat for.sh
#!/bin/bash
for i in `seq 1 1000`
do
touch /data/www/www/$i
touch /data/www/bbs/$i
touch /data/www/blog/$i
done
(6)加入开机自启动
[[email protected] tmp]# tail -4 /etc/rc.local
tail: inotify cannot be used, reverting to polling
#sersync boot
sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml
[[email protected] tmp]#