1. 程式人生 > >linux:rsync + inotifywait 實現【準實時】同步

linux:rsync + inotifywait 實現【準實時】同步

直接上指令碼

#!/bin/bash
export PATH=./sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:$PATH

src=/tmp1
#dest[email protected]192.168.0.18::dest
des=/tmp2
#CMD="rsync -avzcR --password-file=/etc/rsyncd.passwd"
CMD="rsync -avzcR --delete"
LASTFILE=ABC

cd ${src} || exit 1
inotifywait -mrq --format  '%,e %w%f
' -e create,delete,modify,attrib,close_write,move ./ | while read file do echo $(date) : $file EVENT=$(echo $file | awk '{print $1}') FILE=$(echo $file | awk '{print $2}') if [[ "${LASTFILE}" == "${FILE}" ]] ; then continue; fi ${CMD} $(dirname ${LASTFILE}) ${des} LASTFILE
=${FILE} done

 

 

優點:

  比網上覆雜的實現簡單多了:) 且兼顧了效率問題,大大減少無效同步次數

 

缺點:

  如果檔案系統寫活動不再持續,則最後一個檔案的寫操作不能觸發同步,可能造成問題(解決:可以起一個守護程式,定期執行一個寫活動,類似心跳)