rsync+inotify组合实现linux服务器间数据的实时同步
第一种:
服务器端
yum install rsync xinetd -y
#配置xinetd:
vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no
启动xinetd服务:
service xinetd start
chkconfig xinetd on
useradd webuser
passwd webuser
"web_pass_123"
echo "web_pass_123" >/etc/rsync.passwd
chmod 600 /etc/rsync.passwd
cd /usr/src/
wget http://cloud.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=/usr/local/inotify
make
make install
cd /home/webuser
vi rsync.sh
---
#!/bin/bash
src=/home/tomcat7/webapps/
des=webapp
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src webuser@10.3.32.81::$des
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src webuser@10.3.32.82::$des
#/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src webuser@10.3.32.79::$des
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src webuser@10.3.32.80::$des
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src webuser@10.3.32.77::$des
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src webuser@10.3.32.78::$des
echo "${files} was rsynced" >>/home/webuser/rsync.log 2>&1
done
---
chmod 764 rsync.sh
加入到开机启动项里
echo "/home/webuser/rsync.sh" >> /etc/rc.local
#./rsync.sh
这样启动的inotify, 无法在关闭shell命令行之后在后台运行。如果需要在后台运行
可以使用#nohup ./rsync.sh.sh &
81:客户端
yum install rsync xinetd -y
#配置xinetd:
vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no
启动xinetd服务:
service xinetd start
chkconfig xinetd on
useradd webuser
passwd webuser
"web_pass_123"
echo "webuser:web_pass_123" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
vi /etc/rsyncd.conf
---
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[webapp]
path = /home/tomcat7/webapps/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 10.3.32.75
hosts deny = *
list = false
uid = root
gid = root
auth users = webuser
secrets file = /etc/rsync.passwd
---
service xinetd restart
--------------------------------------------------------------------------------------
参考资料
假设有两台文件服务器
A:192.168.1.100
B:192.168.1.101
要实现文件的实时双向同步可以采用rsync+inotify组合,rsync可以实现文件同步,但是它没有办法实时触发,
所以就需要搭配inotifi实时触发。
第一步:在两台服务器上面都安装rsync和inotify
Fedora、Redhat 、centos等在线安装方法;
# yum install rsync
一部搞定rsync的安装
inotify的安装方法如下:
1.下载inotify的源码,例如下载的文件名为inotify.tar.gz
2.解压inotify
#tar -zxvf inotify.tar.gz
进入到解压的文件夹里面
#cd inotify
编译并安装
#./configure --prefix=/usr/local/inotify
#make && make install
inotify的安装目录是/usr/local/inotify
第二步配置每个服务器的rsync和inotify
在/etc目录下面新建文件rsyncd.conf,内容如下
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.100
uid = root
gid = root
use chroot = yes
read only = no
hosts allow=192.168.1.101
hosts deny=*
max connections = 5
motd file = /etc/rsyncd.motd
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[www]
path = /backup
list=no
ignore errors
comment = data backup
auth users = root
secrets file = /etc/rsyncd.secrets
配置说明:
配置文件里面不能包含#注释
address表示本机ip
hosts allow 表示允许访问的ip,我这里配置的是服务器B
secrets file 表示验证文件
[www]里面的www是模块名称,可以自己定义
path是要同步的路径
创建验证文件
在/etc下面创建 名称为rsyncd.secrets的验证文件
格式为 用户名:密码,例如 testuser:pwdtest
此用户名和密码和系统没有关系,是给rsync设置的用户名和密码
设置完了之后需要给rsyncd.secrets设置权限,如下
#chmod 600 /etc/rsyncd.secrets
接下来就是配置inotify,实时出发文件同步,在inotify的安装目录下面新建脚步文件inotify_back.sh,内容如下
#!/bin/bash
src=/backup/
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read file
do
/usr/bin/rsync -auvrtzopgP --progress --password-file=/etc/rsyncd.pwd /backup/testuser@192.168.1.101::www
echo " ${file} was rsynced" >>/usr/log/rsync.log 2>&1
done
配置说明
/etc/rsyncd.pwd 是B服务器里面rsync配置的密码
testuser@192.168.1.101::www 里面的testuser是rsync配置的用户名,后面的ip是B服务器的ip,www是配置要同步的模块名称
配置文件创建完成之后还需要授权
#chmod +xinotify_back.sh
两天服务器的配置方法相同,只需要修改对应的参数即可
最后启动rsync和inotify
#rsync --daemon --config=/etc/rsyncd.conf
然后进入到inotify_back.sh所在的目录启动inotify
#./inotify_back.sh
这样启动的inotify, 无法在关闭shell命令行之后在后台运行。如果需要在后台运行
可以使用#nohup ./inotify_back.sh &
通过以上方式配置并启动两台服务器之后,就可以完美的实现文件的实时双向同步了