假设有以下需求:linux
假设两个服务器:bash
192.168.0.1 源服务器 有目录 /opt/test/服务器
192.168.0.2 目标服务器 有目录 /opt/bak/test/工具
实现的目的就是保持这两个服务器某个文件目录保持实时同步测试
实现方式: 经过rsync+inotify-tools结合来实现spa
首先要给两台机器添加信任关系,具体方法已经在前面的文章介绍过了操作系统
详情查看: linux添加信任关系免密码登陆日志
须要安装软件:code
在 源服务器 和 目标服务器 都须要安装orm
源服务器: 是rsync客户端,不须要配置
目标服务器: 是rsync服务器端,须要配置/etc/rsyncd.conf里的内容
该工具为文件实时监控工具,须要linux操做系统内核支持,内核支持须要至少版本为2.6.13
检查操做系统是否支持,执行以下:
uname -r 查看版本
返回:
1
|
2.6.32-358.6.1.el6.x86_64
|
则表示版本2.6.32 大于2.6.13,则支持。
执行:
1
2
3
4
5
|
ll
/proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Oct 18 12:18 max_queued_events
-rw-r--r-- 1 root root 0 Oct 18 12:18 max_user_instances
-rw-r--r-- 1 root root 0 Oct 18 12:18 max_user_watches
|
有三项输出,则表示默认支持inotify,能够安装inotify-tools工具.
若是不支持,须要采用新版本的linux操做系统
版本达到要求,就能够安装了。
安装inotify-tools后会在相关安装目录下生成以下两个文件:
1
2
3
4
|
ll
/usr/local/bin/
total 88
-rwxr-xr-x 1 root root 44327 Oct 10 15:32 inotifywait
-rwxr-xr-x 1 root root 41417 Oct 10 15:32 inotifywatch
|
则表示安装成功。
注意: 在 源服务器上须要安装,目标服务器上不须要安装inotify。
在源服务器上新建脚本:
inotify_bak.sh
1
2
3
4
5
6
7
|
#!/bin/bash
src=
/opt/test/
/usr/local/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
-arzuq $src 192.168.0.1::www/
echo
" ${file} was rsynced"
>>
/opt/soft/log/rsync
.log 2>&1
done
|
赋予执行权限: chmod +x inotify_bak.sh
而后执行:nohup inotify_bak.sh & 放入后台执行
目标服务器:先启动rsync后台服务: /usr/bin/rsync --daemon
来源服务器: 执行 inotify_bak.sh &
在来源服务器目录中新建目录和文件,inotify_bak.sh脚本会检测到,而后同步到目标服务器的相关目录下
能够查看日志文件: /opt/soft/log/rsync.log 命令以下:观察实时同步的状况。
1
|
tail
-f
/opt/soft/log/rsync
.log
|
/usr/local/bin/inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory
这是由于找不到库文件的缘由,作一个软链接就行了
1
|
ln
-s
/usr/local/lib/libinotifytools
.so.0
/usr/lib64/libinotifytools
.so.0
|