Unix/Linux:How to use rsync: to synchronise 2 servers. [& ssh no password]

scp does not provide an option to "Not-Overwrite" a file if already exists on the destination server. So finally I implemented the rsync, its very simple and straigt forward.

This can be used mainly when you want to take incremental backup of filesystems or keep servers in sync etc. With a simple wrapper, this can be setup a a cron job as well.

Prerequisites:

Establish SSH with No Password.  
1. On Source server generate ssh keys as below: 
Example: 
oracle@source>ssh-keygen -t rsa 
Generating public/private rsa key pair. 
Enter file in which to save the key (/export/oracle/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /export/oracle/.ssh/id_rsa. 
Your public key has been saved in /export/oracle/.ssh/id_rsa.pub.

2. Copy the id_rsa.pub file to the destination server.

3. Append the contents of the id_rsa.pub to authorized_keys (or authorized_keys2-depending on version) file on target server. This file could be located under .ssh directory in the home directory of the user on target server. 
Example: 
cat id_rsa.pub >>/home/bkpuser/.ssh/authorized_keys

Note: 
Check the permissions of the auhorized_keys file and home directory of the users on both systems, public read/write permissions will fail this setup. You can have rwx- - permissions for this file or home directory.

And this is how it works ..

oracle@source>MYDB>mkdir test 
oracle@source>MYDB>pwd 
/backup/oracle/MYDB/RMAN 
oracle@source>MYDB>rsync -av /backup/oracle/MYDB/RMAN/test bkpuser@destination:/backup/oracle/RMAN 
building file list … done 
test/

sent 70 bytes received 16 bytes 172.00 bytes/sec 
total size is 0 speedup is 0.00 
oracle@source>MYDB>cd test 
oracle@source>MYDB>touch mil.txt 
oracle@source>MYDB>rsync -av /backup/oracle/MYDB/RMAN/test bkpuser@destination:/backup/oracle/RMAN <—Sample Command 
building file list … done 
test/ 
test/mil.txt

sent 129 bytes received 36 bytes 330.00 bytes/sec 
total size is 0 speedup is 0.00

Created 2 new Files

oracle@source>MYDB>touch mil2.txt 
oracle@source>MYDB>touch mil3.txt
 
oracle@source>MYDB>rsync -av /backup/oracle/MYDB/RMAN/test bkpuser@destination:/backup/oracle/RMAN 
building file list … done 
test/ 
test/mil2.txt <—————–Only 2 New files copied to destination 
test/mil3.txt

sent 201 bytes received 56 bytes 171.33 bytes/sec 
total size is 0 speedup is 0.00 
oracle@source>MYDB>ls -otr 
total 0 
-rw-r–r– 1 oracle 0 Apr 3 11:19 mil.txt 
-rw-r–r– 1 oracle 0 Apr 3 11:20 mil2.txt 
-rw-r–r– 1 oracle 0 Apr 3 11:20 mil3.txt

Updated only One File:

oracle@source>MYDB>echo "Hi" >>mil.txt 
oracle@source>MYDB>echo "Hi" >>mil.txt
 
oracle@source>MYDB>rsync -av /backup/oracle/MYDB/RMAN/test bkpuser@destination:/backup/oracle/RMAN 
building file list … done 
test/mil.txt <————–Only One file copied to destination

sent 171 bytes received 36 bytes 414.00 bytes/sec 
total size is 6 speedup is 0.03 
oracle@source>MYDB>