使用pt-kill为MySQL保驾护航

1.前言

  1. 192.168.10.1 服务端-数据发送端(被同步目录/www/
  2. 192.168.10.2 客户端-数据接收端(同步服务端的目录到本机的/data/www/

服务端/data/ftp目录下的文件或目录权限等发生改变,不管是增删改,都同步到客户端指定目录,实现实时同步。

2.部署环境搭建

参考教程

2.1 接收端(192.168.10.2)

2.1.1 安装相关服务

# 安装rsync、inotify-tools
[root@192.168.10.2 root]# yum install rsync -y
# 启动rsyncd服务
[root@localhost root]# systemctl start rsyncd
# 设置rsyncd服务开机自启
[root@192.168.10.2 root]# systemctl enable rsyncd

2.1.2 创建同步的目录存放位置

[root@localhost root]# mkdir -p /www

2.2 发送端(192.168.10.1)

2.2.1 安装相关服务

# 安装rsync、inotify-tools
[root@192.168.10.1 root]# yum install rsync inotify-tools -y
# 启动rsyncd服务
[root@localhost root]# systemctl start rsyncd
# 设置rsyncd服务开机自启
[root@192.168.10.1 root]# systemctl enable rsyncd

2.2.2 编写shell脚本

[root@192.168.10.1 root]# vim /data/shell/rsync_file.sh

#!/bin/bash
echo 8192000 > /proc/sys/fs/inotify/max_user_watches
host=192.168.10.2 #接收端ip
local_path=/www/ #本机需要同步的目录
remote_path=/data/www/ # 远程目录
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $local_path | while read file
do
rsync -avPz --delete --progress $local_path $host:$remote_path >>/root/shell/rsync_file.log 2>&1
echo "${file} was rsynced" >>rsync.log 2>&1
done

2.2.3 编写启动服务

[root@192.168.10.1 root]# vim /usr/lib/systemd/system/rsync_file.service

[Unit]
Description = rsync_file

[Service]
Type=simple
WorkingDirectory=/data/shell/
ExecStart=/usr/bin/bash rsync_file.sh
ExecStop=/usr/bin/ps -ef | /usr/bin/grep "bash" | /usr/bin/grep "rsync_file.sh" | /usr/bin/awk '{print $2}' | /usr/bin/xargs/usr/bin/kill -9
User=root
Group=root

[Install]
WantedBy=multi-user.target

2.2.4 免密登入

#生成ssh秘钥,一直回车直到结束
[root@192.168.10.1 root]# ssh-keygen -t rsa -C'rsync-server'	
#把id_rsa.pub私钥发给192.168.10.2
[root@192.168.10.1 root]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.10.2

2.2.5 测试ssh连接

执行测试ssh是否正常连接且,免密登录

[root@192.168.10.1 root]# ssh root@192.168.10.2

2.2.6 启动服务

执行启动服务,验证是否实时同步,如果源/data/ftp原本有文件或目录,也会自动同步过去

# 设置同步服务开机自启
[root@192.168.10.1 root]# systemctl enable rsync_file
# 启动同步服务
[root@192.168.10.1 root]# systemctl start rsync_file

查看同步日志,在/root/shell下,有 rsync_file.log日志文件,这里就不截图了。

3.完结

感谢老铁,点赞666!


使用pt-kill为MySQL保驾护航
https://www.gmtgo.com/212.html
作者
大帅
发布于
2022年11月27日
许可协议