rsync+inotify数据传输

发布时间 2023-11-20 21:44:15作者: 不爱学的李泽信

rsync+inotify数据传输

说明与名称 IP 应用 操作系统
源端:server 192.168.58.146 rsync inotify-tools 脚本 centos7
目标:node 192.168.58.152 rsync centos7

一、在node服务器操作

1. 关闭防火墙与selinux

systemctl stop firewalld
systemctl disable firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
getenforce 0

2. 安装rsync服务端软件

yum -y install rsync

3. 设置rsyncd.conf配置文件

vim /etc/rsyncd.conf  # 增加以下内容

注意在实际使用中,配置文件中不要有注释
log file = /var/log/rsyncd.log # 日志文件位置,启动rsync后自动产生,无需提前创建
pidfile = /var/run/rsyncd.pid #pid文件存放位置
lock file = /var/run/rsync.lock #支持max connections参数的锁文件
secrets file = /etc/rsync.pass #用户认证配置文件,里面存放用户名称和密码,必须手动创建这个文件
[etc_from_client]  #自定义同步名称
path = /data/web/ #rsync服务端存放路径,客户端的数据将同步到此目录
comment = sync etc from client
uid = root  #设置rsync运行权限为root
gid = root   #设置rsync运行权限为root
port = 873    #默认端口为873
ignore errors  #表示出现错误忽视错误
use chroot = no  #默认为true ,修改为no,增加对目录软链接的备份
read only = no #设置rsync服务端为读写权限
list = no  #不显示rsync服务端资源列表
max connections = 200 #最大连接数
timeout = 600  #设置超时时间
auth users = hanweb  #执行数据同步的用户名,可以设置多个,用英文逗号隔开
hosts allow = 192.168.58.146 #允许进行数据同步的IP地址,可以设置多个,用英文逗号隔开
hosts deny = 192.168.24.188  # 禁止进行数据同步的IP地址,可以设置多个,用英文逗号隔开

4. 创建存放路径目录

mkdir /data/web -p

5. 创建用户认证文件

echo 'admin:admin@123' > /etc/rsync.pass

6. 设置文件权限

chmod 600 /etc/rsync* 

7. 启动rsync服务并设置开机自启动

systemctl enable rsyncd --now

二、在server服务器操作

1. 关闭防火墙与selinux

systemctl stop firewalld
systemctl disable firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
setenforce 0

2. 安装epel源

yum -y install epel-release

3. 安装rsync服务端软件,只需要安装

yum -y install rsync

4. 创建认证密码文件

echo 'admin@123' > /etc/rsync.pass

5. 设置文件权限

chmod 600 /etc/rsync.pass

6. 创建测试目录

mkdir -pv /data/test
rsync -avH --port 873 --progress --delete /data/web admin@192.168.58.152::etc_from_client --password-file=/etc/rsync.pass


# 运行完成后在node服务器上查看
ll /data/web

7. 安装inotify-tools,实时触发rsync同步

# 检查服务器内核是否支持inotify
ll /proc/sys/fs/inotify/

max_queued_events
max_user_instances
max_user_watches
# 如果有这三个max开头的文件则表示服务器内核支持inotify
# 安装inotify-tools
yum -y install inotify-tools

8. 编写同步脚本

mkdir /scripts
touch /scripts/inotify.sh
chmod 755 /scripts/inotify.sh

vim /scripts/inotify.sh 
host=192.168.58.152 # 目标服务器的ip(备份服务器)
src=/data/web/ # 在源服务器上所要监控的备份目标
des=etc_from_client # 自定义的模块名,需要与目标服务器上的定义名称同步
password=/etc/rsync.pass # 执行数据同步的密码文件
user=hanweb  # 执行数据同步的名
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ; do
    rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done


# 检查脚本
bash -x /scripts/inotify.sh

# 启动脚本
nohup bash /scripts/inotify.sh &
ps -ef|grep inotify

# 生成测试文件并同步
echo 'hello world' > /data/web/test

# 查看inotify生成的日志
tail /tmp/rsync.log

9. 设置脚本开机自动启动

chmod +x /etc/rc.d/rc.local

echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local

10. 在node节点验证是否同步成功

ll /data/web