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