rsync作为监控 inotify作为服务
我得顺序是先配置rsync 再配置inotify
介质下载路径
https://download.csdn.net/download/qq_35653822/11192154
1 安装rsync
在225和220上分别安装rsync作为监控
- 解压
tar -zxvf rsync-3.0.9.tar.gz
- 配置
mkdir rsync
cd rsynv-3.0.9
./configure --prefix=/opt/rsync
- 编译
make && make install
- 配置文件
rsync.conf配置如下
uid = root
gid = root
port = 873
max connections = 200
timeout = 300
use chroot = no
pid file = /opt/rsync/rsyncd.pid
lock file = /opt/rsync/rsyncd.lock
log file = /opt/rsync/rsyncd.log
host allow = *
[html]
path=/opt/nginx/html
comment = rsync files
ignore errors
read only = false
list = no
auth users = rsync
secrets file = /etc/rsyncd.pass #注意 密码文件位置
rsync.pass配置如下
rsync:123456
- 赋权
chmod 600 rsyncd.pass
- 启动rsync
./rsync --deamon /etc/rsyn.conf
2 安装inotify
在225上服务器安装inotify 作为服务端
- 解压
tar -zxvf inotify-tools-3.14.tar.gz
- 配置
cd inotify-tools-3.14
./configure --prefix=/opt/inotify
- 编译
make && make install
4)上传upload.sh文件
5)赋权
chmod +x upload.sh
6)建立对方服务器密码文件
vim rsyncd220.pass
内容为对方rsync的密码
123456
chmood 600 rsyncd220.pass
7)后台运行脚本
nohup bash upload.sh &
upload.sh内容如下(#后为解释说明 )
#!/bin/sh
#SRC=/share/share1/data/middleware/tomcat/maupload/upload
SRC=/opt/nginx/html/ #同步路径
[email protected]::html #html为rsync.conf中配置的节点名 172.22.22.220是你要往哪台服务器同步就写哪台ip
前提是另外一台服务器要安装了rsync 并且对方rsync.conf中有html这个节点
#增量同步(同步)
dir_sync(){
echo "开始同步"
rsync -avz --progress --password-file=/etc/rsyncd203.pass $SRC $DST
echo "完成同步!"
}
#主函数
main(){
# 启动时先同步一次
dir_sync
echo "进入阻塞状态,等待触发事件..."
#循环
/opt/inotify/bin/inotifywait -mrq -e modify,create ${SRC} | while read D E F
do
echo "目录变化" $D, $E, $F
# 执行同步
dir_sync
echo "进入阻塞状态,等待触发事件..."
done
}
#入口
echo "启动同步"
main
6.2.2.3验证
- 在有inotify的服务器执行
rsync -avz --password-file=/etc/rsyncd220.pass /opt/nginx/conf/nginx.conf rsync@172.22.22.220::html
2.在有inotify的服务器同步目录下上传静态资源,如果另外一台服务器的同步目录下也出现相同静态资源则同步成功