Synchronize Files And Directories Across Systems Using Rsync

Rsync is an open source software that can be used to synchronize files/folders from local to local computers, or local to remote computers and vice versa. The notable feature of the rsync is we can transfer encrypted files using SSH, and SSL encrypted transfer can be done using Stunnel. Moreover, rsysnc has a feature which is not found in the most other similar programs is that the mirroring takes place with only one transmission in each direction, eliminating the message latency overhead inherent in transmitting a large number of small messages.

Also, rsync has options to send files/directories using compression and recursion. This program was first announced in June 1996, originally developed by Andrew Tridgell and Paul Mackerras. Rsync uses “rsync algorithm”, and provides a very fast method for bringing remote files into the sync.

Installation

rsync is available on all modern Unix/Linux systems default repositories.

On Debian/Ubuntu based systems:

sudo apt-get install rsync

On CentOS/RHEL based systems:

yum install rsync

On SUSE/OpenSUSE systems:

zypper in rsync

Usage

For the purpose of this tutorial, I will be using two Ubuntu 14.04 systems. My source system(local system) IP address is 192.168.1.200/24, and my destination system(remote system) IP address is 192.168.1.250/24. I want to synchronize the /home/sk/Downloads/ directory of the source system(192.168.1.200) with /home/sk/sync/ directory of the destination system(192.168.1.250).

1. To synchronize a directory from local system with a remote system, enter the following command:

The same way, you can transfer a file from a local system to remote system.

For more detailed rsync usage, refer the man pages.

man rsync

In the above examples, we have learned the basic usage of rsync commands. However, we can make the permanent synchronization between local and remote systems, and schedule them at a particular interval. For this purpose, let us synchronize the files/folders of /home/sk/Downloads of local system with /home/sk/sync of remote system permanently, and schedule the sync process at a particular time.

First, make sure that you’ve installed rsync and xinetd packages on both source and destination systems.

To install xinetd on Debian/Ubuntu systems, use the following command:

sudo apt-get install xinetd

On RHEL based systems:

yum install xinetd

Now, we have to make some changes in the configuration files.

Do the following changes on the destination systems:

First, edit file /etc/default/rsync,

sudo vi /etc/default/rsync

Find the following line,

RSYNC_ENABLE=false

Change it to look like below.

RSYNC_ENABLE=inetd

Save and close the file.

Then, edit file /etc/xinetd.d/rsync,

Note: The above file will not be found on the Ubuntu/Debian file. You have to create and add the lines mentioned below.

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh [email protected]'"
and check to make sure that only the key(s) you wanted were added.

Now you can login to server using ssh without password.

Configure Cron jobs

If you want to run the sync process at a particular interval everyday, add the following entries in the crontab.

Enter the command:

crontab -e

Add the following line:

00 05 * * * rsync -azvr /home/sk/Downloads/ 192.168.1.250::share

The cron job file will run everyday at 5am, and start the sync process automatically.

POPULAR CATEGORY

Unixmen provide Linux Howtos, Tutorials, Tips & Tricks ,Opensource News. It cover most popular distros like Ubuntu, LinuxMint, Fedora, Centos. It is your Gate to the the world of Linux/Unix and Opensource in General.