RSYNC

Rsync stands for remote sync. Rsync is used for synchronization of files and directories between two locations. So it can serve as a backup process also. Important features of rsync :-
    • Speed: File transfer with rsync is fast and efficient because it checks local files against remote files in small chunks, or blocks, and transfers only the blocks that differ between the files.
    • 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]
[destination] Source and destination could be either local or remote. In case of remote, we also need to specify the login name, remote server name and location. Install rsync :-
  • On Ubuntu :- $ sudo apt-get install rsync
  • On Red Hat Enterprise Linux (RHEL) / CentOS 4.x or older version : – # up2date rsync
  • On RHEL / CentOS 5.x or newer (or Fedora Linux) : –# yum install rsync
Lets go through various common operations that we can perform using rsync. Synchronize between two directories on same system :- Let’s say, there are two folder on the personal computer rsync1 and rsync2. So we have to synchronise the data in these two folders ( Assuming these folders are located in present working directory). The command to sync the data between two will be like:-
$ rsync -vv -e ssh rsync1/* rsync2
rsync1/* for sync all the files and sub directories in the rsync1 directory. Options -v = verbose (-vv for more details) -z = compress file data -e ssh = for enable the secure remote connection -a = Recursive mode, Preserves timestamp, Preserves owner and group -u = Do not overwrite a file at the destination, if it is modified -d = Synchronize only directory tree from source to the destination –progress = Displays detailed progress of rsync execution -i = Option displays the item changes. – -max-size = not to copy files more than max size given.
$ rsync -azvu --delete rsync1/. rsync2/.
For deleting the extra files from destination –times = Set the timestamps of the destination file to match those of the source file, instead of using the time of transfer (that is, reflecting the existence of a new file on the destination host) –exclude = Don’t transfer files whose names match glob-pattern. (glob-pattern like “*-*-*- *-*.*”) –include = specify files to be transferred whoes name matches glob-pattern(“*-*-*-*-*.*”) Synchronize Files From Local to Remote :-
$ rsync -avz -e ssh sync1/* 
username@machinename:path sync1 is local system directory (source detination) username@machinename:path (target destination). We can also use rsh to enable the ssh key :-
$ remote_shell = "ssh -i pathToKey username@machinename"
 $ rsync -avz -e --rsh=\remote_shell\ sync1/* username@machinename:path
Synchronize Files From Remote to Local
$ rsync -avz -e ssh username@machinename:path sync1/*
sync1 is local system directory (target destination) username@machinename:path (source destination) We can also use rsh to enable the ssh key :-
remote_shell = "ssh -i pathToKey username@machinename"
$ rsync -avz -e --rsh=\remote_shell\ sync1/* username@machinename:path