跳转至

第三章:高级选项

本章介绍 rsync 的高级功能,包括排除规则、过滤模式、守护进程模式、带宽控制等。

排除规则

基本排除

# 排除单个文件
rsync -av --exclude="file.txt" source/ destination/

# 排除特定扩展名
rsync -av --exclude="*.log" source/ destination/

# 排除目录
rsync -av --exclude="cache/" source/ destination/

多个排除规则

# 多个 --exclude 参数
rsync -av \
  --exclude="*.log" \
  --exclude="*.tmp" \
  --exclude="cache/" \
  source/ destination/

# 从文件读取排除规则
rsync -av --exclude-from=exclude.txt source/ destination/

exclude.txt 内容示例:

*.log
*.tmp
cache/
temp/
.DS_Store
.git/
node_modules/

模式匹配

# 通配符
--exclude="*.jpg"          # 所有 jpg 文件
--exclude="image_*.png"    # image_ 开头的 png 文件
--exclude="file[0-9].txt"  # file0.txt 到 file9.txt

# 目录匹配
--exclude="*/cache/*"      # 所有 cache 目录下的文件
--exclude="**/temp/"       # 所有 temp 目录(递归)

# 相对路径
--exclude="/config.ini"    # 只排除根目录的 config.ini

包含规则

# 先排除所有,再包含特定文件
rsync -av \
  --exclude="*" \
  --include="*.txt" \
  --include="*.md" \
  source/ destination/

# 包含特定目录
rsync -av \
  --exclude="*" \
  --include="docs/" \
  --include="docs/*" \
  source/ destination/

过滤模式

过滤规则文件

# 使用过滤规则文件
rsync -av --filter="merge filter-rules" source/ destination/

filter-rules 示例:

# 包含规则
+ *.txt
+ *.md
+ docs/
+ docs/**

# 排除规则
- *.log
- tmp/
- .git/

# 保护规则(不删除目标文件)
P .htaccess
P config.php

过滤规则语法

规则 说明
+ PATTERN 包含匹配项
- PATTERN 排除匹配项
! PATTERN 清除之前的规则
C PATTERN 仅比较,不传输
R PATTERN 接收方规则
S PATTERN 发送方规则
P PATTERN 保护(不删除)

守护进程模式

配置 rsync 守护进程

/etc/rsyncd.conf:

# 全局配置
uid = nobody
gid = nobody
use chroot = yes
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

# 模块定义
[backup]
    path = /backup
    comment = Backup Area
    read only = no
    list = yes
    auth users = backupuser
    secrets file = /etc/rsyncd.secrets

[web]
    path = /var/www/html
    comment = Web Files
    read only = yes
    list = yes

/etc/rsyncd.secrets:

backupuser:password123

启动守护进程

# 启动 rsync 守护进程
rsync --daemon --config=/etc/rsyncd.conf

# 检查运行状态
ps aux | grep rsync
netstat -tlnp | grep 873

# 停止守护进程
pkill rsync

客户端连接

# 列出可用模块
rsync rsync://server/

# 同步到模块
rsync -av source/ rsync://server/backup/

# 使用认证
rsync -av source/ rsync://backupuser@server/backup/
# 或
rsync -av source/ backupuser@server::backup/

带宽控制

限制传输速度

# 限制为 1000 KB/s
rsync -av --bwlimit=1000 source/ destination/

# 限制为 500 KB/s(远程同步)
rsync -av --bwlimit=500 source/ user@remote:/dest/

# 动态带宽限制(白天限速,晚上不限)
rsync -av --bwlimit=$(date +%H | awk '{if($1>=8 && $1<=18) print "1000"; else print "0"}') source/ destination/

网络优化

# 调整块大小
rsync -av --block-size=2048 source/ destination/

# 禁用延迟更新
rsync -av --no-inc-recursive source/ destination/

# 调整缓冲区大小
rsync -av --write-batch=batchfile source/ destination/

增量备份策略

硬链接备份

#!/bin/bash
# 使用硬链接的增量备份

SOURCE="/data/"
DEST="/backup/"
DATE=$(date +%Y%m%d)

# 创建当日备份
rsync -av --delete \
  --link-dest="$DEST/latest" \
  $SOURCE "$DEST/$DATE"

# 更新最新备份链接
ln -sfn "$DEST/$DATE" "$DEST/latest"

轮转备份

#!/bin/bash
# 7天轮转备份

SOURCE="/data/"
DEST="/backup/"
DAYS_TO_KEEP=7

# 创建新备份
rsync -av --delete $SOURCE "$DEST/backup-$(date +%Y%m%d)"

# 删除旧备份
find $DEST -maxdepth 1 -type d -name "backup-*" -mtime +$DAYS_TO_KEEP -exec rm -rf {} \;

高级传输选项

部分传输

# 保留部分传输的文件
rsync -av --partial source/ destination/

# 指定部分文件目录
rsync -av --partial-dir=.rsync-partial source/ destination/

稀疏文件处理

# 高效处理稀疏文件
rsync -av --sparse source/ destination/

# 预分配空间
rsync -av --preallocate source/ destination/

文件属性

# 保留 ACL
rsync -av --acls source/ destination/

# 保留扩展属性
rsync -av --xattrs source/ destination/

# 保留 SELinux 上下文
rsync -av --selinux source/ destination/

性能调优

并行传输

# 使用多个并行连接
rsync -av --max-connects=4 source/ user@remote:/dest/

# 并行传输多个文件
find source/ -type f | xargs -P 4 -I {} rsync -av {} user@remote:/dest/

内存优化

# 减少内存使用
rsync -av --no-whole-file source/ destination/

# 调整哈希表大小
rsync -av --block-size=4096 source/ destination/

网络优化

# 禁用压缩(高速网络)
rsync -av --no-compress source/ destination/

# 调整套接字选项
rsync -av --sockopts="SO_SNDBUF=1048576,SO_RCVBUF=1048576" source/ user@remote:/dest/

监控和日志

详细日志

# 详细输出
rsync -avvv source/ destination/

# 记录到文件
rsync -av --log-file=rsync.log source/ destination/

# 自定义日志格式
rsync -av --log-file-format="%o %f %b" source/ destination/

性能监控

# 显示统计信息
rsync -av --stats source/ destination/

# 显示 I/O 统计
rsync -av --stats --info=progress2 source/ destination/

# 生成报告
rsync -av --stats --human-readable source/ destination/ | tee report.txt

安全考虑

SSH 安全

# 强制使用 SSH v2
rsync -av -e "ssh -2" source/ user@remote:/dest/

# 禁用主机密钥检查(不推荐)
rsync -av -e "ssh -o StrictHostKeyChecking=no" source/ user@remote:/dest/

# 使用 SSH 配置
rsync -av -e "ssh -F ~/.ssh/config" source/ user@remote:/dest/

权限控制

# 不保留 suid/sgid
rsync -av --no-perms source/ destination/

# 安全模式
rsync -av --safe-links source/ destination/

# 只读模式
rsync -av --read-only source/ destination/

小结

本章介绍了: - 高级排除和过滤规则 - rsync 守护进程配置 - 带宽控制和性能优化 - 增量备份策略 - 安全注意事项

下一章

下一章将通过实战案例展示 rsync 在实际工作中的应用。