天天看點

rsync+inotify實作實時同步更新

rsync

工作模式:

1、單個主機,cp

2、ssh通道,scp

3、守護程序

rsync -avzP /1 /tmp

rsync --delete -r /2/ /1(删除1下的2目錄)

rsync /etc/hosts -e 'ssh -p 52113' root@ip:/

-z:壓縮傳輸

-a:子目錄遞歸

-o:保持檔案屬主

-p:保持檔案權限

-g:保持檔案屬組

-l:保持連結檔案

--exclude:指定不需要傳輸的檔案或檔案夾

--exclude-from:

守護程序模式:

vim /etc/rsyncd.conf

添加:

log file=/var/log/rsync.log

pid file=/var/run/rsyncd.pid

use chroot = no

timeout = 300

[test]

path=/tmp/syk/

use chroot=true

max connections=4

read only=no

list=true

uid=root

gid=root

auth users=syk

secrets file=/etc/rsyncd.passwd

建立使用者:

groupadd rsync

useradd  -g rsync rsync -s /sbin/nologin

chown -R rsync.rsync /tmp/syk/

rsync --daemon(啟動)

cat /etc/rsyncd.passwd 

syk:syk123

chmod 600 /etc/rsyncd.passwd

用戶端:

yum -y install rsync

echo "syk123" > /etc/rsyncd.passwd

測試:

rsync -avz syk@server_ip::test /tmp

rsync -avz syk@server_ip::test /tmp --password-file=/etc/rsyncd.passwd

rsync -avz rsync://syk@server_ip/test /tmp --password-file=/etc/rsyncd.passwd

無差異同步:

--delete

inotify

inotifywait:

-r:遞歸

-q:列印很好的資訊

-exclude:指定不需要監控的檔案或檔案夾

-e:指定需要監控的事件

access:目錄或檔案被通路時

nodify:被修改

attrib:屬性被修改時

close:檔案被封閉

open:被打開

moved_to:被移動其他目錄

move:被移動到目前或其他目錄

create:被建立

delete:被删除

unmount:被解除安裝

    人工測試監控事件:

./bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /backup

./bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /backup

./bin/inotifywait -mrq  --format '%w%f' -e create,delete /backup

編寫監控腳本:

#/bin/bash

/usr/local/inotify/bin/inotifywait -mrq --format '%w%f' -e create,delete /tmp/syk/ \

|while read line

do

rsync --delete -az  /tmp/syk/ [email protected]::test --password-file=/etc/rsyncd.passwd >/dev/null 2>&1

done

exit 0

本文轉自 sykmiao 51CTO部落格,原文連結:http://blog.51cto.com/syklinux/1851902,如需轉載請自行聯系原作者