以两台服务器为例:
主服务器: 192.168.1.100
从服务器: 192.168.1.101
1.安装rsync (主服务器与从服务器同时安装)
使用xinetd管理rsync
yum install rsync xinetd
设置开机启动
vi /etc/xinetd.d/rsync
...
修改为 disable = no
...
启动xinetd
/etc/init.d/xinetd start
2.主服务器配置
vi /etc/rsyncd.conf
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.100
uid = www
gid = www
use chroot = yes
read only = yes
hosts allow = 192.168.1.101
hosts deny = *
max connections = 10
transfer logging = yes
log file = /data/logs/rsync_server.log
syslog facility = local3
timeout = 600
[data_1]
path = /data/www/data_1/
exclude = application/Runtime/ www/logs/ runtime/
auth users = www
list = yes
ignore errors
secrets file = /etc/rsyncd.password
comment = data_1
vi /etc/rsyncd.password
...
test
...
设置权限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.password
重启rsync服务
/etc/init.d/xinetd restart
检查服务是否运行
lsof -i:873
修改inotify参数
sysctl -w fs.inotify.max_queued_events="9999999"
sysctl -w fs.inotify.max_user_watches="9999999"
sysctl -w fs.inotify.max_user_instances="65535"
编辑inotify监控脚本
vi /opt/sh/inotify.sh
src=/data/www/data_1/
des1=data_1
host1=192.168.1.101
user1=www
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read file D E F;
do
/usr/bin/rsync -uoavzcr --progress $src $user1@$host1::$des1 --password-file=/etc/rsyncd.password
done
3.从服务器配置
同主服务器一样安装rsync和xinetd
配置从服务器rsyncd.conf
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.101
uid = www
gid = www
use chroot = yes
read only = no #一定关闭
hosts allow = 192.168.1.100
hosts deny = *
max connections = 10
transfer logging = yes
log file = /data/logs/rsync_slave.log
syslog facility = local3
timeout = 600
[data_1]
path = /data/www/data_1/
exclude = application/Runtime/ www/logs/ runtime/
auth users = www
list = yes
ignore errors
secrets file = /etc/rsyncd.password
comment = data_1
4.开始同步测试
在两台服务器分别建立一样的目录层级
mkdir /data/www/data_1/
主服务器开启inotify监控脚本
chmod +x /opt/sh/inotify.sh
/opt/sh/inotify.sh
加入开机启动
echo "/opt/sh/inotify.sh &" >> /etc/rc.local
在主服务器添加测试文件观察从服务器是否自动同步
touch /data/www/data_1/1.txt
PS:
1.rsync配置权限统一修改为600
2.inotify脚本注意添加运行权限
3.注意主服务器与从服务器需要同步的文件夹初始权限需要相同
4.关键在于从服务器的配置