Rsync 数据同步工具及实时同步配置
Rsync 数据同步工具及实时同步配置
一、Rsync 简介
Rsync 是 Linux 系统下的快速增量数据镜像备份工具,支持本地复制、远程同步(通过 SSH 或 Rsync 协议),能高效实现数据备份与镜像。
二、Rsync 核心特性
- 完整镜像:支持目录树和文件系统的完整镜像备份
- 属性保留:保持文件权限、时间、软硬链接等元数据
- 无需特殊权限:普通用户即可安装使用
- 高效传输:首次全量复制,后续仅传输增量数据;支持压缩传输,节省带宽
- 安全灵活:可通过 SSH 协议加密传输,也支持匿名传输
- 广泛适用:适用于本地备份、远程同步、网站镜像等场景
三、Rsync 认证与常用命令
1. 认证协议
- SSH 协议:依赖 SSH 进行身份验证,无需单独启动 Rsync 服务,与
scp
原理类似 - Rsync 协议:需配置 Rsync 服务端(
rsyncd.conf
),通过专用端口(873)通信
2. 常用命令格式
# 本地文件复制
rsync [选项] 源路径 目标路径# 本地到远程(SSH协议)
rsync [选项] 源路径 用户@远程IP:目标路径# 远程到本地(SSH协议)
rsync [选项] 用户@远程IP:源路径 目标路径
3. 核心选项
-a
:归档模式(递归传输并保留所有属性)-v
:显示详细输出-z
:传输时压缩数据-r
:递归传输目录-p
:保留文件权限--delete
:删除目标端不存在的源端文件(保持完全一致)-e "ssh -p 端口"
:指定 SSH 端口(默认 22)
4. 示例
# SSH协议同步本地目录到远程(默认端口)
[root@zhangyiwei3 /]# rsync -avz /data root@192.168.100.20:/text
The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established.
ECDSA key fingerprint is SHA256:dxe4J1VoUg7jQYq4ZRvDw2HLfOzu2zjP7bdX6m8J5Is.
ECDSA key fingerprint is MD5:4a:aa:28:8c:2c:62:8b:68:52:e3:c0:3d:ae:0a:b5:e1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.
root@192.168.100.20's password:
sending incremental file list
rsync: link_stat "/data" failed: No such file or directory (2)sent 18 bytes received 12 bytes 1.09 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]# 指定SSH端口(如2222)
rsync -avz /data -e "ssh -p 2222" root@192.168.100.20:/text# 同步远程文件到本地
rsync -avz root@192.168.100.20:/var/log/messages /tmp/
四、Rsync + Inotify 实时同步方案
1. 方案优势
- Rsync 不足:需手动触发或定时执行,无法实时同步;大量文件时扫描耗时
- Inotify 补充:Linux 内核提供的文件系统事件监控机制,可实时捕捉文件创建、修改、删除等事件
- 组合效果:Inotify 监控文件变化,触发 Rsync 实时同步,兼顾效率与实时性
2. 环境准备
角色 | IP 地址 | 安装软件 | 操作系统 |
---|---|---|---|
源服务器 | 192.168.100.20 | rsync、inotify-tools | CentOS 7 |
目标服务器 | 192.168.100.30 | rsync | CentOS 7 |
需求:将源服务器 /root/etc
目录实时同步到目标服务器 /tmp
目录
时钟同步配置
源服务器:
[root@zhangyiwei-2 ~]# vim /etc/chrony.conf
添加行:local stratum 10
( allow 192.168.100.0/24 可添加,只允许 192,168。100.0/24 网段的主机同步本服务器时间)[root@zhangyiwei-2 ~]# systemctl restart chronyd && systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@zhangyiwei-2 ~]# hwclock -w目标服务器:
[root@zhangyiwei3 ~]# vim /etc/chrony.conf
修改如下:
s#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.100.20 iburst[root@zhangyiwei3 ~]# systemctl restart chronyd && systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@zhangyiwei3 ~]# hwclock -w
[root@zhangyiwei3 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.100.20 0 7 0 - +0ns[ +0ns] +/- 0ns
3. 目标服务器配置(192.168.100.30)
(1)基础配置
# 关闭防火墙和SELinux
[root@zhangyiwei3 /]# systemctl stop firewalld && systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[root@zhangyiwei3 /]# setenforce 0
[root@zhangyiwei3 /]# sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config# 配置yum源
[root@zhangyiwei3 ~]# cd /etc/yum.repos.d/
[root@zhangyiwei3 ~]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
[root@zhangyiwei3 ~]# rm -rf *
[root@zhangyiwei3 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 2523 100 2523 0 0 13235 0 --:--:-- --:--:-- --:--:-- 13278
[root@zhangyiwei3 ~]# ls
CentOS-Base.repo epel.repo epel-testing.repo
[root@zhangyiwei3 yum.repos.d]# yum -y install epel-release
已加载插件:fastestmirror, langpacks********省略********
完毕!# 安装rsync
[root@zhangyiwei3 ~]# yum -y install rsync
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile*****省略*****更新完毕:
rsync.x86_64 0:3.1.2-12.el7_9
完毕!
(2)配置 Rsync 服务
# 创建配置文件
[root@zhangyiwei3 ~]# vim /etc/rsyncd.conflog file = /var/log/rsyncd.log # 日志文件
pidfile = /var/run/rsyncd.pid # PID文件
lock file = /var/run/rsync.lock # 锁文件
secrets file = /etc/rsync.pass # 认证文件
[etc_from_client] # 模块名(自定义)
path = /tmp/ # 同步目标目录
comment = Sync etc from source
uid = root # 运行用户
gid = root # 运行组
port = 873 # 端口
ignore errors # 忽略错误
use chroot = no # 禁用chroot
read only = no # 允许写入
list = no # 不显示模块列表
max connections = 200 # 最大连接数
timeout = 600 # 超时时间
auth users = admin # 认证用户
hosts allow = 192.168.100.20 # 允许的源服务器IP# 创建认证文件(用户名:密码)
[root@zhangyiwei3 ~]# echo 'admin:redhat' > /etc/rsync.pass
[root@zhangyiwei3 ~]# cat /etc/rsync.pass
admin:redhat
[root@zhangyiwei3 ~]# chmod 600 /etc/rsync* # 限制权限
[root@zhangyiwei3 ~]# ll /etc/rsync*
-rw-------. 1 root root 1383 8月 20 21:14 /etc/rsyncd.conf
-rw-------. 1 root root 13 8月 20 21:15 /etc/rsync.pass# 启动服务并设置开机自启
[root@zhangyiwei3 ~]# rsync --daemon
[root@zhangyiwei3 ~]# echo 'rsync --daemon --config=/etc/rsyncd.conf' >> /etc/rc.d/rc.local
[root@zhangyiwei3 ~]# chmod +x /etc/rc.d/rc.local
[root@zhangyiwei3 ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 555 8月 20 21:17 /etc/rc.d/rc.local
4. 源服务器配置(192.168.100.20)
(1)基础配置
# 关闭防火墙和SELinux(同上)
[root@zhangyiwei-2 ~]# systemctl stop firewalld && systemctl disable firewalld
[root@zhangyiwei-2 ~]# setenforce 0 && sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config# 配置yum源并(同上)并安装依赖
[root@zhangyiwei2 /]# cd /etc/yum.repos.d/
[root@zhangyiwei2 ~]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
[root@zhangyiwei2 ~]# rm -rf *
[root@zhangyiwei2 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 2523 100 2523 0 0 13235 0 --:--:-- --:--:-- --:--:-- 13278
[root@zhangyiwei2 ~]# ls
CentOS-Base.repo epel.repo epel-testing.repo
[root@zhangyiwei2 yum.repos.d]# yum -y install epel-release
已加载插件:fastestmirror, langpacks********省略********
完毕![root@zhangyiwei-2 ~]# yum -y install inotify-tools rsync
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile*****省略******
已安装:inotify-tools.x86_64 0:3.14-9.el7
完毕!
(2)配置 Rsync 客户端
# 创建密码文件(仅需密码,与服务端对应)
[root@zhangyiwei-2 ~]# echo 'redhat' > /etc/rsync.pass
[root@zhangyiwei-2 ~]# cat /etc/rsync.pass
redhat
[root@zhangyiwei-2 ~]# chmod 600 /etc/rsync.pass
[root@zhangyiwei-2 ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 8月 20 21:19 /etc/rsync.pass# 创建测试目录
[root@zhangyiwei-2 ~]# mkdir -p /root/etc/test
(3)编写实时同步脚本
# 创建脚本目录并编写脚本
[root@zhangyiwei-2 ~]# mkdir /zhang3 && touch /zhang3/inotify.sh && chmod +x /zhang3/inotify.sh
[root@zhangyiwei-2 ~]# ll /zhang3/inotify.sh
-rwxr-xr-x. 1 root root 0 8月 20 21:24 /zhang3/inotify.sh[root@zhangyiwei-2 ~]# vim /zhang3/inotify.shhost=192.168.100.30 # 目标服务器IP
src=/root/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;dorsync -avzP --delete --timeout=100 --password-file=$password $src $user@$host::$desecho "${files} was synced" >> /tmp/rsync.log 2>&1
done
(4)启动脚本并设置开机自启
# 后台启动脚本
[root@zhangyiwei-2 ~]# nohup bash /zhang3/inotify.sh &
[1] 63253
nohup: 忽略输入并把输出追加到"nohup.out"# 设置开机自启
[root@zhangyiwei-2 ~]# echo 'nohup /bin/bash /zhang3/inotify.sh &' >> /etc/rc.d/rc.local
[root@zhangyiwei-2 ~]# chmod +x /etc/rc.d/rc.local
5. 验证实时同步
# 在源服务器创建文件
touch /root/etc/testfile.txt# 在目标服务器查看是否同步
ls /tmp/testfile.txt # 应显示同步的文件
五、总结
- Rsync 是高效的增量同步工具,适用于各种备份场景
- 通过 SSH 协议可快速实现简单同步,无需配置服务端
- Rsync + Inotify 组合实现实时同步,解决了定时同步的延迟问题
- 生产环境中需注意权限控制、日志监控和故障处理,确保数据一致性