Data Synchronization with rsync under Linux
The rsync software makes unidirectional data synchronization from one source directory to one target directory possible under Linux. These two directories may be on different computers (changes will then be transferred through the network).
Example: Data Synchronization for Backup Purposes
Often, rsync is used for making backups. Using the following example command, the /home/ directory on the server.example.com server will be synchronized with the /mnt/server-mirror/home/ directory on the local computer. The data will be transmitted through an SSH tunnel.
rsync -av --delete -e ssh firstname.lastname@example.org:/home/ /mnt/server-mirror/home/
If rsync should be automatically run by a script, authentication may be performed using an SSH key.
rsync -av --delete -e "ssh -i /home/user/.ssh/sshkey" email@example.com:/home/ /mnt/server-mirror/home/
This indicated rsync flags have the following meanings:
- -a (short form for --archive), same meaning as -rlptgoD
- -r (short form for --recursive), copies subdirectories
- -l (short form for --links), copies symbolic links
- -p (short form for --perms), retains the rights to the source file
- -t (short form for --times), retains the date of the source file
- -g (short form for --group), retains the group rights to the source file
- -o (short form for --owner), retains the ownership rights to the source file (only possible for the root user)
- -D (short form for --devices --specials), retains the device files for the source file (only possible for the root user)
- -v (short form for --verbose), indicates which files are being synchronized while the program is running
- --delete: deletes files in the target directory that no longer exist in the source directory
Creating backups using rdiff-backup
The command indicated above will synchronize the data from the source to the target directory. Of course, if a file has been deleted in error and rsync executed afterwards, the deleted file will also no longer exist in the target directory. The rdiff-backup utility ideally supplements rsync in such cases.
Author: Werner Fischer
Werner Fischer, working in the Web Operations & Knowledge Transfer team at Thomas-Krenn, completed his studies of Computer and Media Security at FH Hagenberg in Austria. He is a regular speaker at many conferences like LinuxTag, OSMC, OSDC, LinuxCon, and author for various IT magazines. In his spare time he enjoys playing the piano and training for a good result at the annual Linz marathon relay.