inotify+rsync实时文件同步

版权声明:转发原创文章请复制文章链接地址 https://blog.csdn.net/weixin_42579642/article/details/83993360

代码发布服务器 192.168.67.133

发布服务器上下载
wget https://download.samba.org/pub/rsync/src/rsync-3.1.3.tar.gz
wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

在代码发布服务器上安装inotify,执行如下命令
tar xzvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
cd ..
安装完毕 建立软连接
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

#------以下命令在每台服务器上都需要执行---------
在所有服务器上安装rsync,命令如下:
wget https://download.samba.org/pub/rsync/src/rsync-3.1.3.tar.gz
tar zxvf rsync-3.1.3.tar.gz
cd rsync-3.1.3
./configure
make
make install
#------以上命令在每台服务器上都需要执行---------

免登陆配置
在代码发布服务器与需要同步的服务器之间配置ssh key信任,在代码发布服务器上生成公钥和私钥
ssh-keygen -t rsa
直接三次回车
将公钥添加到各个需要更新的主机authorized_keys 文件中,在代码更新服务器上再执行
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]#需要同步的服务器IP
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]#需要同步的服务器IP

如果有提示 输入yes
按提示输入密码回车

//----------------------------------------------------------

在代码发布服务器上以root身份创建inotify_rsync.sh脚本
mkdir /root/script
vi /root/script/inotify_rsync.sh 输入

#!/bin/sh
SRC=/home/wwwroot/default/    #代码发布服务器目录
DST=/home/wwwroot/default/    #目标服务器目录
IP="192.168.67.131 192.168.67.132"    #目标服务器IP,多个以空格隔开
USER=root
/usr/local/bin/inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f %e' --exclude "(.swp|.swx|.svn)" \
-e create,move,delete,close_write,attrib $SRC | while read files
do
for i in $IP
do
    /usr/local/bin/rsync -ahqzt --exclude Runtime --delete $SRC $USER@$i:$DST
    echo $files >>/tmp/rsync.log 2>&1 
done
done

然后赋予脚本可执行权限
chmod +x /root/script/inotify_rsync.sh
设置开机自启动 echo "/root/inotify_rsync.sh &" >> /etc/rc.local
执行脚本/root/script/inotify_rsync.sh &


http://www.ttlsa.com/web/let-infotify-rsync-fast/

相关解释-------

相关解释:

-------------------实时同步----------------------
/usr/local/bin/inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f %e' --exclude "(.swp|.swx|.svn)" \
-e create,move,delete,close_write,attrib $SRC | while read files

 -m 是保持一直监听
 -r 是递归查看目录
 -q 是打印出事件
 -e create,move,delete,close_write,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件
 
/usr/local/bin/rsync -ahqzt --exclude Runtime --delete $SRC $USER@$i:$DST
 -a 存档模式
 -h 保存硬连接
 -q 制止非错误信息
 -z 压缩文件数据后传输
 -t 维护修改时间
 -delete 删除于多余文件

--exclude 排除同步的文件
-------------------实时同步解释----------------------

shutdown -r now

ps aux |grep inotify

/etc/init.d/nginx {start|stop|reload|restart}

监控脚本
#!/bin/bash
if [ "$(ps aux | grep  "nginx: master process"|grep -v grep)" == "" ]
then
        service keepalived stop
fi 
 

猜你喜欢

转载自blog.csdn.net/weixin_42579642/article/details/83993360