Copy Files From B to C via A with SSH
The other day I had an interesting situation to solve:
I have some data on machine B and I want it copied on machine C. However, machines B and C have no direct connection (known_hosts), but machine A can connect to B and C without password.
It is obvious that one needs to use machine A as a proxy between B and C.
[B] ---> [A] ---> [C]
After a while thinking of tunnelling and using some sort of actual network proxy, I realised that I/O would also do the trick: We can have a command runing on B to output data to stdout and another command running on C reading from stdin. The end command would look something like:
ssh user1@B 'cd src_dir && tar -Ozc .' | ssh user2@C 'cd dest_dir && tar zxf -'
where:
user1anduser2are the users under which machine A has password-less access to B and C respectivelysrc_diris the location where data isdest_diris the location where data needs to be copied to
Few notes:
- We change to
src_dirfirst in order to have the archive with a relative path to the actual data that needs to be copied. Otherwise, the path indest_dirwill also containsrc_dir -zis used to compress/decompress data viagzip. One can usebz2too if available on the source/dest machines.- While using
-zresults in less data transferred between machines, the CPU impact of the compression may result in longer copy times. My particular case worked best without the-zflag - Depending on the number of files to be copied, you might want to also drop the verbosity flag at destination (
-f)
Taking into account the above, my fastest variant of the commant is:
ssh user1@B 'cd src_dir && tar -Oc .' | ssh user2@C 'cd dest_dir && tar f -'
HTH,