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:
user1
anduser2
are the users under which machine A has password-less access to B and C respectivelysrc_dir
is the location where data isdest_dir
is the location where data needs to be copied to
Few notes:
- We change to
src_dir
first in order to have the archive with a relative path to the actual data that needs to be copied. Otherwise, the path indest_dir
will also containsrc_dir
-z
is used to compress/decompress data viagzip
. One can usebz2
too if available on the source/dest machines.- While using
-z
results 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-z
flag - 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,
Member discussion: