环境介绍:
服务器类型 | IP 地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器 | 10.10.100.100 | rsync inotify-tools 脚本 |
centos7.9 |
目标服务器 | 10.10.100.101 | rsync | centos7.9 |
rsync 命令
rsync [选项] 原始位置 目标位置
常用选项 说明
-r 递归模式,包含目录及子目录中的所有文件
-l 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程的详细信息
-z 在传输文件时进行压缩 goD
-p 保留文件的权限标记
-a 归档模式,递归并保留对象属性,等同于 -rlpt
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)-o 保留文件的属主标记(仅超级用户使用)-H 保留硬链接文件
-A 保留 ACL 属性信息
-D 保留设备文件及其他特殊文件
--delete 删除目标位置有而原始位置没有的文件
--checksum 根据对象的校验和来决定是否跳过文件
inotify-tools 辅助工具
调整 inotify 内核参数(优化)/etc/sysctl.conf(内核参数配置文件),需要配置
max_queue_events:监控事件队列大小
max_user_instances:最多监控实例
max_user_watches:每个实例最多监控文件数
配置的监控数量应该大于监控目标的总文件数
inotifywait:用于持续监控,实时输出结果
可监控 modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一有变动立即输出结果。inotifywatch:用于短期监控,任务完成后再输出结果
可用来收集文件系统变动情况,并在运行结束后输出汇总的变化情况。例:inotifywait -mrq -e modify,create,attrib,move,delete 文件或目录
-m 持续进行监控
-r 递归监控所有子对象
-q 简化输出信息
-e 指定要监控哪些事件类型
modify 修改
create 创建
attrib 属性更改
move 移动
deletc 删除
需求:把源服务器上 /etc 目录实时同步到目标服务器的 /tmp/ 下
目标服务器操作:
// 关闭防火墙与 selinux
[root@master ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# setenforce 0
// 安装 rsync
[root@master ~]# yum -y install rsync rsync-daemon
// 配置 rsyncd.conf 配置文件
[root@master ~]# 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 = /tmp/ # rsync 服务端数据存放路径,客户端的数据将同步至此目录
comment = sync etc from client
uid = root # 设置 rsync 运行权限为 root
gid = root # 设置 rsync 运行权限为 root
port = 873 # 默认端口
ignore errors # 表示出现错误忽略错误, 这个很重要
use chroot = no # 默认为 true,修改为 no,增加对目录文件软连接的备份
read only = no # 设置 rsync 服务端为读写权限
list = no # 不显示 rsync 服务端资源列表
max connections = 200 # 最大连接数
timeout = 600 # 设置超时时间
auth users = admin # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
#hosts allow = 172.16.12.128 # 允许进行数据同步的客户端 IP 地址,可以设置多个,用英文状态下逗号隔开
#hosts deny = 192.168.1.1 # 禁止数据同步的客户端 IP 地址,可以设置多个,用英文状态下逗号隔开
// 以上注释在实际使用中需要删除掉,不然可能会出问题
// 创建用户生成文件
[root@master ~]# echo 'admin:123456' > /etc/rsync.pass
// 修改文件权限,只有管理员可读可写
[root@master ~]# chmod 600 /etc/rsync.pass
[root@master ~]# ll /etc/rsync.pass
-rw-------. 1 root root 13 Sep 22 18:07 /etc/rsync.pass
// 设置 rsync 开机自启
[root@master ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 128 [::]:22 [::]:*
源主机操作:
// 关闭防火墙与 selinux
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
// 配置 epel 源
[root@localhost ~]# dnf -y install epel-release
// 安装 rsync,只需要安装,不要启动,不需要配置
[root@localhost ~]# dnf -y install rsync rsync-daemon
// 创建用户认证密码文件
[root@localhost ~]# echo '123456' > /etc/rsync.pass
// 修改文件权限,只有管理员可读可写
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 Sep 22 18:15 /etc/rsync.pass
// 在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@localhost ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'
[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@10.10.100.101::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_879-4013723248/
deleting vmware-root_875-4022308853/
deleting vmware-root_873-4013854327/
deleting vmware-root_866-2722763301/
deleting vmware-root_860-2722763268/
deleting vmware-root_839-3979774022/
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
./
test/
sent 77 bytes received 285 bytes 724.00 bytes/sec
total size is 0 speedup is 0.00
// 运行完成后,在目标服务器上查看,在 /tmp 目录下有 test 目录,说明数据同步成功
[root@master ~]# ls /tmp/
test
// 安装 inotify-tools 工具,实时触发 rsync 进行同步
[root@localhost ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Sep 22 18:42 max_queued_events
-rw-r--r--. 1 root root 0 Sep 22 18:42 max_user_instances
-rw-r--r--. 1 root root 0 Sep 22 18:42 max_user_watches
// 如果有这三个 max 开头的文件则表示服务器内核支持 inotify
// 安装 inotify-tools
[root@localhost ~]# dnf -y install inotify-tools
// 写同步脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch inotify.sh
[root@localhost scripts]# chmod +x inotify.sh
[root@localhost scripts]# ll
total 0
-rwxr-xr-x. 1 root root 0 Sep 22 18:45 inotify.sh
[root@localhost scripts]# vim inotify.sh
#!/bin/bash
host=10.10.100.101
src=/etc
des=etc_from_client
password=/etc/rsync.pass
user=admin
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
// 启动脚本
[root@localhost scripts]# nohup bash /scripts/inotify.sh &
[1] 11489
[root@localhost scripts]# nohup: ignoring input and appending output to 'nohup.out'
[root@localhost scripts]# ps -efl | grep inotify | grep -v grep
0 S root 11489 11107 0 80 0 - 3174 - 18:49 pts/0 00:00:00 bash /scripts/inotify.sh
4 S root 11490 11489 0 80 0 - 1676 core_s 18:49 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
1 S root 11491 11489 0 80 0 - 3174 - 18:49 pts/0 00:00:00 bash /scripts/inotify.sh
// 在目标服务器上查看
root@master ~]# ls /tmp/etc/
………………
// 在源服务器上生成一个新文件
[root@localhost ~]# echo "hello world" > /etc/abc
[root@localhost ~]# cat /etc/abc
hello world
// 在目标服务器上面进行查看
[root@master ~]# cat /tmp/etc/abc
hello world
设置脚本开机自动启动:
[root@localhost ~]# vim /etc/rc.local
追加以下内容
nohup bash /scripts/inotify.sh &
[root@localhost ~]# vim /etc/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
微信扫描下方的二维码阅读本文
正文完