萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

rsync 排除文件/目錄完整指南:--exclude 與 --exclude-from 實戰詳解

核心問題:如何同步時跳過指定檔案或目錄

參考:slicehost.com

基本用法:--exclude

同步 / 下 /usr /boot,排除 /proc /tmp:

rsync -av --exclude "proc" --exclude "tmp" /source/ /dest/

關鍵規則:路徑必須為相對路徑,相對於來源根目錄。
錯誤示範: --exclude "/checkout"(絕對路徑不生效)。

實戰範例

排除單一目錄/檔案

# 源端 /home/yjwan/bashshell 下有 checkout 目錄
rsync -av --exclude "checkout" yjwan@172.16.251.241:/home/yjwan/bashshell /tmp
# 目標端 /tmp/bashshell 不會有 checkout

排除目錄內特定檔案

--exclude "checkout/123"

通配符排除

--exclude "fire*"  # 所有 fire 開頭的檔案/目錄

進階:--exclude-from 檔案列表

排除項目過多時,寫入檔案 /exclude.list(絕對路徑),內容每行一個相對路徑:

checkout
fire*
downloads/test/*

執行:

rsync -av --exclude-from="/exclude.list" yjwan@172.16.251.241:/home/yjwan/bashshell /tmp

驗證同步數量是否正確

  1. 查看錯誤日誌。
  2. 源端統計應同步總數:
    rsync -av /source/ | grep "^-" | wc -l(或 ls -AlR | grep "^-" | wc -l)
  3. 加上 exclude 後統計:
    rsync -av --exclude "fire*" /source/ | grep "^-" | wc -l
  4. 目標端統計對比,數字一致即同步完整。

其他常用參數

  • -z, --compress:傳輸壓縮(內網小檔案可不必)
  • --password-file:僅適用於 rsync daemon 模式,非 SSH 密碼
  • --stats:顯示傳輸統計
  • --progress:顯示每檔進度,大檔案傳輸極實用
  • -P:等同 --partial --progress,支援斷點續傳與進度顯示
copyright © 萬盛學電腦網 all rights reserved