文章目录
rsync实时同步实验
rsync服务器:192.168.209.134
客户机:192.168.209.135
配置rsync服务器
检查rsync
rpm -q rsync
rpm -qc rsync
配置主配置文件
vim /etc/rsyncd.conf
修改如下
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
address = 192.168.209.134
port 873
log file = /var/log/rsyncd.log
hosts addow = 192.168.209.0/24
[wwwroot]
path = /var/www/html
comment = www.kgc.con
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
设置账户和密码
vim /etc/rsyncd_users.db
backuper:abc123
对账户密码文件赋予只读权限,保证安全
chmod 600 /etc/rsyncd_users.db
启动
rsync --daemon
查看接口是否开启
netstat -nultp | grep rsync
清空关闭服务
iptables -F
setenforce 0
添加测试网页,并赋予权限
yum install httpd -y
cd /var/www/html
echo "this is test web" > index.html
cat index.html
cd ..
chmod 777 html/
客户机
连接sync服务器
格式一:
[root@localhost ~]# rsync -zva backuper@192.168.209.134::wwwroot /opt/
Password:
receiving incremental file list
./
index.html
sent 46 bytes received 128 bytes 9.94 bytes/sec
total size is 17 speedup is 0.10
格式二:
[root@localhost ~]# rsync -avz rsync://backuper@192.168.209.134/wwwroot /opt/
Password:
receiving incremental file list
sent 20 bytes received 65 bytes 5.86 bytes/sec
total size is 17 speedup is 0.20
然后我们到opt目录下,查看一下是否已经有同步
cd /opt
ls
index.html rh
cat index.html
this is test web
这边看到已经同步到了
编写server.pass
vim /etc/server.pass
abc123
chmod 600 /etc/server.pass
免交互
rsync -zva --delete --password-file=/etc/server.pass backuper@192.168.209.134::wwwroot /opt/
添加测试网页
cd /var/www/html
echo "this is KY03 web" > web.html
调整inotify内核参数
vim /etc/sysctl.conf
监控事件队列大小
fs.inotify.max_queued_events = 16384
最多监控实例数
fs.inotify.max_user_instances = 1024
每个实例最多监控文件数
fs.inotify.max_user_watches = 1048576
启动
sysctl -p
接下来,我们把inotify-tools的压缩包解压到opt目录
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
yum install gcc gcc-c++ -y
make && make install
开始监听
inotifywait -mrq -e modify,create,move,delete /var/www/html/
接下来,我们对客户机重新打开一个新的窗口
进行实时同步
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.209.134::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -1) -le 0 ] ; then
$RSYNC_CMD
fi
done
赋予执行权限
chmod +x inotify.sh
./inotify.sh
这边注意,实时同步是需要上传内容的,所以我们要在rsync中,将只读关闭
vim /etc/rsyncd.conf
修改如下:
read only = no
然后分别对两台机器中的html文件赋予777权限
chmod 777 /var/www/html