rsync two remote hosts

Usually, rsync is used to synchronize one local directory to/from one remote directory. But sometimes, because two hosts cannot see each other directly, one cannot log into one host and rsync to from the other.

The solution is to find a third host that can see both hosts and then sync them. Unfortunately, rsync accepts only one remote host. The solution : use a reverse SSH tunnel :

Let’s say you want to sync www@host_A:django/apps/ to www@host_B:django/apps, but host_A cannot communicate directly with host_B (because of a firewall for instance). Nevertheless you can find in the middle a host_M that can communicate with both host_A and host_B.

First open a reverse SSH tunnel to host_B by logging on host_A from host_M, here the tunnel will be available from host_A on port 50000 :

Now your are logged onto host_A and a tunel on port 50000 is available to connect to host_B, you can test that by doing :

You should have succeeded to log onto host_B !

Go back to host_A (ctrl-D or exit)

it’s time to do the rsync what you wanted : do not forget to specify localhost and port 50000 as destination instead of host_B:

Et voilà !