Linux: How to rsync between two remote servers that do not communicate each
rsync is widely used by sysadmins to sync files between servers, primarily because of its advantages to sync deltas and resume interrupted…
Linux: How to rsync between two remote servers that do not communicate each other
rsync is widely used by sysadmins to sync files between servers, primarily because of its advantages to sync deltas and resume interrupted transfers.
The scenarios that I have faced so far were straightforward: “sync files from remote host to local” or “sync files from local to remote host” You can find many examples on the internet.
But the scenario that I have had to deal with lately was to sync files between two remote servers using a third server between those two remote servers. The reason for this was that the two remote servers had no connectivity between them due to firewall restrictions, and this was not something that could change fast or easily due to corporate policy. rsync does not support syncing two remote servers, so I started thinking alternatives
- scp: because f the -3 parameter allows using a server between the remote hosts. Still, the transfer job cannot resume in case of a connection failure.
- rsync: from remote host one to the between host and the rsync to remote host two. It will work and supports resuming a failed transfer job, but it will take twice the time needed.
sshfs + rsync == A match from heaven
As I said, rsync cannot sync two remote servers, but we can still trick rsync by thinking it syncs a local directory to a remote server; we can achieve this trick using sshfs. sshfs allows us to mount a remote file system to a local directory.
Installing sshfs in a Debian-like os is very easy and straight.
sudo apt-get install sshfsTo mount a remote server filesystem to a local directory can be tricky because of users and permissions and mount options which must not “break” rsync capability to resume a transfer; I will not get much into details on this, and I will only provide what worked for me
sudo sshfs -o default_permissions root@server:/dir /localdirNow we have mounted the directory “dir” of server “server” to the directory “/localdir” of the localhost, and we are ready to rsync the contents of “/localdir” to remote server “server2”.
The following rsync command allows to resume the transfer in a case of failure
sudo rsync — progress — append — partial -vz -e 'ssh -p 22' /localdir/* root@server2:/dirNow you have managed to rsync files between two remote servers that cannot communicate directly! I hope you found this article valuable and easy to reproduce in your environment!