SuperAPI AI Directory

rsync

General & other

View repository

Updated 10-15 17:33

About this skill

rsync file synchronization and backup

Overview

rsync is a powerful file synchronization tool that supports incremental transfer, remote synchronization, backup, and other scenarios.

Basic Usage

# 本地同步
rsync -av source/ dest/

# 远程同步(推送)
rsync -av source/ user@remote:/path/dest/

# 远程同步(拉取)
rsync -av user@remote:/path/source/ dest/
Common Options -a archive mode (preserve permissions, times, etc.) -v verbose output -z compressed transfer -P show progress + resume interrupted transfers -n simulate run (dry-run)

## 常用参数组合

```bash
# 标准备份
rsync -avz source/ dest/

# 带进度显示
rsync -avzP source/ dest/

# 删除目标多余文件(镜像同步)
rsync -avz --delete source/ dest/

# 排除文件
rsync -avz --exclude='*.log' --exclude='.git' source/ dest/

# 使用排除文件
rsync -avz --exclude-from='exclude.txt' source/ dest/

# 限制带宽(KB/s)
rsync -avz --bwlimit=1000 source/ dest/

Remote Synchronization

# 通过 SSH(默认)
rsync -avz -e ssh source/ user@host:/path/

# 指定 SSH 端口
rsync -avz -e 'ssh -p 2222' source/ user@host:/path/

# 使用 SSH 密钥
rsync -avz -e 'ssh -i ~/.ssh/key' source/ user@host:/path/

# rsync daemon 模式
rsync -avz source/ rsync://user@host/module/

Backup Strategies

Incremental Backup

# 使用硬链接实现增量备份
rsync -avz --link-dest=/backup/latest source/ /backup/$(date +%Y%m%d)/

# 更新 latest 链接
ln -snf /backup/$(date +%Y%m%d) /backup/latest

Scheduled Backup Script

#!/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"

# 更新 latest 链接
ln -snf "$BACKUP" "$LATEST"

# 保留最近 7 天
find "$DEST" -maxdepth 1 -type d -mtime +7 -exec rm -rf {} \;

Common Scenarios

Scenario 1: Website File Synchronization

# 同步网站文件,排除缓存和日志
rsync -avz --delete \
  --exclude='cache/' \
  --exclude='*.log' \
  --exclude='uploads/tmp/' \
  /var/www/html/ backup@remote:/backup/www/

Scenario 2: Database Backup Synchronization

# 先导出数据库
mysqldump -u root -p database > /backup/db.sql

# 同步到远程
rsync -avzP /backup/db.sql backup@remote:/backup/mysql/

Scenario 3: Resuming Interrupted Transfers of Large Files

# 使用 -P 参数支持断点续传
rsync -avzP large_file.tar.gz user@remote:/path/

# 如果中断,重新执行相同命令即可继续

Troubleshooting

Problem Solution
Permission error Check the target directory permissions, use --chmod
Connection timeout Check the network, SSH configuration, firewall
Insufficient space Clean up the target disk, use --max-size limit
Slow synchronization Use -z compression,--bwlimit rate limiting
Files skipped Check --exclude rules, use -v to view details

Notes

# 源路径末尾的 / 很重要!
rsync -av source/ dest/   # 同步 source 目录内容到 dest
rsync -av source dest/    # 同步 source 目录本身到 dest/source

# 先用 -n 模拟
rsync -avzn --delete source/ dest/

# 确认无误后执行
rsync -avz --delete source/ dest/