【发布时间】:2013-02-25 15:16:58
【问题描述】:
我正在尝试创建一个 bash 脚本,它将指定为命令行参数的目录同步到远程服务器(也由参数指定)。目前,我正在使用eval,它解决了参数扩展问题,但由于某种原因导致 rsync 无法保留远程文件的所有权(我知道除了是 Evil 之外)。从命令提示符运行带有所有相同标志和参数的 rsync 命令可以正常工作。
我尝试使用$() 作为替代方案,但我在变量扩展和保护远程 rsync 路径需要保护的位(需要引号 和 反斜杠带空格的路径)。
所以 - 我猜有两个问题 - eval 是否有理由阻止 rsync 保留所有权(bash 脚本在源计算机上以 root 身份运行,ssh 以root 太 - 只是现在)?有没有办法让$() 在这种情况下工作? (修剪后的)代码如下:
#!/bin/bash
RSYNC_CMD="/usr/bin/rsync"
RSYNC_FLAGS="-az --rsh=\"/usr/bin/ssh -i \${DST_KEY}\"" # Protect ${DST_KEY} until it is assigned later
SRC=${1} # Normally this is sense checked and processed to be a canonical path
# Logic for setting DST based on command line parameter snipped for clarity - just directly assign for testing
DST='root@some.server.com:'
DST_KEY='/path/to/sshKey.rsa'
TARG=${DST}${SRC//' '/'\ '} # Escape whitespace for target system
eval ${RSYNC_CMD} ${RSYNC_FLAGS} \"${SRC}\" \"${TARG}\" # Put quotes round the paths - even though ${TARG} is already escaped
# All synced OK - but ownership not preserved despite -a flag
我尝试将RSYNC_CMD 更改为sudo /usr/bin/rsync,并将--rsync-path="sudo /usr/bin/rsync 添加到RSYNC_FLAGS,但都没有任何区别。我只是看不到我错过了什么......
【问题讨论】:
-
rsync --help | grep owner告诉我:-o, --owner preserve owner (super-user only)。你试过那个选项吗? -
谢谢 - 正如@chepner 下面所说,-a 意味着 -o,如果我从命令行执行它,它工作正常,所以我假设它是 eval(只是为了确定,我确实尝试过-o,但没有区别)。我将在接下来的几天内尝试对 chepner 的重写。