bash 有没有办法让 rsync 在开始传输之前执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23851127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:31:53  来源:igfitidea点击:

Is there a way to make rsync execute a command before beginning its transfer

bashshell

提问by lacrosse1991

I am working on a script which will be used to transfer a file (using rsync) from a remote location and then perform some basic operations on the retrieved content.

我正在编写一个脚本,该脚本将用于从远程位置传输文件(使用 rsync),然后对检索到的内容执行一些基本操作。

When I initially connect to the remote location (not running an rsync daemon, I'm just using rsync to retrieve the files) I am placed in a non-standard shell. In order to enter the bash shell I need to enter "run util bash". Is there a way to execute "run util bash" before rsync begins to transfer the files over?

当我最初连接到远程位置(不运行 rsync 守护程序,我只是使用 rsync 来检索文件)时,我被放置在一个非标准的 shell 中。为了进入 bash shell,我需要输入“run util bash”。有没有办法在 rsync 开始传输文件之前执行“run util bash”?

I am open to other suggestions if there is a way to do this using scp/ftp instead of rsync.

如果有办法使用 scp/ftp 而不是 rsync 来做到这一点,我愿意接受其他建议。

回答by ealfonso

One way is to exectue rsync from the server, instead of from the client. An ssh reverse tunnel allows us to temporarily access the local machine from the remote server.

一种方法是从服务器而不是从客户端执行 rsync。ssh 反向隧道允许我们从远程服务器临时访问本地机器。

  1. Assume the local machine has an ssh server on port 22
  2. Shh into the remote host while specifying a reversetunnel that maps a port in the remote machine (in this example let us use 2222) to port 22 in our local machine
  3. Execute your rsync command, replacing any reference to your local machine with the reverse ssh tunnel address: my-local-user@localhost
  4. Add a port option to rsync's ssh to have it use the 2222 port.
  1. 假设本地机器在端口 22 上有一个 ssh 服务器
  2. 在指定将远程机器中的端口(在本例中让我们使用 2222)映射到本地机器中的端口 22的反向隧道的同时,嘘到远程主机
  3. 执行您的 rsync 命令,用反向 ssh 隧道地址替换对本地机器的任何引用:my-local-user@localhost
  4. 向 rsync 的 ssh 添加端口选项以使其使用 2222 端口。

The command:

命令:

ssh -R 2222:localhost:22 remoteuser@remotemachine << EOF

  # we are on the remote server.
  # we can ssh back into the box running the ssh client via ${REMOTE_PORT}
  run utils bash
  rsync -e "ssh -p 2222" --args /path/to/remote/source remoteuser@localhost:/path/to/local/machine/dest
EOF

Reference to pass complicated commands to ssh: What is the cleanest way to ssh and run multiple commands in Bash?

将复杂命令传递给 ssh 的参考: 在 Bash 中 ssh 和运行多个命令的最干净的方法是什么?

回答by SkyRar

You can achieve it using --rsync-pathalso. E.g rsync --rsync-path="run util bash && rsync" -e "ssh -T -c aes128-ctr -o Compression=no -x" ./tmp [email protected]:~

您也可以使用它来实现它--rsync-path。例如rsync --rsync-path="run util bash && rsync" -e "ssh -T -c aes128-ctr -o Compression=no -x" ./tmp [email protected]:~

--rsync-pathis normally used to specify what program is to be run on the remote machine to start-up rsync. Often used when rsync is not in the default remote-shell's path (e.g. –rsync-path=/usr/local/bin/rsync). Note that PROGRAM is run with the help of a shell, so it can be any program, script, or command sequence you'd care to run, so long as it does not corrupt the standard-in & standard-out that rsync is using to communicate.

--rsync-path通常用于指定在远程机器上运行什么程序来启动 rsync。通常在 rsync 不在默认远程 shell 的路径中时使用(例如 –rsync-path=/usr/local/bin/rsync)。请注意,PROGRAM 是在 shell 的帮助下运行的,因此它可以是您想要运行的任何程序、脚本或命令序列,只要它不会破坏 rsync 正在使用的标准输入和标准输出沟通。

For more details refer

欲了解更多详情,请参阅