我正在尝试使用以下脚本备份整个系统目录:
#!/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/* 。有人能告诉我我在这里做错了什么吗?
答案1
执行命令时
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/