我正在尝试围绕 rsync 命令制作一个包装器以重用代码。我需要传递源文件夹、目标文件夹,然后传递任意数量的排除目录。这是我的日志;基本上我正在构建我的多个 rsync --从我的“vararg”参数中排除参数。可能有更好的方法,但我发现一个就足够了。
function run_backup(){
source="$1"
target="/backups/unraid$2"
#Copying $source into remote folder $target
# Building my exclude params
if [ $# -ge 3 ]; then
excludes="--exclude '$3'"
while shift && [ -n "$3" ]; do
echo $excludes
excludes="${excludes} --exclude '$3'"
done
else
excludes = "--exclude '/tmp'" # Dummy exclude; I'll handle this case later
fi
echo $excludes
# Creating folder because rsync crash otherwise n some case
ssh -p 22 -i /home/backup/.ssh/mykey [email protected] "mkdir -p $target"
# Executing the copy
set -e
sudo rsync -azvh \
-e "ssh -p 22-i /home/backup/.ssh/mykey "\
--progress \
--delete \
$excludes \
$source \
[email protected]:$target
}
mkdir -p /tmp/dummy
echo "dummy" > /tmp/dummy/save-ignored-1
echo "dummy" > /tmp/dummy/save-ignored-2
echo "dummy" > /tmp/dummy/save-test
run_backup /tmp/dummy/ /dummy /tmp/dummy/save-ignored-1 /tmp/dummy/save-ignored-2 /tmp/toto /tmp/tata /tmp/tutu #TODO
--exclude '/tmp/dummy/save-ignored-1'
--exclude '/tmp/dummy/save-ignored-1' --exclude '/tmp/dummy/save-ignored-2'
--exclude '/tmp/dummy/save-ignored-1' --exclude '/tmp/dummy/save-ignored-2' --exclude '/tmp/toto'
--exclude '/tmp/dummy/save-ignored-1' --exclude '/tmp/dummy/save-ignored-2' --exclude '/tmp/toto' --exclude '/tmp/tata'
--exclude '/tmp/dummy/save-ignored-1' --exclude '/tmp/dummy/save-ignored-2' --exclude '/tmp/toto' --exclude '/tmp/tata' --exclude '/tmp/tutu'
++ ssh -p 22-i /home/backup/.ssh/mykey [email protected] 'mkdir -p /backups/unraid/dummy'
++ sudo rsync -azvh -e 'ssh -p 22 -i /home/backup/.ssh/mykey' --progress --delete --exclude ''\''/tmp/dummy/save-ignored-1'\''' --exclude ''\''/tmp/dummy/save-ignored-2'\''' --exclude ''\''/tmp/toto'\''' --exclude ''\''/tmp/tata'\''' --exclude ''\''/tmp/tutu'\''' /tmp/dummy/ [email protected]:/backups/unraid/dummy
我们可以看到 var$excludes
正是我想要的;但是当我将它传递给 rsync 时,bash 正在对它施展一些魔法。我不知道如何避免这种情况。我想这与扩展有关,但我找不到与我的或相关文档类似的问题。
编辑:第一个答案后的代码:
function run_backup(){
excludes=()
if [ $# -ge 3 ]; then
echo $3
excludes+=(--exclude $3)
while shift && [ -n "$3" ]; do
echo $3
excludes+=(--exclude $3)
done
else
excludes+=(--exclude /tmp)
fi
set -x
echo "${excludes[@]}"
}
run_backup /tmp/dummy/ /dummy "/tmp/dummy/save-ignored" "/tmp/dummy/save non'standard ignored" /tmp/toto /tmp/tata /tmp/tutu
但它不适用于包含引号的字符串(至少是简单的引号)
/tmp/dummy/save-ignored
/tmp/dummy/save non'standard ignored
/tmp/toto
/tmp/tata
/tmp/tutu
++ echo --exclude /tmp/dummy/save-ignored --exclude /tmp/dummy/save 'non'\''standard' ignored --exclude /tmp/toto --exclude /tmp/tata --exclude /tmp/tutu
--exclude /tmp/dummy/save-ignored --exclude /tmp/dummy/save non'standard ignored --exclude /tmp/toto --exclude /tmp/tata --exclude /tmp/tutu
编辑2:无用;只是保留它作为历史 包含所有报价可能性的测试:
run_backup(){
excludes=()
excludes2=()
excludes3=()
if [ $# -ge 3 ]; then
echo $3
excludes+=(--exclude $3)
excludes2+=(--exclude "$3")
excludes3+=(--exclude "\"$3\"")
while shift && [ -n "$3" ]; do
echo $3
excludes+=(--exclude $3)
excludes2+=(--exclude "$3")
excludes3+=(--exclude "\"$3\"")
done
else
excludes+=(--exclude /tmp)
fi
set -x
echo "${excludes[@]}"
echo "${excludes2[@]}"
echo "${excludes3[@]}"
sudo rsync -azvh \
--dry-run \
"${excludes[@]}" \
--dry-run \
"${excludes2[@]}" \
--dry-run \
"${excludes3[@]}" \
--dry-run \
${excludes[@]} \
--dry-run \
${excludes2[@]} \
--dry-run \
${excludes3[@]} \
/tmp \
[email protected]:/tmp
}
run_backup "/tmp/dummy/" "/dummy" "/tmp/dummy/save non'standard" "/tmp/toto"
++ echo --exclude /tmp/dummy/save 'non'\''standard' --exclude /tmp/toto
--exclude /tmp/dummy/save non'standard --exclude /tmp/toto
++ echo --exclude '/tmp/dummy/save non'\''standard' --exclude /tmp/toto
--exclude /tmp/dummy/save non'standard --exclude /tmp/toto
++ echo --exclude '"/tmp/dummy/save non'\''standard"' --exclude '"/tmp/toto"'
--exclude "/tmp/dummy/save non'standard" --exclude "/tmp/toto"
++ sudo rsync -azvh --dry-run --exclude /tmp/dummy/save 'non'\''standard' --exclude /tmp/toto --dry-run --exclude '/tmp/dummy/save non'\''standard' --exclude /tmp/toto --dry-run --exclude '"/tmp/dummy/save non'\''standard"' --exclude '"/tmp/toto"' --dry-run --exclude /tmp/dummy/save 'non'\''standard' --exclude /tmp/toto --dry-run --exclude /tmp/dummy/save 'non'\''standard' --exclude /tmp/toto --dry-run --exclude '"/tmp/dummy/save' 'non'\''standard"' --exclude '"/tmp/toto"' /tmp [email protected]:/tmp
答案1
使用数组而不是字符串。
例如:
opt=()
opt+=(--exclude "first")
opt+=(--exclude "another one")
set -x
echo "${opt[@]}"
这将正确处理空格(输出):
+ echo --exclude first --exclude 'another one'