inotify+rsync实现静态资源同步

rsync作为监控   inotify作为服务

我得顺序是先配置rsync 再配置inotify

介质下载路径 

https://download.csdn.net/download/qq_35653822/11192154

1 安装rsync

在225和220上分别安装rsync作为监控

  1. 解压

tar -zxvf rsync-3.0.9.tar.gz

  1. 配置

mkdir rsync

cd rsynv-3.0.9

./configure --prefix=/opt/rsync

  1. 编译

make && make install

  1. 配置文件

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

  1. 赋权

chmod 600 rsyncd.pass

  1. 启动rsync

./rsync --deamon /etc/rsyn.conf

 

2 安装inotify

在225上服务器安装inotify 作为服务端

  1. 解压

tar -zxvf inotify-tools-3.14.tar.gz

  1. 配置

cd inotify-tools-3.14

./configure --prefix=/opt/inotify

  1. 编译

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验证

  1. 在有inotify的服务器执行

rsync -avz --password-file=/etc/rsyncd220.pass /opt/nginx/conf/nginx.conf rsync@172.22.22.220::html

2.在有inotify的服务器同步目录下上传静态资源,如果另外一台服务器的同步目录下也出现相同静态资源则同步成功

发布了50 篇原创文章 · 获赞 2 · 访问量 9425

猜你喜欢

转载自blog.csdn.net/qq_35653822/article/details/90413674
今日推荐