测试环境:
资源服务器(主服务器):192.168.4.163
备份服务器(客户端):192.168.4.154
同步资源目录:/etc/test
实验目的:资源服务器的同步目录下的文件变化时,备份服务器的同步目录更新,以资源服务器为准,去同步其他客户端。
一、资源服务器配置
1、安装rsync,安装路径为:/usr/local/rsync
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
[root@nginx src]# tar zxvfrsync-3.0.9.tar.gz
[root@nginx src]# cd rsync-3.0.9
[root@nginx rsync-3.0.9]# ./configure--prefix=/usr/local/rsync
[root@nginx rsync-3.0.9]# make
[root@nginx rsync-3.0.9]# make install
2、安装inotify,安装路径/usr/local/inotify
[root@nginx rsync]# cd /usr/src/
[root@nginx src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz
[root@nginx src]# cd inotify-tools-3.14
[root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@nginx inotify-tools-3.14]# make
[root@nginx inotify-tools-3.14]# make install
3、创建自动同步脚本autorsync.sh
#!/bin/bash
host=192.168.4.154
src=/etc/test/
des=backup
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src \
| while read files
do
/usr/local/rsync/bin/rsync -vzrtopg --delete --progress $src $host::$des >>/var/log/rsyncprogress.log2>&1 &&
echo "${files} was rsynced">>/var/log/rsync.log 2>&1
done
给予764权限
#chmod +x rsync.sh
5、运行这个脚本
#sh /tmp/rsync.sh &
二、备份服务器配置
1、安装rsync,安装路径为:/usr/local/rsync
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
[root@nginx src]# tar zxvfrsync-3.0.9.tar.gz
[root@nginx src]# cd rsync-3.0.9
[root@nginx rsync-3.0.9]# ./configure--prefix=/usr/local/rsync
[root@nginx rsync-3.0.9]# make
[root@nginx rsync-3.0.9]# make install
2、配置/etc/rsyncd.conf
[backup]
uid = root
gid = root
path = /data/backup/
hosts allow = 192.168.4.163
read only = no
3、启动rsync
# /usr/local/rsync/bin/rsync --daemon--config=/etc/rsyncd.conf
我们可以把rsync脚本加入到开机启动项里
# echo "/usr/local/rsync/bin/rsync--daemon --config=/etc/rsyncd.conf" >> /etc/rc.local