rsync 高效的文件同步工具

适用场景:数据备份、跨服务器同步、增量传输

简介

rsync 是一个快速、多功能的文件传输工具,支持增量传输、压缩等功能。它可以通过 SSH 或者通过 xinetd 启动的服务模式来同步文件。

核心优势

  • 增量传输:仅传输文件变化部分,减少带宽消耗。
  • 压缩数据:传输过程中自动压缩,节省网络流量。
  • 多平台支持:Linux、Windows、macOS 均可使用。
  • 安全性:通过 SSH 加密独立服务模式 确保数据安全。

安装rsync

1
2
3
4
5
# 检查是否已安装
rpm -qa | grep rsync || whereis rsync

# 未安装则执行
yum -y install rsync

服务端配置

方法一:xinetd 启动模式(低负载场景)

步骤

  1. 安装 xinetd:

xinetd 是一个超级服务器,可以用来启动 rsync 服务。

1
yum -y install xinetd
  1. 编辑 xinetd 配置文件 /etc/xinetd.d/rsync

确保包含以下内容:

1
2
3
4
5
6
7
8
9
10
service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon --config=/etc/rsyncd.conf
log_on_failure += USERID
}
  1. 配置 rsync 服务 /etc/rsyncd.conf

这是一个示例配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
##虚拟用户
uid = rsync
##虚拟用户组
gid = rsync
##端口号
port = 873
##伪装root权限
fake super = yes
##安全相关
use chroot = no
##最大链接数
max connections = 200
##超时时间
timeout = 300
##进程对应的进程号文件
pid file = /var/run/rsyncd.pid
##锁文件
lock file = /var/run/rsync.lock
##日志文件,显示出错信息
log file = /var/log/rsyncd.log
##忽略错误程序
ignore errors
##是否只读
read only = false
##是否可以列表
list = false
##准许访问rsync服务器的客户ip范围
hosts allow = 0.0.0.0/24
##禁止访问rsync服务器的客户ip范围
hosts deny = 0.0.0.0/24
##不存在的用户;只用于认证
auth users = rsync_backup
##设置进行连接认证的密匙文件
secrets file = /etc/rsync.password

##模块名称
[backup]
##模块对应的位置(路径)
path = /backup
##连接信息
comment = “backup dir by Jerry”
## 用户
auth users=work
## 是否只读
read only = false
1
touch /etc/rsyncd/rsyncd.motd         #非必须,连接上rsyncd显示的欢迎信息,此文件可不创建
  1. 设置密码文件

创建并编辑密码文件 /etc/rsync.password

1
2
3
4
5
6
7
8
## 格式--->用户名:口令,该用户不要求是系统用户
## 注意:该密码文件里面每行不要有空格,不然同步的时候一直报错auth failed on module
phper1021:123456
lamper1021:123456

echo "用户名:密码" > /etc/rsync.password
## 确保权限设置为 600:
chmod 600 /etc/rsync.password
  1. 启动rsync服务

xinetd 启动模式:适合低负载情况

1
2
3
4
5
6
7
8
9
10
11
## xinetd的rsync配置文件是/etc/xinetd.d/rsync,需要编辑此文件修改一个参数,显式的指定rsyncd服务的配置文件
vi /etc/xinetd.d/rsync
server_args = --daemon --config=/etc/rsyncd.conf

# 开机自启
chkconfig rsync on

# 启动
service xinetd restart
# or
systemctl restart xinetd

查看rsync服务是否启动

1
netstat -an | grep 873

方法二:独立模式(高负载场景)

独立启动模式:适合高负载情况

1
2
3
4
5
## 启动
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
## nohup 后台执行
nohup /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
## 为了保证开机时自动启动,需要手动加上面的命令(/usr/bin/rsync --daemon --config=/etc/rsyncd.conf)加入 /etc/rc.local 文件中

防火墙配置

确保防火墙允许 rsync 的默认端口 873

1
2
3
4
5
iptables -A INPUT -p tcp --dport 873 -j ACCEPT
/etc/init.d/iptables reload
# or
firewall-cmd --add-port=873/tcp --permanent
firewall-cmd --reload

关闭 SELinux

编辑 /etc/selinux/config 文件,禁用 SELinux:

1
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

rsync 的基本用法

命令格式

1
rsync [选项] 源路径 目标路径
  • 源路径:要同步的文件或目录。
  • 目标路径:同步的目标位置,可以是本地路径或远程路径。

常用选项

选项说明
-a归档模式,保留文件属性(权限、时间戳等)并递归同步目录,相当于-rlptgoD
-v显示详细输出。
-z压缩传输的数据,节省带宽。
-r递归同步目录。
-P显示传输进度,并支持断点续传。
-e指定远程 Shell(如 ssh)。
--delete删除目标中源不存在的文件(保持完全同步)。
--exclude排除指定的文件或目录。
--include包含指定的文件或目录。
-n--dry-run模拟运行,不实际执行同步。

