配置 Rsync 下行同步
1.环境准备
主机 主机名 操作系统 IP 地址 主要软件
Master CentOS 7-1 CentOS7 192.168.126.11 rsync
Slave CentOS 7-2 CentOS7 192.168.126.12 rsync / inotify-tools-3.14.tar.gz
2.将 Master 服务器数据备份到 Slave 服务器
Master(192.168.126.11):
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#关闭防火墙及安装机制
yum install -y httpd rsync
#rsync系统一般已默认安装,安装httpd是为了生成/var/www/html目录(后续会用到作为共享目录)
vim /etc/rsyncd.conf
#编辑rsync配置文件
uid = root
gid = root
use chroot = yes
address = 192.168.126.11
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.126.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.xcf.com
read only = yes
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = xixi
secrets file = /etc/rsyncd_users.db
----详解----
uid = root #用户id
gid = root #组id
use chroot = yes #开启,禁锢在源目录
address = 192.168.126.11 #监听地址
port 873 #默认端口号为873
log file = /var/log/rsyncd.log #日志文件存放位置
pid file = /var/run/rsyncd.pid #存放进程id的文件位置
hosts allow = 192.168.126.0/24 #允许访问的主机网段
[wwwroot] #共享模块的名称
path = /var/www/html #源目录路径
comment = Document Root of www.xcf.com #
read only = yes #是否为只读
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z #同步时不再压缩的文件类型
auth users = xixi #授权用户,多个账户以空格隔开
secrets file = /etc/rsyncd_users.db #存放账号信息的数据文件,一行一个
----
vim /etc/rsyncd_users.db
xixi:123123
#编辑用户账号文件,固定格式为[名称:密码],一行一个
chmod 600 /etc/rsyncd_users.db
#官方要求,最好只是赋权600!
rsync --daemon
#开启服务
netstat -natp | grep rsync
#检测端口号,确认服务是否成功开启
cd /var/www/html
#切换至共享目录下
touch qwe.html zxc.html
ls
3.实现下行同步
Slave(192.168.126.12):
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum install -y rsync
cd /opt
mkdir xcf1
chmod 777 xcf1
vim /etc/server.pass
123123
#编辑免交互密钥文件,第一行为密码
chmod 600 /etc/server.pass
#给密钥文件赋权600
rsync -az --delete --password-file=/etc/server.pass xixi@192.168.126.11::wwwroot /opt/xcf1
#rsync,使用密钥文件/etc/server/pass对应xixi用户,IP地址为192.168.126.11的共享模块文件进行压缩,并归档同步至当前服务器的/opt/xcf1目录下,同时删除差异内容,保持一致性
ls xcf1
#查看下行同步是否成功
配置 Rsync+Inotify 实时同步
1.Master 关闭只读模式并为共享目录赋权
Master(192.168.126.11):
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
#为共享目录赋权777
2.优化 Slave 内核参数
Slave(192.168.126.12):
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
#查看当前默认inotify的内核参数
#依次为监控事件队列、最多监控实例与每个实例最多监控的文件
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
#优化内核参数
sysctl -p
#加载内核参数配置文件使其生效
3.编译安装 inotify-tools
inotify-tools下载地址
Slave(192.168.126.12):
yum install -y gcc gcc-c++
cd /opt
#将软件包传至该目录下
tar zxf inotify-tools-3.14.tar.gz
cd /opt/inotify-tools-3.14/
./configure
make -j 4 && make install
4.编写自动监控同步脚本
Slave(192.168.126.12):
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/xcf1/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/xcf1/ xixi@192.168.126.11::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
----详解----
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/xcf1/"
#INOTIFY_CMD变量:持续监控 /opt/xcf1 目录中的创建,删除,移动,修改,改变时间的操作
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/xcf1/ xixi@192.168.126.11::wwwroot"
#RSYNC_CMD变量:使 xixi 用户,/etc/server.pass 密钥文件,将 /opt/xcf1 目录下的文件进行压缩,归档,保留硬链接文件同步至 192.168.126.11 的共享模块定义的目录 /var/www/html 下,并删除差异性内容,保持一致性
$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/xcf1
touch qqq.html
rm -rf qwe.html
#创建一个新的html文件并删除之前的qwe
ls
#再次确认一下
5.验证
Master(192.168.126.11):
cd /var/www/html
ls
#可以看到实时同步成功
#之后还可继续尝试,Master在共享目录内如何操作都不会影响到Slave端,而Slave的在该目录内的操作都会同步给Master
原文链接:https://blog.csdn.net/weixin_51486343/article/details/114378464