从bash脚本运行时,rsync的语法错误 [英] Rsync syntax error when run from bash script

查看:90
本文介绍了从bash脚本运行时,rsync的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用的rsync做一次增量备份的备份脚本。

我有以下rsync命令手动测试,运行和完成备份没有错误:

  rsync的-aAXv --delete --progress --link-DEST = /备份/ Uyuk / Uyuk备份 - 第1部分/ 2014年2月24日/到/ mnt /备份/ /备份/ Uyuk / Uyuk备份 - 第1部分/ 2014年2月25日/

然而

当我运行,在我的备份脚本相同的命令它给了我以下错误:

 的rsync:-aAXv --delete --progress --link-DEST = /备份/ Uyuk / Uyuk备份 - 第1部分/ 2014年2月24日/到/ mnt /备份/ /备份/ Uyuk / Uyuk备份 - 第1部分/ 2014年2月25日/:未知的选项
rsync的错误:在main.c中的语法和用法错误​​(code 1)(1422)的客户端= 3.0.6]

我跑我的脚本bash的-x弄清楚到底是什么发送到控制台,这里是什么是印:

  + rsync的'-aAXv --delete --progress --link-DEST = /备份/ Uyuk / Uyuk备份 - 第1部分/ 2014年2月24日/到/ mnt /备份/ /备份/ Uyuk / Uyuk备份 - 第1部分/ 2014年2月25日/

有谁看到什么是错的?我找不到任何会导致语法错误。

编辑:
下面是实际的code我在剧本,这是所以是一些变量这里没有定义pretty大脚本,但你的想法。

 的mkdir -p/备份/ $ HOST / $ NAME / $ TODAY
#source目录
SRC =$ MNT
#LINK目录
LNK =/备份/ $ HOST / $名称/ $ LAST /
#target目录
TRG =/备份/ $ HOST / $名称/ $ TODAY /
#rsync选项
OPT1 = - aAXv --delete --progress --link-DEST = $ LNK#run rsync命令
回声rsync的$ $ OPT1 $ SRC TRG
rsync的$ OPT1 $ SRC $ TRG> /var/log/backup/backup.rsync.log 2 - ;&放大器; 1


解决方案

您是通过你的选项列表作为一个参数,需要时作为参数的列表的传递它。一般情况下,你应该使用庆典一个数组来保存你的论点,如果其中任何包含空格。请尝试以下操作:

 的mkdir -p/备份/ $ HOST / $ NAME / $ TODAY
#source目录
SRC =$ MNT
#LINK目录
LNK =/备份/ $ HOST / $名称/ $ LAST /
#target目录
TRG =/备份/ $ HOST / $名称/ $ TODAY /
#rsync选项
OPTS =(-aAXv--delete--progress--link-DEST = $ LNK)#run rsync命令
回声rsync的$ $ OPT1 $ SRC TRG
rsync的$ {OPTS [@]}$ SRC$ TRG> /var/log/backup/backup.rsync.log 2 - ;&放大器; 1

这是阵列扩展 $ {OPTS [@]} ,引用的时候,经过特殊处理的参数序列,其中每个单独报价为preserve中的各个元素任何空格或特殊字符。如果 ARR =(ABCD),那么回声$ {ARR [@]}是一样的为

 回声A BCD

而不是

 回声A B C D


这不会在shell不支持数组,但随后的工作中,数组被发明了,因为没有一个安全的方式(即不使用评估)来处理这个用例没有他们。

I have been working on a backup script that uses rsync to do an incremental backup.

I have tested the following rsync command manually, and it runs and completes a backup without error:

rsync -aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/

however when I run that same command in my backup script it gives me the following error:

rsync: -aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/: unknown option
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]

I ran bash -x on my script to figure out exactly what is sent to the console and here is what was printed:

+ rsync '-aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/'

Does anyone see what is wrong? I cant find anything that would cause the syntax error.

EDIT: Here is the actual code I have in the script, and this is a pretty large script so yes some variables are not defined here, but you get the idea.

mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
SRC="$MNT"
#link directory
LNK="/backup/$HOST/$NAME/$LAST/"
#target directory
TRG="/backup/$HOST/$NAME/$TODAY/"
#rsync options
OPT1="-aAXv --delete --progress --link-dest=$LNK"

#run the rsync command
echo "rsync $OPT1 $SRC $TRG"
rsync "$OPT1 $SRC $TRG" > /var/log/backup/backup.rsync.log 2>&1

解决方案

You are passing your option list as a single argument, when it needs to be passed as a list of arguments. In general, you should use an array in bash to hold your arguments, in case any of them contain whitespace. Try the following:

mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
SRC="$MNT"
#link directory
LNK="/backup/$HOST/$NAME/$LAST/"
#target directory
TRG="/backup/$HOST/$NAME/$TODAY/"
#rsync options
OPTS=( "-aAXv" "--delete" "--progress" "--link-dest=$LNK" )

#run the rsync command
echo "rsync $OPT1 $SRC $TRG"
rsync "${OPTS[@]}" "$SRC" "$TRG" > /var/log/backup/backup.rsync.log 2>&1

An array expansion ${OPTS[@]}, when quoted, is treated specially as a sequence of arguments, each of which is quoted individually to preserve any whitespace or special characters in the individual elements. If arr=("a b" c d), then echo "${arr[@]}" is the same as

echo "a b" "c" "d"

rather than

echo "a b c d"


This will not work in a shell that doesn't support arrays, but then, arrays were invented because there wasn't a safe way (that is, without using eval) to handle this use case without them.

这篇关于从bash脚本运行时,rsync的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