1. 程式人生 > >我的一次rsync+inotify本地數據同步示例

我的一次rsync+inotify本地數據同步示例

mef apt-get 註意 efm col update 下載安裝 下載 configure

環境:
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

我的一次rsync+inotify本地數據同步示例