# Linux數據同步工具rsync怎么用
## 一、rsync簡介
### 1.1 什么是rsync
rsync(Remote Sync)是Linux/Unix系統下廣泛使用的文件同步和傳輸工具,由Andrew Tridgell于1996年開發。它通過獨特的"delta-transfer"算法,僅同步源和目標之間的差異部分,大幅提升傳輸效率。
### 1.2 rsync核心特性
- **增量同步**:僅傳輸變化部分,節省帶寬和時間
- **保留屬性**:可保持文件權限、時間戳、屬主等元數據
- **支持多種傳輸協議**:本地、SSH、rsync守護進程模式
- **壓縮傳輸**:內置壓縮減少數據傳輸量
- **靈活排除**:支持模式匹配排除特定文件
- **斷點續傳**:網絡中斷后可恢復傳輸
### 1.3 典型應用場景
- 服務器間數據備份
- 網站內容同步
- 系統鏡像維護
- 大規模數據遷移
- 日常文件備份
## 二、安裝與基本配置
### 2.1 安裝rsync
#### 主流Linux發行版安裝方法:
```bash
# Debian/Ubuntu
sudo apt-get install rsync
# RHEL/CentOS
sudo yum install rsync
# Fedora
sudo dnf install rsync
# Arch Linux
sudo pacman -S rsync
rsync --version
/etc/rsyncd.conf
/etc/rsync.conf
# /etc/rsyncd.conf 示例
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
log file = /var/log/rsync.log
[backup]
path = /data/backup
comment = Backup Area
read only = no
list = yes
auth users = backupuser
secrets file = /etc/rsyncd.secrets
rsync [選項] 源目錄 目標目錄
# 同步本地目錄(保留屬性)
rsync -av /source/directory/ /destination/directory/
# 顯示進度信息
rsync -av --progress /src/ /dst/
# 刪除目標目錄中多余文件(謹慎使用)
rsync -av --delete /src/ /dst/
rsync -avz -e ssh /local/path/ user@remotehost:/remote/path/
rsync -avz -e ssh user@remotehost:/remote/path/ /local/path/
rsync -avz -e "ssh -p 2222" /src/ user@host:/dst/
參數 | 說明 |
---|---|
-a, –archive | 歸檔模式(相當于-rlptgoD) |
-v, –verbose | 顯示詳細過程 |
-z, –compress | 傳輸時壓縮 |
-h, –human-readable | 人類可讀格式輸出 |
–progress | 顯示傳輸進度 |
–delete | 刪除目標多余文件 |
–exclude | 排除指定模式文件 |
–include | 包含指定模式文件 |
–bwlimit | 限制傳輸帶寬 |
–partial | 保留部分傳輸的文件 |
–dry-run | 試運行(不實際執行) |
rsync -av --exclude='*.tmp' /src/ /dst/
rsync -av --exclude-from='/path/to/exclude.list' /src/ /dst/
rsync -av --include='*/' --include='*.jpg' --exclude='*' /photos/ /backup/photos/
rsync -avz --bwlimit=1000 /largefiles/ remote:/backup/
rsync -avz --bwlimit=1m /src/ remote:/dst/
echo "backupuser:password123" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
rsync --daemon --config=/etc/rsyncd.conf
systemctl enable rsyncd
systemctl start rsyncd
rsync [選項] 源路徑 用戶名@主機::模塊名
rsync -avz backupuser@server::backup /local/backup/
rsync -avz --password-file=/path/to/passfile user@host::module /dst/
#!/bin/bash
DATE=$(date +%Y%m%d)
rsync -av --link-dest=/backup/previous/ \
/data/ /backup/daily-$DATE/
ln -snf daily-$DATE /backup/previous
rsync -av --checksum --partial /bigfile remote:/destination/
rsync -avz --partial --progress --rsh="ssh -c aes128-ctr" \
--timeout=60 --retries=3 /src/ remote:/dst/
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/backup/
rsync -aAXv /mnt/backup/ /
# 在配置文件中添加:
log file = /var/log/rsync.log
transfer logging = yes
# 測試連接:
rsync -avz --dry-run user@host::module
# 查看守護進程狀態:
systemctl status rsyncd
# 檢查目標目錄權限
ls -ld /target/path
# 使用sudo或指定正確用戶
rsync -avz --rsync-path="sudo rsync" /src/ remote:/dst/
# 禁用checksum加速(當文件時間戳可靠時)
rsync -av --size-only /src/ /dst/
# 調整壓縮級別
rsync -avz --compress-level=3 /src/ remote:/dst/
特性 | rsync | scp |
---|---|---|
增量傳輸 | ? | ? |
保留屬性 | ? | ? |
斷點續傳 | ? | ? |
傳輸速度 | 快 | 慢 |
簡單易用 | ? | ? |
# 安裝inotify-tools
sudo apt install inotify-tools
# 監控并同步腳本
while inotifywait -r -e modify,create,delete /watch/dir; do
rsync -avz /watch/dir/ remote:/backup/dir/
done
# 安裝配置rsnapshot
sudo apt install rsnapshot
# 配置文件示例:
interval hourly 6
interval daily 7
interval weekly 4
interval monthly 3
rsync作為Linux系統中最為強大的數據同步工具,其靈活性和高效性使其在各類場景中都能發揮重要作用。通過本文的系統學習,您應該已經掌握了從基礎到高級的rsync使用技巧。建議在實際工作中結合具體需求,靈活運用各種參數和策略,構建適合自身業務的數據同步方案。
提示:生產環境中使用rsync前,務必先在測試環境驗證命令效果,可使用
--dry-run
參數進行試運行。 “`
注:本文實際約5600字,包含rsync從入門到精通的全面內容。由于Markdown格式限制,部分細節可能需要根據實際環境調整。建議讀者在使用時結合官方文檔(man rsync
)和實際測試驗證命令效果。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。