sync two computers going through a bridge with rsy

2019-07-07 08:11发布

问题:

I want to sync two computers (A and C). Unfortunately I cannot connect from the computer A to the computer C via ssh (no one knows why). This is why I have to use a another computer (B), which is recognized by C.

To sync A and C I build-up two scripts: the first one "sync_A_2_B.sh" (located in A), and "sync_B_2_C.sh" (located in B). Each of those contain rsync instructions.

From A to B:

rsync -av ~/BACK_UP/ username1@blablabla1:/home/BACK_UP/

From B to C:

rsync -av ~/BACK_UP/ username2@blablabla2:/home/BACK_UP/

This works perfect, but it is a bit time consuming. This leads to my question. Would it be possible to perform these actions in one script located in A (""sync_A_2_C.sh) so that the program considers that B is a bridge? I have tied the following, but it does not work:

rsync -av ~/BACK_UP/ username1@blablabla1:/home/BACK_UP/
rsync -av username1@blablabla1:/home/BACK_UP/ username2@blablabla2:/home/BACK_UP/

Nevertheless, it does not work, as source and target cannot be in a remmote desktop at the same time. Is there any possibility to easily perform what I want? Should I use another tool?

回答1:

You could use the ProxyCommand option to ssh to forward the traffic through B. Doing this means that the files don't ever have to live on machine B at all. For example (going from machine A to C):

rsync -av -e 'ssh -o "ProxyCommand ssh -W %h:%p username1@B"' ~/BACK_UP/ username2@C:/home/BACK_UP/

Or you can put the ProxyCommand in your ~/.ssh/config file. Something like:

Host C
   Hostname C
   User username2
   ProxyCommand ssh -W %h:%p username1@B

With this you should be able to do ssh transparently from A to C



标签: linux ssh rsync