11

I need to transfer around 4.2 GB of files from my local computer to a server B. However to ssh into server B, I need to ssh into server A.

Currently I'm copying files from my local computer to server A and then from server A to server B.

So the flow goes like this:

rsync -avz --del ~/Desktop/abc/ <my-user-name>@<server-A>:~/abc

rsync -avz --del ~/Desktop/abc/ <my-user-name>@<server-B>:~/abc

This is slow and copies 4.2 gb of data two times instead of one!

Can I transfer files with rsync from my local computer to directly server B ?

kmario23
  • 57,311
  • 13
  • 161
  • 150
arpit
  • 728
  • 7
  • 22
  • "copies 4.2 gb of data two times instead of one!" Well, since you're routing through server A, you'll always be transferring data "twice", once from local to server A, once from server A to server B (whether they can be simultaneous is another question). That's just the nature of routing. – 4ae1e1 Nov 08 '15 at 20:30

3 Answers3

11

You can always use ssh with proxy command, which allows you to transfer files transparently. Using this config (~/.ssh/config):

Host <server-A>
    User <user-A>

Host <server-B>
    User <user-B>
    ProxyCommand ssh <server-A> -W %h:%p

You can call your rsync:

rsync -avz --del ~/Desktop/abc/ <server-B>:~/abc

The data will be only "routed" over the middle host.

Jakuje
  • 24,773
  • 12
  • 69
  • 75
0

What you want is to use port-forwarding to forward the ssh/rsync port (generally port 22) from server B to alternate ports on server A so when you call rsync -e "ssh -p altport" serverA:/sourcedir /destdir, you are actually invoking rsync from serverB.

There are many good howtos available on StackExchange and other sites. For example:

will get you started. Using port-forwarding, you are essentially using serverA as a pass-through host so you will only have to transfer your 4.2G once.

Community
  • 1
  • 1
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

Yes, you can copy the files (and even folders) directly without making any intermediate copies on the contact/login server, which is by default the machine known to the outside world, or contacted to get access to a specific local network.

Below is a simple demonstration using scp without any unnecessary complications. On the local machine, simply do the following:

$ scp -r -o ProxyCommand="ssh -W %h:%p your_username@contact-server.de" your_username@machine_name:/file/path/on/this/machine   ~/destination/path/to/save/the/copied/folder
  • -r option instructs scp to copy the contents of the entire folder.
  • your_username need not be the same on both machines.

If it is successful, you'll be asked for your passwords on both machines for authentication.

In the above command it is assumed that the typical way to access the machine named as "machine_name" would be via the contact server.


Note:
The above command also works for transferring data from a source remote machine (e.g. s) to a target remote machine (say t). In such a scenario, first ssh to the source remote machine (s) and navigate to the path where the data resides. After that you can simply think of/treat that remote machine as a local/source machine and then simply use the same scp command listed above for copying folders.

For copying individual files, just remove the -r option and provide the path to the specific file that you want to copy.

kmario23
  • 57,311
  • 13
  • 161
  • 150