Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

shell

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.

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?

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

like image 308
lacrosse1991 Avatar asked May 25 '14 01:05

lacrosse1991


1 Answers

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.

  1. Assume the local machine has an ssh server on port 22
  2. Shh into the remote host while specifying a reverse tunnel 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.

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?

like image 193
ealfonso Avatar answered Sep 24 '22 14:09

ealfonso