# Linux命令-rsync远程/本地文件同步 —— 增量传输的备份与镜像神器 简介六种使用模式安装位置 语法⚙️ 选项核心选项必须掌握-a 归档模式的分解高级选项 示例示例 1本地目录同步基本用法示例 2推送到远程服务器示例 3从远程拉取到本地示例 4镜像同步删除目标多余文件示例 5文件过滤与排除示例 6限速与断点续传示例 7实际运维场景集合⚠️ 注意源路径末尾 / 的含义极易混淆rsync vs scp vs cp 对比常见陷阱与排查 总结 相关命令快速参考rsync是 Linux 系统中最强大的文件同步和传输工具。它采用独有的增量传输算法——只传输源和目标之间的差异部分极大节省带宽和时间。支持本地同步、远程推送/拉取、SSH 隧道传输、daemon 模式并具备断点续传、文件过滤、带宽限制、删除多余文件等生产级特性。是系统备份、文件分发、网站部署、数据迁移的首选工具。 简介rsync由 Andrew Tridgell 和 Paul Mackerras 开发其核心创新在于 “rsync 算法”将文件分块计算每块的校验和只传输源端和目标端不同的块。这意味着对一个 10GB 文件即使只改动了最后 1KB也只传输那 1KB。六种使用模式模式语法说明本地到本地rsync [选项] 源 目标同主机内目录/文件同步本地到远程rsync [选项] 源 用户主机:目标Push推送文件到远程远程到本地rsync [选项] 用户主机:源 目标Pull从远程拉取文件远程到远程rsync [选项] 用户主机A:源 用户主机B:目标通过本地中转Daemon 模式rsync [选项] 用户主机::模块/路径直接连接 rsync 服务SSH 隧道rsync [选项] -e ssh 源 目标通过 SSH 加密传输安装位置# 查看 rsync 路径和版本whichrsync# 输出/usr/bin/rsyncrsync--version|head-3# 输出# rsync version 3.1.2 protocol version 31# Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.# Web site: http://rsync.samba.org/# 安装大多数发行版默认安装# CentOS/RHELsudoyuminstallrsync# CentOS/RHEL 8sudodnfinstallrsync# Debian/Ubuntusudoaptinstallrsync# 查看配置文件路径ls/etc/rsyncd.conf2/dev/null||echodaemon 模式配置文件不存在需手动创建 语法rsync [选项] 源 目标源和目标格式格式说明/path/to/dir本地路径userhost:/path远程路径SSH 方式userhost::module/path远程路径daemon 方式rsync://userhost/module/pathURL 格式daemon 方式[USER]HOST::SRC [USER]HOST::DEST远程到远程本地中转⚙️ 选项核心选项必须掌握选项说明-a, --archive归档模式最常用等价于-rlptgoD-v, --verbose详细输出-z, --compress传输时压缩数据-P等价于--partial --progress断点续传 进度-h, --human-readable人类可读的大小格式如 1.5GB-n, --dry-run模拟运行预览将要进行的操作不实际传输-a 归档模式的分解子选项说明-r, --recursive递归目录-l, --links保留符号链接-p, --perms保留权限-t, --times保留修改时间-g, --group保留组-o, --owner保留所有者需 root-D等价于--devices --specials保留设备文件和特殊文件高级选项选项说明--delete⚠️ 小心使用删除目标端多余的文件镜像同步--delete-excluded同时删除被排除的文件--delete-before传输前先删除节省目标空间--exclude 模式排除匹配的文件/目录--exclude-from 文件从文件读取排除模式列表--include 模式包含匹配的文件/目录--bwlimit 速率限制传输带宽单位 KB/s如--bwlimit10000即 10MB/s-e, --rsh 命令指定远程 Shell如-e ssh -p 2222--partial保留未完整传输的文件断点续传基础--progress显示传输进度--stats传输完成后显示统计信息-x, --one-file-system不跨越文件系统边界-u, --update跳过目标端更新的文件--max-size 大小限制最大文件大小--min-size 大小限制最小文件大小--remove-source-files传输成功后删除源文件相当于移动--chmod 权限设置目标文件权限如--chmodD0755,F0644--log-file 文件将输出记录到日志文件 示例示例 1本地目录同步基本用法# 准备工作创建测试目录和文件mkdir-p/tmp/rsync_test/sourcecd/tmp/rsync_test/sourcemkdir-pdir1 dir2 dir3echoHello from file1file1.txtechoHello from file2file2.txtechoHello from file3dir1/file3.txtechoConfig dataconfig.conf# 查看源目录结构find/tmp/rsync_test/source-typef# 输出# /tmp/rsync_test/source/file1.txt# /tmp/rsync_test/source/file2.txt# /tmp/rsync_test/source/config.conf# /tmp/rsync_test/source/dir1/file3.txt# 基本同步保留目录结构、权限、时间戳rsync-av/tmp/rsync_test/source/ /tmp/rsync_test/dest/# 输出# sending incremental file list# ./# config.conf# file1.txt# file2.txt# dir1/# dir1/file3.txt# dir2/# dir3/## sent 558 bytes received 124 bytes 1,364.00 bytes/sec# total size is 58 speedup is 0.09# 验证目标目录ls-la/tmp/rsync_test/dest/# 输出# drwxr-xr-x 5 user group 100 Jun 14 14:30 .# drwxr-xr-x 4 user group 80 Jun 14 14:30 ..# -rw-r--r-- 1 user group 13 Jun 14 14:30 config.conf# drwxr-xr-x 2 user group 40 Jun 14 14:30 dir1# drwxr-xr-x 2 user group 40 Jun 14 14:30 dir2# drwxr-xr-x 2 user group 40 Jun 14 14:30 dir3# -rw-r--r-- 1 user group 18 Jun 14 14:30 file1.txt# -rw-r--r-- 1 user group 18 Jun 14 14:30 file2.txt示例 2推送到远程服务器# 将本地 /data/web/ 推送到远程服务器的 /var/www/rsync-avz/data/web/ userweb-server:/var/www/# 输出# sending incremental file list# ./# index.html# style.css# script.js# images/# images/logo.png# images/banner.jpg## sent 2,456,789 bytes received 156 bytes 491,389.00 bytes/sec# total size is 2,450,000 speedup is 1.00# 带进度条的推送推荐日常使用rsync-avzP/data/web/ userweb-server:/var/www/# 输出# sending incremental file list# ./# index.html# 3,456 100% 0.00kB/s 0:00:00 (xfr#1, to-chk4/6)# style.css# 12,789 100% 12.49MB/s 0:00:00 (xfr#2, to-chk3/6)# script.js# 45,678 100% 22.30MB/s 0:00:00 (xfr#3, to-chk2/6)# images/logo.png# 234,567 100% 19.12MB/s 0:00:00 (xfr#4, to-chk1/6)# images/banner.jpg# 2,160,345 100% 17.85MB/s 0:00:00 (xfr#5, to-chk0/6)## sent 2,458,912 bytes received 115 bytes 4,918,054.00 bytes/sec# total size is 2,450,000 speedup is 1.00# 测试大文件增量同步第二次传输几乎瞬间完成rsync-avh--stats/data/web/ userweb-server:/var/www/# 输出# sending incremental file list## Number of files: 6 (reg: 5, dir: 1)# Number of created files: 0# Number of deleted files: 0# Number of regular files transferred: 0 # 没有文件被传输# Total file size: 2.39M bytes# Total transferred file size: 0 bytes # 传输了 0 字节# Literal data: 0 bytes# Matched data: 0 bytes# File list size: 0# File list generation time: 0.001 seconds# File list transfer time: 0.000 seconds# Total bytes sent: 205# Total bytes received: 108## sent 205 bytes received 108 bytes 626.00 bytes/sec# total size is 2,450,000 speedup is 7,827.48示例 3从远程拉取到本地# 从远程服务器拉取数据rsync-avzuserdb-server:/var/backups/ /local/backups/# 输出# receiving incremental file list# ./# db_backup_20260601.sql.gz# db_backup_20260602.sql.gz# db_backup_20260603.sql.gz## sent 45 bytes received 125,678,901 bytes 8,378,596.40 bytes/sec# total size is 125,600,000 speedup is 1.00# 拉取并显示进度rsync-avzPuserdb-server:/var/backups/db_backup_20260614.sql.gz /local/backups/# 输出# receiving incremental file list# db_backup_20260614.sql.gz# 125,678,901 100% 35.67MB/s 0:00:03 (xfr#1, to-chk0/1)## sent 30 bytes received 125,722,456 bytes 25,144,497.20 bytes/sec# total size is 125,600,000 speedup is 1.00# 通过非标准 SSH 端口拉取rsync-avz-essh -p 2222userdb-server:/data/ /local/data/示例 4镜像同步删除目标多余文件# 准备工作目标端有多余文件mkdir-p/tmp/rsync_mirror/src /tmp/rsync_mirror/dstechokeep/tmp/rsync_mirror/src/keep.txtechoremove me/tmp/rsync_mirror/dst/old_file.txtechokeep/tmp/rsync_mirror/dst/keep.txt# 查看同步前状态ls-la/tmp/rsync_mirror/src/# 输出keep.txtls-la/tmp/rsync_mirror/dst/# 输出keep.txt old_file.txt# 预览镜像同步效果--dry-run 模拟rsync-av--delete--dry-run /tmp/rsync_mirror/src/ /tmp/rsync_mirror/dst/# 输出# sending incremental file list# deleting old_file.txt # 会删除# ./## sent 105 bytes received 13 bytes 236.00 bytes/sec# 执行镜像同步rsync-av--delete/tmp/rsync_mirror/src/ /tmp/rsync_mirror/dst/# 输出# sending incremental file list# deleting old_file.txt# ./## sent 110 bytes received 15 bytes 250.00 bytes/sec# 验证ls-la/tmp/rsync_mirror/dst/# 输出keep.txtold_file.txt 已被删除# ⚠️ 目标端与源端完全一致# 生产环境中使用前务必先 --dry-runrsync-avz--delete--dry-run /production/data/ backup-server:/backup/data/# 确认无误后再执行rsync-avz--delete/production/data/ backup-server:/backup/data/示例 5文件过滤与排除# 准备工作mkdir-p/tmp/rsync_filter/projectcd/tmp/rsync_filter/projectmkdir-psrc dist node_modules .git logsechosourcesrc/app.jsechobuilddist/bundle.jsechomodulesnode_modules/module.jsechogit.git/HEADechologlogs/app.logechologlogs/error.logtouchREADME.md package.json# 排除 node_modules 和 .git 目录rsync-av--excludenode_modules/--exclude.git//tmp/rsync_filter/project/ /tmp/rsync_filter/backup/# 输出注意 node_modules 和 .git 未出现# sending incremental file list# ./# README.md# package.json# dist/# dist/bundle.js# logs/# logs/app.log# logs/error.log# src/# src/app.js## sent 456 bytes received 78 bytes 1,068.00 bytes/sec# 排除所有 .log 文件rsync-av--exclude*.log/tmp/rsync_filter/project/ /tmp/rsync_filter/backup2/# 输出无 log 文件# ./# README.md# package.json# dist/# dist/bundle.js# src/# src/app.js# 使用排除文件列表适合复杂过滤规则cat/tmp/rsync_exclude.txtEOF node_modules/ .git/ *.log dist/ .DS_Store Thumbs.db *.swp *~ EOFrsync-av--exclude-from/tmp/rsync_exclude.txt\/tmp/rsync_filter/project/ /tmp/rsync_filter/backup3/# 输出# ./# README.md# package.json# src/# src/app.js示例 6限速与断点续传# 限速 10MB/s防止占满带宽影响其他服务rsync-avzP--bwlimit10000/data/large_files/ userremote:/backup/# 输出# large_file.tar.gz# 2,147,483,648 10% 10.00MB/s 0:03:12 (xfr#1, to-chk3/4)# ^^^^^^^^^^^^ 稳定在 10MB/s# 断点续传中断后使用相同命令继续# 第一次传输模拟中断rsync-avP/data/huge_file.iso userremote:/backup/# huge_file.iso# 4,294,967,296 45% 50.00MB/s 0:00:38# ^C (用户中断CtrlC)# 重新执行相同命令自动续传rsync-avP/data/huge_file.iso userremote:/backup/# huge_file.iso# 4,294,967,296 45% 50.00MB/s 0:00:38# (从 45% 继续而非从 0% 开始)# 限速 非高峰时段打包传输脚本示例cat/usr/local/bin/nightly-sync.shSCRIPT #!/bin/bash # 夜间备份脚本限速 50MB/s记录日志 rsync -avzP \ --bwlimit50000 \ --log-file/var/log/rsync-backup.log \ --exclude*.tmp \ --exclude.cache/ \ /data/production/ \ backup-server:/nightly-backup/$(date %Y%m%d)/ echo $(date): Sync completed with exit code $? /var/log/rsync-backup.log SCRIPTchmodx /usr/local/bin/nightly-sync.sh示例 7实际运维场景集合# 场景1网站代码部署 rsync-avz--delete\--exclude.git/\--exclude.env\--excludestorage/\/local/project/\deployweb-server:/var/www/project/# 输出# sending incremental file list# deleting old-page.html# ./# app/Controllers/NewController.php# resources/views/# resources/views/welcome.blade.php# 场景2数据库备份同步 rsync-avzP--remove-source-files\/tmp/daily_backups/\archivenas-server:/archive/db/$(date%Y/%m)/# --remove-source-files 传输成功后删除本地文件相当于移动# 场景3Linux 系统迁移 # 将整个系统复制到新磁盘排除虚拟文件系统sudorsync-avxHAX--progress\--exclude{/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lostfound}\/ /mnt/new_disk/# -x: 不跨文件系统# -H: 保留硬链接# -A: 保留 ACL# -X: 保留扩展属性# 场景4跨服务器目录对比 # 使用 --dry-run 查看差异而不实际同步rsync-avz--dry-run --itemize-changes\/data/server_a/ userserver_b:/data/server_a/# 输出# f new_file.txt # 表示发送到远程# .d..t...... existing_dir/ # . 表示未传输仅属性变化# f.st...... modified_file.txt # s 大小变化t 时间变化# 场景5使用 daemon 模式 # 服务端 /etc/rsyncd.conf 配置示例cat/tmp/rsyncd_example.confEOF uid nobody gid nobody use chroot yes max connections 10 timeout 300 [backup] path /data/backup comment Backup Area read only no list yes auth users backupuser secrets file /etc/rsyncd.secrets [public] path /data/public comment Public Files read only yes list yes EOF# 客户端使用 daemon 模式拉取rsync-avrsync://backupusernas-server/backup/ /local/backup/# 或双冒号语法rsync-avbackupusernas-server::backup/ /local/backup/⚠️ 注意源路径末尾/的含义极易混淆# 准备测试环境mkdir-p/tmp/slash_test/src/datatouch/tmp/slash_test/src/data/file.txt# 不带末尾 /同步目录本身rsync-av/tmp/slash_test/src /tmp/slash_test/dest1/# 结果find/tmp/slash_test/dest1-typef# 输出/tmp/slash_test/dest1/src/data/file.txt# 创建了 src 目录本身# 带末尾 /同步目录内容rsync-av/tmp/slash_test/src/ /tmp/slash_test/dest2/# 结果find/tmp/slash_test/dest2-typef# 输出/tmp/slash_test/dest2/data/file.txt# 只同步了内容没有创建 src 目录# 记忆口诀# /src → 目标中创建 src/复制这个目录# /src/ → 目标中展开 src 的内容不创建 src 目录本身rsync vs scp vs cp 对比# scp安全复制# 适用简单的一次性文件传输# 缺点总是全量传输不支持增量scp-r/data/ userserver:/data/# 输出逐个文件复制不检查已有文件# cp本地复制# 适用本地简单复制# 缺点无增量、无远程、无过滤cp-a/src/ /dst/# rsync智能同步# 适用备份、镜像、部署、增量传输# 优势# 1. 增量传输只传差异# 2. 断点续传# 3. 文件过滤# 4. 删除同步# 5. 带宽限制# 6. 支持多种传输协议# 性能对比示例# scp传输修改了 1KB 的 100GB 文件 → 传输 100GB需要几个小时# rsync传输修改了 1KB 的 100GB 文件 → 传输 ~1KB几乎瞬间完成常见陷阱与排查# 陷阱1源路径缺少末尾 / 导致目录嵌套# 错误rsync-av/source dest-server:/backup/# 结果/backup/source/...创建了额外的 source 目录# 陷阱2--delete 误删重要文件# 始终先用 --dry-run 预览rsync-av--delete--dry-run /src/ dest-server:/dst/# 检查输出的 deleting ... 行确认无误后再去掉 --dry-run# 陷阱3权限问题# 传输到 root 拥有的目录时可能出错sudorsync-av--rsync-pathsudo rsync/src/ userserver:/root/dst/# 陷阱4大文件传输中断# 使用 -P 确保支持断点续传rsync-avP/large/file userserver:/path/# 陷阱5符号链接问题# -a 已包含 -l保留符号链接但如果需要复制实际文件rsync-avL--copy-links /src/ /dst/# -L 跟随并复制链接目标 总结rsync是 Linux 运维工具箱中最重要的传输工具之一核心优势增量传输 —— 只传输差异部分第二次同步几乎瞬间完成归档模式-a是日常使用的基础选项保留所有属性-P进度 断点续传在大文件或网络不稳定时至关重要--delete实现镜像同步但务必先用--dry-run预览路径末尾/决定是同步目录本身还是目录内容易混淆但极关键六种模式涵盖了从本地复制到远程 daemon 的所有场景掌握了rsync就掌握了 Linux 下最高效的数据传输方式。 相关命令命令说明scp基于 SSH 的简单安全文件复制全量cp本地文件复制mv移动/重命名文件tar归档工具常与 rsync 配合备份sshrsync 远程传输的底层隧道cron定时任务配合 rsync 实现周期性备份lsyncd基于 rsync inotify 的实时同步工具rclone云存储同步工具rsync 的云版本