测试环境:
资源服务器(主服务器):192.168.200.95
备份服务器(客户端):192.168.200.89
同步目录:/etc/test
同步时使用的用户名hadoop密码12345
实验目的:资源服务器的同步目录下的文件变化时,备份服务器的同步目录更新,以资源服务器为准,去同步其他客户端。
一、资源服务器配置
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、建立密码文件
#vi /etc/rsync.pas
12345
#cd /etc
#chmod 600 rsync.pas
3、安装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
4、创建自动同步脚本autorsync.sh
放在同步目录下
脚本文件放在同步目录下autorsync.sh
vi autorsync.sh
#!/bin/bash
host=192.168.200.89
src=/etc/test
des=backup
user=hadoop
/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/bin/rsync -vzrtopg --delete --progress--password-file=/etc/rsync.pas $src $user@$host::$des
>>/var/log/rsyncprogress.log2>&1 &&
echo "${files} was rsynced">>/var/log/rsync.log 2>&1
done
给予764权限
#chmod 764 rsync.sh
5、运行这个脚本
#sh /tmp/rsync.sh &
我们还可以把autorsync.sh脚本加入到开机启动项里
#echo "/etc/test/autorsync.sh" >> /etc/rc.local
二、备份服务器配置
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、建立密码文件
#vi /etc/rsync.pas
hadoop:12345
#cd /etc
#chmod 600 rsync.pas
3、配置/etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 4
strict modes =yes
port = 873
[backup]
path = /etc/
comment = This is a test
read only = yes
list = no
auth users = hadoop
secrets file = /etc/rsync.pas
hosts allow = 192.168.200.95
#transfer logging = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
注意:path = /etc/ 是同步目录的上一级
4、启动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