我的一次rsync+inotify本地数据同步示例

环境:
web工作目录:/var/www/mydafuhao
git仓库目录: /var/www/mydafuhao.git/mydafuhao

需求:inotify监控git仓库目录,发现有版本更新立即同步至web工作目录
之前的想法是git仓库目录与web工作目录做软连接,但是发现apache2 无法识别软连接的目录,具体原因没查到,所以用数据同步方法实现版本更新

ubuntu16.4 系统已经安装了rsync下载安装inotify
# apt-get install make     #默认没有安装make
# apt-get install automake libtool
# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar xzf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14/
# ./configure --prefix=/usr/local/inotify
# make && make install

---------------------------------------------------------------------------------------------------------
# cat /var/www/mydafuhao.com.git/notify_rsync.sh
#!/bin/bash
# xliang
# 2018-12-26
export PATH=/bin:/usr/bin:/usr/local/bin:/usr/local/inotify/bin

WEB=mydafuhao.com
SRC=/var/www/$WEB.git     #监控目录路径
DEST1=/var/www/$WEB       #更新同步目录
DT=`date +"%Y-%m-%d %H:%M:%S"`    # 打印当前时间

#wait for change
inotifywait -mrq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' \
--event modify,create,move,delete,attrib $SRC/$WEB | while read line
do
  echo "[$DT] -> file update:" >> /var/www/$WEB.git/rsync.log 2>&1
  echo "$line" >> /var/www/$WEB.git/rsync.log 2>&1
  echo "" >> /var/www/$WEB.git/rsync.log 2>&1
  rsync -avz --exclude=".git" --progress $SRC/$WEB/ $DEST1/ >> /var/www/$WEB.git/rsync.log 2>&1
done

---------------------------------------------------------------------------------------------------------
使用rsync一定要注意的一点是,源路径如果是一个目录的话,带上尾随斜线和不带尾随斜线是不一样的,不带尾随斜线表示的是整个目录包括目录本身,
带上尾随斜线表示的是目录中的文件,不包括目录本身。
---------------------------------------------------------------------------------------------------------

--然后执行此脚本 sh notify_rsync.sh
# nohup sh /var/www/mydafuhao.com.git/notify_rsync.sh &         #后台执行
# echo "nohup sh /var/www/mydafuhao.com.git/notify_rsync.sh &" >> /etc/rc.local      #开机启动

# ps -aux |grep notify

root 25939 0.0 0.0 4504 708 pts/1 S 10:32 0:00 sh /var/www/mydafuhao.com.git/notify_rsync.sh

上一篇:[转载]如何在Ubuntu上安装LAMP服务器系统


下一篇:织梦DedeCMS信息发布员发布文章阅读权限不用审核自动开放亲测试通过!