Syncing Files Via SSH Using rsync



by AndrzejL

I own a few machines. One of them is my main, everyday machine. Others are my backup machines that I use when the main machine is doing a heavy task, or when I cannot access it for another reason.

Sometimes it’s annoying. Really annoying. Why? Oh, for example, the pidgin config files were not synchronized between the machines. I added some contacts to the main machine pidgin configuration and I never had a chance to set them up on the rest of my machines. I had a chat or two with a friend, and the logs were not available on my other machines, so I could not check the history of the chat. I changed the configuration of Firefox on my main machine, and the other machines were not updated. Same goes for Thunderbird / e-mail settings and synchronization. Even my folders with data. It was seriously bothering me. So, I have found a perfect solution.

It’s extremely simple. Assuming that you have passwordless authentication working, all you need is one command. The command should be run on the ssh client machine. In other words,  you should run it on the machine you want to synchronize files to. It will connect to the remote SSH server machine and download the files from it.

rsync -avz -e ssh andrzejl@192.168.0.100:/home/andrzejl/.purple /home/andrzejl/

This command will connect with login andrzejl to the SSH server running on the 192.168.0.100 machine and it will incrementally synchronize the /home/andrzejl/.purple folder to the /home/andrzejl/ folder on a local machine. I didn’t add the .purple folder at the end of the command as the synchronization process will create it if it’s not there. If I did add it, it would create /home/andrzejl/.purple/.purple folder.

Now imagine the possibilities. Put that command in a script and then create an automated job in crontab so it will be executed every, let’s say, one hour. I did it here and it works perfectly! Your client (backup/spare) machine config files will always be synchronized with your main  server machine. Your data folders can be synchronized between all your machines. Perfect. That’s what I wanted.

OH BOY! Is this faster than copying the files with midnight commander. When syncing my pidgin folder to a fresh install and using mc, it could take up to 3 hours due to the fact that the logs folder has almost 60 megs, and it contains over 41,000 small files. Rsync does it in less than 20 minutes. And, later it just copies the “new” files thanks to the “increment” option. So it takes several seconds. Rsync rocks.

If that’s not cool, I don’t know what is.

If you have tightened up SSH server security and changed the SSH port from the standard use of port 22, you will have to slightly modify the command to add the port number.

Here is what it looks like:

rsync -avz -e 'ssh -p 20202' andrzejl@192.168.0.100:/home/andrzejl/.purple /home/andrzejl/