rsync 的常见用法

本地文件同步

将本地目录同步到另一个目录:

1
2
# 同步目录内容(保留目录结构)
rsync -av /path/to/source/ /path/to/destination/
  • 注意:源路径末尾的 / 表示同步目录内容,而不是目录本身。

远程同步 SSH 方式

1
2
3
4
# 将本地文件同步到远程服务器:
rsync -avz -e ssh /path/to/source/ user@remote:/path/to/destination/
# 将远程服务器的文件同步到本地:
rsync -avz -e ssh user@remote:/path/to/source/ /path/to/destination/
  • -e ssh:使用 SSH 协议传输。

删除目标中多余的文件

1
2
# 保持目标与源完全一致,删除目标中源不存在的文件:
rsync -av --delete /path/to/source/ /path/to/destination/

排除特定文件或目录

1
2
3
4
# 排除 .log 文件和 dir2 目录
rsync -av --exclude='*.log' --exclude='dir2' /source/ /destination/
# 多个排除项(使用大括号语法)
rsync -av --exclude={'file.txt','dir1','*.tmp'} /source/ /destination/

增量备份

1
2
# 将文件同步到备份目录,并保留每次备份的版本:
rsync -av --backup --backup-dir=backup-$(date +%Y%m%d) /path/to/source/ /path/to/destination/

模拟运行

1
2
# 测试同步操作,不实际执行:
rsync -av --dry-run /path/to/source/ /path/to/destination/

通过模组进行传输

1
2
3
export RSYNC_PASSWORD="密码";rsync -av --exclude='*.log' 文件 用户名@IP::模块/
或者使用密码文件:
rsync --password-file=/etc/rsync.password -av --exclude='*.log' 文件地址 用户名@IP::模块/

通过 SSH 进行传输

使用 -e 参数指定 SSH 作为远程 shell,并提供必要的 SSH 选项(如密钥路径、端口号等)

1
rsync -av -e --exclude='*.log' "ssh -i 密钥 -p 端口" 文件地址 用户名@IP:远程路径/
  • 不需要在远程服务器上配置 rsync 服务端。
  • 利用了 SSH 的安全特性,确保数据传输的安全性。
  • 支持增量传输,只传输变化的部分,提高效率。
  • 确保 SSH 访问权限正确配置,特别是使用非默认端口或私钥认证时。
  • 如果文件量较大或者网络不稳定,考虑增加 --partial 选项以支持断点续传。

rsync 的高级用法

限速传输

限制传输带宽(单位为 KB/s):

1
rsync -av --bwlimit=1000 /path/to/source/ /path/to/destination/

部分传输

仅传输文件的一部分(适用于大文件):

1
rsync -av --partial /path/to/source/ /path/to/destination/

同步权限

同步文件权限和所有者信息:

1
rsync -av --chmod=ugo=rwX /path/to/source/ /path/to/destination/

同步符号链接

保留符号链接:

1
rsync -av -L /path/to/source/ /path/to/destination/

断点续传

支持断点续传:

1
rsync -avP /path/to/source/ /path/to/destination/

常见问题及解决方案

问题:防火墙阻止连接

确保防火墙规则正确配置,允许 rsync 默认端口 873 通过。

1
2
3
4
5
iptables -A INPUT -p tcp -m state --state NEW  -m tcp --dport 873 -j ACCEPT
/etc/init.d/iptables reload
# or
firewall-cmd --add-port=873/tcp --permanent
firewall-cmd --reload

问题:SELinux 影响同步

确认 SELinux 已被关闭或设置为宽松模式。

1
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

问题:部分文件同步失败

工作中遇到一部分rsync成功了,一部分超时了

检查 /etc/rsyncd.conf 中的最大连接数设置:

1
2
3
4
## /etc/rsyncd.conf  检查 /etc/rsyncd.conf 中的最大连接数设置:
max connections = 200
## /etc/xinetd.d/ 同时调整 xinetd 的并发连接限制:
cps = 60000 10

问题:SSH 连接问题

如果使用 SSH 连接远程服务器,确保 SSH 密钥配置正确,或者使用密码登录。

优先使用 -e ssh 方式,避免明文传输。

问题:路径问题

注意源路径末尾的 /,它会影响同步行为:

  • /:同步目录内容。
  • /:同步目录本身。

问题:断点续传

使用 -P 选项支持断点续传:

1
rsync -avzP /path/to/source/ user@remote:/path/to/destination/

问题:路径与权限

  • 路径末尾的 /
    • source/:同步目录内容。
    • source:同步整个目录(包括目录本身)。
  • 权限问题:
    • 确保目标路径有写入权限。
    • 使用 --chmod 显式设置权限:
1
rsync -av --chmod=ugo=rwX /source/ /destination/