rsync
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesersync 文件同步与备份
rsync 文件同步与备份
概述
概述
rsync 是强大的文件同步工具,支持增量传输、远程同步、备份等场景。
rsync 是强大的文件同步工具,支持增量传输、远程同步、备份等场景。
基础用法
基础用法
bash
undefinedbash
undefined本地同步
本地同步
rsync -av source/ dest/
rsync -av source/ dest/
远程同步(推送)
远程同步(推送)
rsync -av source/ user@remote:/path/dest/
rsync -av source/ user@remote:/path/dest/
远程同步(拉取)
远程同步(拉取)
rsync -av user@remote:/path/source/ dest/
rsync -av user@remote:/path/source/ dest/
常用参数
常用参数
-a 归档模式(保留权限、时间等)
-a 归档模式(保留权限、时间等)
-v 详细输出
-v 详细输出
-z 压缩传输
-z 压缩传输
-P 显示进度 + 断点续传
-P 显示进度 + 断点续传
-n 模拟运行(dry-run)
-n 模拟运行(dry-run)
undefinedundefined常用参数组合
常用参数组合
bash
undefinedbash
undefined标准备份
标准备份
rsync -avz source/ dest/
rsync -avz source/ dest/
带进度显示
带进度显示
rsync -avzP source/ dest/
rsync -avzP source/ dest/
删除目标多余文件(镜像同步)
删除目标多余文件(镜像同步)
rsync -avz --delete source/ dest/
rsync -avz --delete source/ dest/
排除文件
排除文件
rsync -avz --exclude='*.log' --exclude='.git' source/ dest/
rsync -avz --exclude='*.log' --exclude='.git' source/ dest/
使用排除文件
使用排除文件
rsync -avz --exclude-from='exclude.txt' source/ dest/
rsync -avz --exclude-from='exclude.txt' source/ dest/
限制带宽(KB/s)
限制带宽(KB/s)
rsync -avz --bwlimit=1000 source/ dest/
undefinedrsync -avz --bwlimit=1000 source/ dest/
undefined远程同步
远程同步
bash
undefinedbash
undefined通过 SSH(默认)
通过 SSH(默认)
rsync -avz -e ssh source/ user@host:/path/
rsync -avz -e ssh source/ user@host:/path/
指定 SSH 端口
指定 SSH 端口
rsync -avz -e 'ssh -p 2222' source/ user@host:/path/
rsync -avz -e 'ssh -p 2222' source/ user@host:/path/
使用 SSH 密钥
使用 SSH 密钥
rsync -avz -e 'ssh -i ~/.ssh/key' source/ user@host:/path/
rsync -avz -e 'ssh -i ~/.ssh/key' source/ user@host:/path/
rsync daemon 模式
rsync daemon 模式
rsync -avz source/ rsync://user@host/module/
undefinedrsync -avz source/ rsync://user@host/module/
undefined备份策略
备份策略
增量备份
增量备份
bash
undefinedbash
undefined使用硬链接实现增量备份
使用硬链接实现增量备份
rsync -avz --link-dest=/backup/latest source/ /backup/$(date +%Y%m%d)/
rsync -avz --link-dest=/backup/latest source/ /backup/$(date +%Y%m%d)/
更新 latest 链接
更新 latest 链接
ln -snf /backup/$(date +%Y%m%d) /backup/latest
undefinedln -snf /backup/$(date +%Y%m%d) /backup/latest
undefined定时备份脚本
定时备份脚本
bash
#!/bin/bash
set -euo pipefail
SOURCE="/data/"
DEST="/backup/"
DATE=$(date +%Y%m%d_%H%M%S)
LATEST="$DEST/latest"
BACKUP="$DEST/$DATE"bash
#!/bin/bash
set -euo pipefail
SOURCE="/data/"
DEST="/backup/"
DATE=$(date +%Y%m%d_%H%M%S)
LATEST="$DEST/latest"
BACKUP="$DEST/$DATE"增量备份
增量备份
rsync -avz --delete --link-dest="$LATEST" "$SOURCE" "$BACKUP"
rsync -avz --delete --link-dest="$LATEST" "$SOURCE" "$BACKUP"
更新 latest 链接
更新 latest 链接
ln -snf "$BACKUP" "$LATEST"
ln -snf "$BACKUP" "$LATEST"
保留最近 7 天
保留最近 7 天
find "$DEST" -maxdepth 1 -type d -mtime +7 -exec rm -rf {} ;
undefinedfind "$DEST" -maxdepth 1 -type d -mtime +7 -exec rm -rf {} ;
undefined常见场景
常见场景
场景 1:网站文件同步
场景 1:网站文件同步
bash
undefinedbash
undefined同步网站文件,排除缓存和日志
同步网站文件,排除缓存和日志
rsync -avz --delete
--exclude='cache/'
--exclude='*.log'
--exclude='uploads/tmp/'
/var/www/html/ backup@remote:/backup/www/
--exclude='cache/'
--exclude='*.log'
--exclude='uploads/tmp/'
/var/www/html/ backup@remote:/backup/www/
undefinedrsync -avz --delete
--exclude='cache/'
--exclude='*.log'
--exclude='uploads/tmp/'
/var/www/html/ backup@remote:/backup/www/
--exclude='cache/'
--exclude='*.log'
--exclude='uploads/tmp/'
/var/www/html/ backup@remote:/backup/www/
undefined场景 2:数据库备份同步
场景 2:数据库备份同步
bash
undefinedbash
undefined先导出数据库
先导出数据库
mysqldump -u root -p database > /backup/db.sql
mysqldump -u root -p database > /backup/db.sql
同步到远程
同步到远程
rsync -avzP /backup/db.sql backup@remote:/backup/mysql/
undefinedrsync -avzP /backup/db.sql backup@remote:/backup/mysql/
undefined场景 3:断点续传大文件
场景 3:断点续传大文件
bash
undefinedbash
undefined使用 -P 参数支持断点续传
使用 -P 参数支持断点续传
rsync -avzP large_file.tar.gz user@remote:/path/
rsync -avzP large_file.tar.gz user@remote:/path/
如果中断,重新执行相同命令即可继续
如果中断,重新执行相同命令即可继续
undefinedundefined故障排查
故障排查
| 问题 | 解决方法 |
|---|---|
| 权限错误 | 检查目标目录权限,使用 |
| 连接超时 | 检查网络、SSH 配置、防火墙 |
| 空间不足 | 清理目标磁盘,使用 |
| 同步慢 | 使用 |
| 文件被跳过 | 检查 |
| 问题 | 解决方法 |
|---|---|
| 权限错误 | 检查目标目录权限,使用 |
| 连接超时 | 检查网络、SSH 配置、防火墙 |
| 空间不足 | 清理目标磁盘,使用 |
| 同步慢 | 使用 |
| 文件被跳过 | 检查 |
注意事项
注意事项
bash
undefinedbash
undefined源路径末尾的 / 很重要!
源路径末尾的 / 很重要!
rsync -av source/ dest/ # 同步 source 目录内容到 dest
rsync -av source dest/ # 同步 source 目录本身到 dest/source
rsync -av source/ dest/ # 同步 source 目录内容到 dest
rsync -av source dest/ # 同步 source 目录本身到 dest/source
先用 -n 模拟
先用 -n 模拟
rsync -avzn --delete source/ dest/
rsync -avzn --delete source/ dest/
确认无误后执行
确认无误后执行
rsync -avz --delete source/ dest/
undefinedrsync -avz --delete source/ dest/
undefined