【需求】
WEB服务器实现实时同步,切换机制暂不涉及
IP_A作为主服务器
IP_B作为备份服务器
当IP_A挂了以后,实时切换至IP_B,需要保证IP_A的数据实时同步至IP_B
【环境】
Distributor ID: Ubuntu
Description: Ubuntu 14.04.3 LTS
Release: 14.04
Codename: trusty
【实现方案】
inotify+rsync
具体实现:
Rsync配置:
Ubuntu 14.04已经自带rsync,如果没有直接安装即可
apt-get install rsync
step 1:默认启动项
root@ubuntu2:/etc# cat /etc/default/rsync
# defaults file for rsync daemon mode
# start rsync in daemon mode from init.d script?
# only allowed values are "true", "false", and "inetd"
# Use "inetd" if you want to start the rsyncd from inetd,
# all this does is prevent the init.d script from printing a message
# about not starting rsyncd (you still need to modify inetd's config yourself).
RSYNC_ENABLE=true
#默认为False改成true
step2 :修改配置文件
默认情况下/etc下没有rsync的配置文件,需要拷贝一份示例文件过来进行配置
sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
# sample rsyncd.conf configuration file
# GLOBAL OPTIONS
#motd file=/etc/motd
log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
# pid file=/var/run/rsyncd.pid
#syslog facility=daemon
#socket options=
# MODULE OPTIONS
#模块名称,后续同步文件会用到
[ftp]
comment = public archive
path = /opt/data/
#需要同步的文件目录/保留
use chroot = yes
#是否可以切换到root目录
# max connections=10
lock file = /var/lock/rsyncd
# the default for read only is yes...
read only = no
#是否只读
list = yes
uid = root
gid = root
# exclude =
# exclude from =
# include =
# include from =
# auth users =
secrets file = /etc/rsync.password
#秘钥 test:123456
strict modes = yes
# hosts allow =
# hosts deny =
ignore errors = no
ignore nonreadable = yes
transfer logging = no
step 3 :创建/etc/rsync.password密码文件
echo "test:123456" > /etc/rsync.password
#写文件
chmod 600 /etc/rsync.password
#更改权限
step 4 :启动 rsync
rsync --daemon
查看是否开启
data# netstat -ntlp | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27350/rsync
tcp6 0 0 :::873 :::* LISTEN 27350/rsync
防火墙打开873端口
iptables -I INPUT -p tcp -m tcp --dport 873 -j ACCEPT
iptables -I INPUT -p udp -m udp --dport 873 -j ACCEPT
注意:因为IP_A和IP_B可以互换,所以上面的配置两个设备都配置下
测试配置是否正确
root@ubuntu2:/etc# rsync -avpz --delete /opt/data/ root@192.168.3.8::ftp
sending incremental file list
sent 4,859 bytes received 62 bytes 9,842.00 bytes/sec
total size is 64,462,605 speedup is 13,099.49
以上Rsync的配置和开启方式已完成,且可以手动同步文件,下面配置Inotify实时监控文件节点改变并同步文件
Inotify 配置
Step 1 : 安装
apt-get install inotify-tools
Step 2:编写监控脚本
#!/bin/bash
#param
host=192.168.3.8
src=/opt/data/
dst_module_config=ftp_config
dst_module=ftp
user=root
rsync_passwd=/etc/rsyncd.passwmrd
cd ${src}
/usr/bin/inotifywait -mrq --format '%Xe %w%f' --timefmt '%d/%m/%y %H:%M' -e modify,delete,create,attrib ./ | while read file
do
INO_EVENT=$(echo $file | awk '{print $1}') # 把inotify输出切割 把事件类型部分赋值给INO_EVENT
INO_FILE=$(echo $file | awk '{print $2}') # 把inotify输出切割 把文件路径部分赋值给INO_FILE
echo "-------------------------------$(date)------------------------------------"
echo $file
echo $INO_FILE
echo $INO_EVENT
#增加、修改、写入完成、移动进事件
echo "${file} was rsyncd" >>/var/log/rsyncd.log 2>&1
#增、改放在同一个判断,因为他们都肯定是针对文件的操作,即使是新建目录,要同步的也只是一个空目录,不会影响速度。
if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]] # 判断事件类型
then
echo 'CREATE or MODIFY or CLOSE_WRITE or MOVED_TO'
/usr/bin/rsync -avzR --delete --progress $(dirname ${INO_FILE}) ${user}@${host}::${dst_module} --password-file=${rsync_passwd} 2>&1 & >/dev/null
/usr/bin/rsync -avzR --delete --progress ${src_config} ${user}@${host}::${dst_module_config} --password-file=${rsync_passwd} 2>&1 & >/dev/null
fi
if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]
then
echo 'DELETE or MOVED_FROM'
/usr/bin/rsync -avzR --delete --progress $(dirname ${INO_FILE}) ${user}@${host}::${dst_module} --password-file=${rsync_passwd} 2>&1 & >/dev/null
/usr/bin/rsync -avzR --delete --progress ${src_config} ${user}@${host}::${dst_module_config} --password-file=${rsync_passwd} 2>&1 & >/dev/null
fi
if [[ $INO_EVENT =~ 'ATTRIB' ]]
then
echo 'ATTRIB'
if [ ! -d "$INO_FILE" ]
then
echo /usr/bin/rsync -avzcR --delete --progress $(dirname ${INO_FILE}) ${user}@${host}::${dst_module} --password-file=${rsync_passwd} 2>&1 & >/dev/null
/usr/bin/rsync -avzR --delete --progress $(dirname ${INO_FILE}) ${user}@${host}::${dst_module} --password-file=${rsync_passwd} 2>&1 & >/dev/null
/usr/bin/rsync -avzR --delete --progress ${src_config} ${user}@${host}::${dst_module_config} --password-file=${rsync_passwd} 2>&1 & >/dev/null
fi
fi
done
step 3 : 运行监控脚本
root@ubuntu2:~# chmod +x inotify_monitor.sh
root@ubuntu2:~# nohup sh inotify_monitor.sh & >/dev/null
root@ubuntu2:~# echo "/root/data/inotify_monitor.sh &" >> /etc/rc.local
#加入开机自启动
测试是否成功
root@ubuntu2:~# touch /opt/data/test
root@ubuntu2:~# cat /var/log/rsyncd.log
#查看日志及对端目录是否有test文件