通过 ssh 使用 rsync 从远程复制多个文件

Sup*_*ish 14 command-line rsync

我想使用rsync. 所以我使用以下命令。

rsync -Pav -e 'ssh -i sshkey' user@remotemachine:/home/user/file1.zip file2.zip file3.zip  .
Run Code Online (Sandbox Code Playgroud)

它显示以下错误

意外的本地 arg:file2.zip 如果 arg 是远程文件/目录,请在其前面加上冒号 (:)。rsync 错误:main.c(1362) [Receiver=3.1.0] 处的语法或使用错误(代码 1)

小智 17

所有远程文件都应该是 rsync 的一个参数。因此,只需将所有远程文件放在单引号中:

rsync -Pav -e 'ssh -i sshkey' 'user@remotemachine:/home/user/file1.zip file2.zip file3.zip' .
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你也可以用一个星号来做到这一点(星号将被远程shell解析):

rsync -Pav -e 'ssh -i sshkey' 'user@remotemachine:/home/user/*.zip' .
Run Code Online (Sandbox Code Playgroud)

  • 因为亚历山大大帝,它工作得很好 (2认同)

小智 17

这已经很老了,但接受的答案有点过于严格 - 多个文件不一定是 rsync 的单个参数。来自man rsync

ADVANCED USAGE
       The  syntax  for  requesting  multiple  files  from a remote host is done by specifying additional remote-host args in the same style as the first, or with the hostname omitted.  For
       instance, all these work:

              rsync -av host:file1 :file2 host:file{3,4} /dest/
              rsync -av host::modname/file{1,2} host::modname/file3 /dest/
              rsync -av host::modname/file1 ::modname/file{3,4}

Run Code Online (Sandbox Code Playgroud)

所以OP的命令是

rsync -Pav -e 'ssh -i sshkey' user@remotemachine:/home/user/file1.zip :/home/user/file2.zip :/home/user/file3.zip  .
Run Code Online (Sandbox Code Playgroud)

这对于旧版本的 rsync 是不可能的,但我认为所有主要发行版都已经使用了好几年了。