inotify + rsync實現(xiàn)linux文件實時同步
一.項目需求: A 192.168.13.151 三.安裝配置 服務(wù)器端BC (192.168.13.252 192.168.13.253) # vi /etc/rsyncd.conf uid = root gid = root use chroot = no max connections = 10 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsync.log [images] path = /opt/images read only = no auth users = valeRsync secrets file = /etc/rsyncd.secrets 建立用戶認(rèn)證文件 開機自動執(zhí)行 啟動服務(wù) # wget http://ncu.dl./project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz 建立用戶密碼文件 開機自動執(zhí)行 我的腳本 #!/bin/bash host_1=192.168.13.252 host_2=192.168.13.253 src=/opt/image/ dst=images user=valeRsync /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read file do rsync -avzP --delete --password-file=/etc/rsyncd.secrets $src $user@$host_1::$dst > /dev/null rsync -avzP --delete --password-file=/etc/rsyncd.secrets $src $user@$host_2::$dst > /dev/null #echo "${file} was rsynced" done exit 0 四.inotify支持的觸發(fā)事件 IN_ACCESS,即文件被訪問 備注: #!/bin/sh #set -x #var src="/usr/local/nginx/html/lib /usr/local/nginx/html/www /usr/local/nginx/html/var/www.work.com.conf.php" des_ip="172.18.1.35 172.18.1.36 172.18.1.37 172.18.1.38" #function inotify_fun () { /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,move $1 | while read time file do for ip in $des_ip do echo "`date +%Y%m%d-%T`: rsync -avzq --delete --progress $1 $ip:`dirname $1`" rsync -avzq --delete --progress $1 $ip:`dirname $1` echo done done } #main for a in $src do inotify_fun $a & done |
|