我正在尝试从 python 脚本中的子进程通过 ssh 执行 rsync,以将图像从一台服务器复制到另一台服务器。我有一个函数定义为:
def rsyncBookContent(bookIds, serverEnv):
bookPaths = ""
if len(bookIds) > 1:
bookPaths = "{" + ",".join(("book_"+str(x)) for x in bookIds) + "}"
else:
bookPaths = "book_" + str(bookIds[0])
for host in serverEnv['content.hosts']:
args = ["rsync", "-avz", "--include='*/'", "--include='*.jpg'", "--exclude='*'", "-e", "ssh", options.bookDestDir + "/" + bookPaths, "jill@" + host + ":/home/jill/web/public/static/"]
print "executing " + ' '.join(args)
subprocess.call(args)
我最终想要做的是让 Python 执行这个(它在 bash shell 中工作):
rsync -avz --include='*/' --include='*.jpg' --exclude='*' -e ssh /shared/books/{book_482,book_347} jill@10.12.27.20:/home/jill/web/public/static/
事实上,我的打印语句输出:
executing rsync -avz --include='*/' --include='*.jpg' --exclude='*' -e ssh /shared/books/{book_482,book_347} jill@10.12.27.20:/home/jill/web/public/static/
但是当从这个 python 脚本中执行时,有两个问题:
- 如果 len(bookIds) > 1,/shared/books/ 下的子目录列表会被 bash 或 rsync 误解。错误信息是:
- rsync:link_stat“/shared/books/{book_482,book_347}”失败:没有这样的文件或目录(2))
- 如果 len(bookIds) == 1,则源目录下的所有文件都被 rsync(不只是 *.jpg,这是我的意图)
似乎 subprocess.call 函数需要转义一些字符或其他东西,不是吗?