一:配置rsync源服务器(服务器A)
注意事项 : 可以在最开始就把每台服务器上的防火墙关闭,当然也可以开放防火墙
这里是关闭防火墙以及Linux内核
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
注意 每台服务器都可以这样配置 我已经试过了 可以实现 注意改动IP地址
[root@localhost ~]# yum -y install rsync xinetd
[root@localhost ~]# vim /etc/rsyncd.conf //rsync的配置文件
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.10.101
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.10.0/24
[www] //同步站点的名字 (可以多个)
path = /var/www/html //同步的目录
comment = Document Root of www1.benet.com
read only = no //需要no掉 yes是只读 no是可写
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = bdqn
secrets file = /etc/rsyncd_users.db
[root@localhost ~]# vim /etc/rsyncd_users.db //创建文件 添加一下内容
bdqn:pwd123
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db //限制权限
[root@localhost ~]# chmod -R 777 /var/www/html/ //给同步目录权限
[root@localhost ~]# rsync –daemon //以守护进程方式启动 想要关闭需要kill
[root@localhost ~]# netstat -auntp | grep rsync
tcp 0 0 192.168.10.101:873 0.0.0.0:* LISTEN 57018/rsync
二:配置rsync目标服务器(服务器B)
这是服务器的内核参数: (都是默认的)
[root@localhost ~]# vi /proc/sys/fs/inotify/max_queued_events
16384 \\监控时间队列
[root@localhost ~]# vi /proc/sys/fs/inotify/max_user_instances
128 \\最多监控实例树
[root@localhost ~]# vi /proc/sys/fs/inotify/max_user_watches
8192 \\每隔实例最多监控文件数
安装inotify 实现同步复制
[root@localhost ~]# cd /usr/src
[root@localhost src]# tar zxf inotify-tools-3.14.tar.gz
[root@localhost src]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make
[root@localhost inotify-tools-3.14]# make install
[root@localhost ~]# vim /etc/server.pass //添加一个密码文件 方便脚本使用
添加
pwd123
[root@localhost ~]# chmod 600 /etc/server.pass //限制权限
[root@localhost ~]# vim 1.sh //写个同步脚本
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www/html"
RSYNC_CMD="rsync -avzH --delete --password-file=/etc/server.pass /var/www/html/ bdqn@192.168.10.102::www"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
$RSYNC_CMD
Done
-m:持续监控
-r:递归整个目录
-q:简化输出信息
打开另一个终端在var/www/html中创建文件,观察上个终端的变化
在到服务器(A)上看看var/www/html中的变化