Using a Raspberry Pi to synchronize data

Rsync: Tips and Tricks

For a long time, Rsync was the top dog among sync tools for Unix. Then, it became somewhat overshadowed by Dropbox. Even so, the small tool Rsync is a veritable Swiss army knife for synchronization. It gives you the option of transferring only new changes to your data and then verifying that everything has arrived correctly. You can also use it for making local copies. The following command copies the directory /home/pi/src from a local computer to a second computer, pi2:

$ rsync -avz /home/pi/src admin@pi2:/home/admin

In the example, the login takes place as user admin and the target directory is /home/admin. You can also enter the source and target the other way around. Then, Rsync simply retrieves the directory from the remote computer. Care should be taken with path naming. If a source directory does not have a slash (/) at the end of its name, Rsync will copy the entire directory, as in the src example above. Otherwise, it will only copy the contents. It doesn't matter whether the name of the target directory has a slash at the end.

The -v option means "verbose" and outputs all transferred files, whereas the -z option is used to compress the data during transfer. The latter option is not suitable for synchronization of images, MP3 files, or other already compressed formats. However, Rsync comes with a built-in list of relevant file name extensions and typically gets everything right. Further details on this can be found at the man page.

This simple Rsync command with its three options covers a lot of ground. The remaining 110 parameters of Rsync are generally only used in special circumstances.

Transferring Data via the Hub

The scenario described in the introduction, in which images that have been reduced in size are sent from the desktop to the smartphone, can work with Rsync because the desktop sends the files to the hub, and Rsync deletes the correctly transferred files.

$ rsync -av --remove-source-files /data/pics hub:/data/

Later, the smartphone can retrieve the files with the same option. As a result, no unnecessary data accumulates on the hub.

RSyncBackup is a free application available for Android systems, but the source code is not public. In RSyncBackup, you define the command line and then execute the command on the profile with a click. This is very practical if you want to use the same directories for data transfer again.

Rsync can also handle mirroring; in other words, it can handle single-direction synchronization of one computer with another. For this process, you need to replace the --remove-source-files option with the --delete option. This step also deletes directories on the target computer instead of on the source computer, which the first option does not support.

If you need two-way synchronization, you should take a look at Unison [10]. It uses the Rsync algorithm and offers a nice interface, in addition to a pure command-line operation. A ready-to-install version of Unison can be found in the Raspbian repository.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content