rsync下行同步 & inotify实时同步部署
一、rsync下行同步部署
1. 实验环境
主机 | 操作系统 | 主机IP | 软件包 |
---|---|---|---|
Master | CentOS7 | 192.168.117.10 | rsync |
Slave | CentOS7 | 192.168.117.20 | rsync、inotify-tools-3.14.tar.gz |
2. 服务部署
Master:192.168.117.10
systemctl stop firewalld.service
setenforce 0
yum -y install httpd rsync
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.117.10
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.117.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.ng.com
read only = yes
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = kiki
secrets file = /etc/rsyncd_users.db
vim /etc/rsyncd_users.db
kiki:123
chmod 600 /etc/rsyncd_users.db
rsync --daemon
netstat -natp | grep rsync
cd /var/www/html
touch a.html b.html
Slave:192.168.117.20
systemctl stop firewalld.service
setenforce 0
yum -y install rsync
cd /opt
mkdir ng
chmod 777 ng
vim /etc/server.pass
123
chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass kiki@192.168.117.10::wwwroot /opt/ng
ls ng
二、rsync+inotify实时同步部署
Master:192.168.117.10
vim /etc/rsyncd.conf
read only = no
kill `cat /var/run/rsyncd.pid`
netstat -natp | grep rsync
rsync --daemon
netstat -natp | grep rsync
chmod 777 /var/www/html
Slave:192.168.117.20
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
vim /etc/sysctl.conf #内核参数优化,加大三个参数的值
sysctl -p #加载内核参数配置文件使其生效
yum -y install gcc gcc-c++
#放入安装包
tar zxvf inotify-tools-3.14.tar.gz -C /opt
cd /opt/inotify-tools-3.14/
./configure
make && make install
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/haha/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/haha/ kiki@192.168.184.10::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
cd /opt/
chmod +x inotify.sh
./inotify.sh
cd /opt/ng
touch c.html
rm -rf a.html
验证:
Master:192.168.117.10
cd /var/www/html
ls
上图slave同步时会出现报错,原因是以匿名用户身份登陆的
Master:192.168.117.10