rsync

rsync stands for remote sync.

rsync is used to perform the backup operation in UNIX / Linux.

rsync utility is used to synchronize the files and directories from one location to another in an effective way. Backup location could be on local server or on remote server.

 

Important features of rsync

Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.

 

Security: rsync allows encryption of data using ssh protocol during transfer.

 

Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less

compared to other file transfer protocols.

 

Privileges: No special privileges are required to install and execute rsync

 

 

Syntax :

$ rsync options source destination

 

 

Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

 

Synchronize Two Directories in a Local Server

 

To sync two directories in a local computer, use the following rsync –zvr command.

 

$ rsync -zvr /var/opt/sathish/   /root/temp

building file list … done

./

sva.xml

svB.xml

.sent 26385 bytes received 1098 bytes 54966.00 bytes/sec

total size is 44867 speedup is 1.63

 

 

In the above rsync example:

• -z is to enable compression

• -v verbose

• -r indicates recursive

 

Now let us see the timestamp on one of the files that was copied from source to destination. As you see below, rsync didn’t preserve timestamps during sync.

 

 

$ ls -l /var/opt/sathish/sva.xml   /root/temp/sva.xml

-r–r–r– 1 bin bin 949 Apr 18 2013 /var/opt/sathish/sva.xml

-r–r–r– 1 root bin 949 May 2 2013 /root/temp/sva.xml

 

 

Preserve timestamps during Sync using rsync -a

rsync option -a indicates archive mode. -a option does the following,

• Recursive mode

• Preserves symbolic links

• Preserves permissions

• Preserves timestamp

• Preserves owner and group

 

Now, executing the same command provided in example 1 (But with the rsync option -a) as shown below:

 

 

$ rsync -azv /var/opt/sathish/   /root/temp/

building file list … done

./

sva.xml

svB.xml

.

sent 26499 bytes received 1104 bytes 55206.00 bytes/sec

total size is 44867 speedup is 1.63

 

As you see below, rsync preserved timestamps during sync.

 

$ ls -l /var/opt/sathish/sva.xml    /root/temp/sva.xml

-r–r–r– 1 root bin 949 Apr 18 2013 /var/opt/sathish/sva.xml

-r–r–r– 1 root bin 949 Apr 18 2013 /root/temp/sva.xml

 

Synchronize Only One File

 

To copy only one file, specify the file name to rsync command, as shown below.

 

$ rsync -v /var/lib/rpm/Pubkeys /

root/temp/

Pubkeys

sent 42 bytes received 12380 bytes 3549.14 bytes/sec

total size is 12288 speedup is 0.99

 

 

Synchronize Files From Local to Remote

rsync allows you to synchronize files/directories between the local and remote system.

 

$ rsync -avz /root/temp/      sathish@192.168.31.1:/home/sathish/temp/

Password:

building file list … done

./

rpm/

rpm/Basenames

rpm/Conflictname

sent 15810261 bytes received 412 bytes 2432411.23

bytes/sec

total size is 45305958 speedup is 2.87

 

While doing synchronization with the remote server, you need to specify username and ip-address of the remote server. You should also specify the destination directory on the remote server. The format is username@machinename:path As you see above, it asks for password while doing rsync from local to remote server.

 

Sometimes you don’t want to enter the password while backing up files from local to remote server. For example, If you have a backup shell script, that copies files from local to remote server using rsync, you need the ability to rsync without having to enter the password.

To do that, setup ssh password less login as we explained earlier.

 

Synchronize Files From Remote to Local

 

When you want to synchronize files from remote to local, specify remote path in source and local path in target as shown below.

 

$ rsync -avz sathish@192.168.31.1:/var/lib/rpm

/root/temp

Password:

receiving file list … done

rpm/

rpm/Basenames

.

sent 406 bytes received 15810230 bytes 2432405.54

bytes/sec

total size is 45305958 speedup is 2.87