# rsync 使用指南 🚀
rsync
是一个用于远程和本地同步文件的工具,支持增量传输、压缩以及权限保留。
# 1. 安装 rsync 🛠️
# 🐧 Linux(Ubuntu/Debian)
# 🍏 macOS
# 🪟 Windows(使用 WSL 或 Cygwin)
# 2. rsync 基本语法 📜
- 📂 本地同步:
rsync /source/ /destination/
- 🌍 远程推送:
rsync /source/ user@remote:/destination/
- ⬇️ 远程拉取:
rsync user@remote:/source/ /destination/
# 3. 常用选项 ⚙️
选项 | 说明 |
---|
-a | 📦 归档模式(保留权限、时间戳、符号链接等) |
-v | 🔍 显示详细信息 |
-z | 🗜️ 传输时压缩数据 |
-P | ⏳ 显示进度(等价于 --progress --partial ) |
--delete | 🗑️ 目标目录中没有的文件会被删除 |
-e "ssh -p PORT" | 🔐 指定远程 SSH 端口 |
--exclude="pattern" | 🚫 排除某些文件 |
--include="pattern" | ✅ 只包含某些文件 |
-n | 🛑 预览模式,不执行同步 |
# 4. 典型使用示例 🏗️
# 4.1 📂 本地目录同步
| rsync -av /home/user/docs/ /backup/docs/ |
# 4.2 🌍 远程推送(本地 → 远程服务器)
| rsync -avz /home/user/docs/ user@remote:/backup/docs/ |
# 4.3 ⬇️ 远程拉取(远程服务器 → 本地)
| rsync -avz user@remote:/backup/docs/ /home/user/docs/ |
# 4.4 🔐 指定 SSH 端口
| rsync -avz -e "ssh -p 2222" /home/user/docs/ user@remote:/backup/docs/ |
# 4.5 🗑️ 删除目标目录多余文件
| rsync -avz --delete /home/user/docs/ user@remote:/backup/docs/ |
# 4.6 🔄 仅同步修改过的文件
| rsync -avzu /home/user/docs/ user@remote:/backup/docs/ |
# 4.7 ⏸️ 断点续传
| rsync -avz --partial --progress /home/user/docs/ user@remote:/backup/docs/ |
# 4.8 🎯 仅同步特定类型的文件
| rsync -avz --include="*.txt" --exclude="*" /home/user/docs/ user@remote:/backup/docs/ |
# 4.9 🚫 排除特定文件
| rsync -avz --exclude="*.log" /home/user/docs/ user@remote:/backup/docs/ |
# 5. 配合 cron
定时备份 ⏰
编辑 crontab
添加自动同步任务:
添加以下内容,每天凌晨 2 点自动同步:
| 0 2 * * * rsync -avz --delete /home/user/docs/ user@remote:/backup/docs/ |
# 6. 高级技巧 🏆
# 6.1 🚀 多线程加速
| rsync -avz --info=progress2 --compress-level=1 /home/user/docs/ user@remote:/backup/docs/ |
# 6.2 🎛️ 限制 IO 负载(减少系统资源占用)
| ionice -c2 -n7 rsync -avz /home/user/docs/ user@remote:/backup/docs/ |
# 7. 总结 📌
功能 | 命令 |
---|
📂 本地同步 | rsync -av /source/ /destination/ |
🌍 远程推送 | rsync -avz /source/ user@remote:/destination/ |
⬇️ 远程拉取 | rsync -avz user@remote:/source/ /destination/ |
🔐 指定 SSH 端口 | rsync -avz -e "ssh -p 2222" /source/ user@remote:/destination/ |
⏸️ 断点续传 | rsync -avz --partial --progress /source/ user@remote:/destination/ |
🗑️ 删除多余文件 | rsync -avz --delete /source/ user@remote:/destination/ |
🔄 仅同步修改文件 | rsync -avzu /source/ user@remote:/destination/ |
🎯 仅同步 .txt 文件 | rsync -avz --include="*.txt" --exclude="*" /source/ user@remote:/destination/ |
🚫 排除 .log 文件 | rsync -avz --exclude="*.log" /source/ user@remote:/destination/ |
⏰ 定时任务 | 0 2 * * * rsync -avz --delete /source/ user@remote:/destination/ |
🎉 希望这个指南对你有帮助! 🚀