Shell

使用 rsync 備份系統數據給出“沒有這樣的文件”

  • April 14, 2022

我正在嘗試使用以下腳本備份我的整個系統目錄:

#!/bin/bash

cd /

BACKUP_LOCATION=/media/data/backup/system-backup/

echo Backing up system data to $BACKUP_LOCATION, excluding home directories
sudo rsync -aAXv --exclude={"/home/*", "/swap.img", "/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / $BACKUP_LOCATION

執行此程序時,我收到以下錯誤:

sending incremental file list
rsync: [sender] link_stat "/swap.img," failed: No such file or directory (2)
rsync: [sender] change_dir "/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*," failed: No such file or directory (2)

據我了解,排除路徑是相對於源目錄的,即 /,因此 /home/* 應該解析為真實文件系統上的 /home/*。誰能告訴我我在這裡做錯了什麼?

執行命令時

sudo rsync -aAXv --exclude={"/home/*", "/swap.img", "/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / $BACKUP_LOCATION

支持大括號擴展的 shell 只會在包含正確格式的大括號表達式的單詞上執行它。您可以通過執行來測試命令的解釋方式,例如:

bash -xc 'rsync -aAXvn --exclude={"/home", "/swap.img", "/dev"} / /media/data/backup/system-backup/' 2>&1 | grep ^\+

結果是

+ rsync -aAXvn '--exclude={/home,' /swap.img, '/dev}' / /media/data/backup/system-backup/

換句話說,shell 執行分詞,將其轉換--exclude={"/home", "/swap.img", "/dev"}為三個參數:(--exclude={/home,它不是格式正確的大括號表達式,因為它沒有右大括號)、/swap.img,(包括逗號)和/dev}.

如果改為命令

bash -xc 'rsync -aAXvn --exclude={"/home","/swap.img","/dev"} / /media/data/backup/system-backup/' 2>&1 | grep ^\+

執行時,我們看到正確的大括號擴展為三個單獨的--exclude參數:

+ rsync -aAXvn --exclude=/home --exclude=/swap.img --exclude=/dev / /media/data/backup/system-backup/

引用自:https://unix.stackexchange.com/questions/698952