rsync+inotify實現數據同步

rsyncd用來同步2臺機器上的操作來保證數據的一致,inotify用來檢測系統的變化,利用rsync+inotify來實現實時同步

s:192.168.1.234   路徑:/usr/local/nginx-1.6.3/html/home/paixian/root

c:192.168.1.136   路徑:/data/paixian/root


 

1.服務器上的操作

yum install rsync xinetd -y       ///下載軟件

 

vi /etc/rsyncd.conf             ///手動創建rsyncd配置文件

#Global Settings

uid = nobody

gid = nobody

use chroot = yes

max connections = 10

timeout = 300

strict modes = yes

pid file = /var/run/rsync/rsyncd.pid

#lock file = /var/run/rsync/rsyncd.lock

log file = /var/log/rsync/rsyncd.log

[data]

#path =/usr/local/nginx-1.6.3/html/home/paixian/root

path =/usr/local/nginx-1.6.3/html/home/paixian/root

ignore errors = yes

read only = no

write only = no

hosts allow = 192.168.1.0/24

hosts deny = *

list = false

uid = root

gid = root

auth users = min

secrets file = /etc/rsyncd.passwd       

 

vi /etc/rsyncd.passwd              ///創建密碼文件

  min:min

chmod 600 /rsyncd.passwd

 

service xinetd start                  ///啓動xinetd服務(包括rsyncd服務)

 

2.客戶端配置

先測試rysnc

echo "passwd" > /etc/rsync.passwd    ///創建密碼文件

[root@min root]# rsync --list-only--password-file=/etc/rsyncd.passwd [email protected]::data

drwxrwxrwx        4096 2015/09/10 17:46:58 .

-rwxr-xr-x         404 2015/09/10 14:58:26 admin.php

-rwxr-xr-x         244 2015/09/10 14:50:04 config.php

-rwxr-xr-x         299 2015/09/10 14:50:04 index.php

-rwxr-xr-x          25 2015/09/10 14:58:26 phpinfo.php

-rwxr-xr-x        1414 2015/09/10 14:50:02product_list.phtml

-rwxr-xr-x        1998 2015/09/10 14:58:26 test.php

-rw-r--r--         114 2015/09/10 14:46:31 test1.php

drwxrwxrwx        4096 2015/09/10 14:59:05 .cache

drwxrwxrwx        4096 2015/09/10 14:58:28 admin

drwxrwxrwx        4096 2015/09/10 14:50:02 data

drwxrwxrwx        4096 2015/09/10 14:58:26 demo

drwxrwxrwx        4096 2015/09/10 14:50:02 demo2

drwxrwxrwx        4096 2015/09/10 14:46:31phpdemo_boxcontroler

drwxrwxrwx        4096 2015/09/10 14:46:34 statics

drwxrwxrwx        4096 2015/09/10 14:58:26 test

drwxrwxrwx        4096 2015/09/10 14:50:41 web

 

利用inotify監控系統的變化

wget                                                                              http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

mv  inotify-tools-3.14.tar.gz  /usr/local

cd /usr/local

tar xf inotify-tools-3.14.tar.gz

mv inotify-3.14 inotify

./configure

make&&make install

寫腳本實現同步

[root@min root]# cat/usr/local/inotify/backup.sh

inotifywait -mrq --timefmt'%Y/%m/%d-%H:%M:%S' --format '%T %w %f %e' -e modify,attrib,move,create,delete/data/paixian/root|while read files

do

rsync -vzrtopg --delete --progress--password-file=/etc/rsyncd.passwd /data/paixian/root/ [email protected]::data&& echo "${files} was rsynced" >>/tmp/rsync.log2>&1

echo"--------------------------------------------------------"

done

 

運行腳本

bash /usr/local/inotify/backup.sh &

[root@min root]# ps -ef |grep backup

root      1670      1  0 16:16 ?        00:00:00 bash/usr/local/inotify/backup.sh

root      1672   1670  0 16:16 ?        00:00:00 bash /usr/local/inotify/backup.sh

root      1782   1729  0 18:32 pts/1    00:00:00 grep backup

 

 

 

測試

服務器

[root@localhost root]# pwd

/usr/local/nginx-1.6.3/html/home/paixian/root

[root@localhost root]# ls

total 72

drwxrwxrwx. 11 root root 4096 Sep 10 18:36.

drwxr-xr-x. 4 root root 4096 Sep  7 06:34 ..

drwxrwxrwx. 8 root root 4096 Sep 10 14:58 admin

-rwxr-xr-x  1 root root  404 Sep 10 14:58admin.php

drwxrwxrwx. 2 root root 4096 Sep 10 14:59 .cache

-rwxr-xr-x  1 root root  244 Sep 10 14:50config.php

drwxrwxrwx. 6 root root 4096 Sep 10 14:50 data

drwxrwxrwx. 4 root root 4096 Sep 10 14:58 demo

drwxrwxrwx. 3 root root 4096 Sep 10 14:50 demo2

-rwxr-xr-x  1 root root  299 Sep 10 14:50index.php

drwxrwxrwx. 3 root root 4096 Sep 10 14:46 phpdemo_boxcontroler

-rwxr-xr-x  1 root root   25 Sep 10 14:58phpinfo.php

-rwxr-xr-x  1 root root 1414 Sep 10 14:50 product_list.phtml

drwxrwxrwx. 7 root root 4096 Sep 10 14:46 statics

drwxrwxrwx. 2 root root 4096 Sep 10 14:58 test

-rw-r--r--  1 root root  114 Sep 10 14:46test1.php

-rwxr-xr-x  1 root root 1998 Sep 10 14:58 test.php

drwxrwxrwx. 17 root root 4096 Sep 10 14:50web

[root@localhost root]#

客戶機

[root@min root]# pwd

/data/paixian/root

[root@min root]# mkdir min

[root@min ~]# tail -f /tmp/rsync.log

2015/09/10-18:35:27 /data/paixian/root/ minCREATE,ISDIR was rsynced

 

 

 

http://debris.blog.51cto.com/1614432/1683627

http://ljbaby.blog.51cto.com/10002758/1675870


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章