Saturday, 7 June 2014

RSync Command to Backup and Synchronize Files in Linux

RSync or Remote Sync is the Linux command usually used for backup of files/directories and synchronizing them locally or remotely in an efficient way. One of the reasons of why RSync is preferred over all other alternatives is the speed of operation, RSync copies the chunk of data to other location at a significantly faster rate. This is because, whenever Rsync is executed for the very first occasion, it transfers all the data from source to the destination. On the next turn, it would just copy the files/directories whose contents are changed.

    Another plus point of using this utility is, as it makes use of SSH protocol to encrypt the data to be replicated, so it is much more secure and trustworthy. One more advantage of using Rsync is, as it performs compression of the data at source end and decompresses it at the destination, the bandwidth used during the sync operation will be considerably less. Also, the file permissions, their user/group information and the timestamps is/can be preserved.

Syntax:

rsync [OPTIONS] SOURCE DESTINATION
Important Options:
-v : indicates Verbose mode which provides detailed information.
-r : indicates Recursive operation, timestamps and file permissions are not preserved
-a : indicates Archive mode,timestamps and file permissions are preserved
-z : indicates Compression, it compresses the data before it is transferred to destination.
-h : indicates Human Readable output format.

1. Sync Files and Directories on Local Machine

In the following command, we will synchronize the file /var/log/syslog from the local machine to the /home/mandar/logfiles directory on the same machine. Here, as a file is single element of the directory, there is no need of using -r option.

$ rsync -zvh /var/log/syslog /home/mandar/logfiles/
In the next command, we will sync all the elements in the /home/mandar directory inside the /root/mandar-bkp directory on the same machine.

$ rsync -avzr /home/mandar/ /root/mandar-bkp/

2. Sync Files and Directories to a Remote Machine

In order to mention a remote host as a SOURCE or DESTINATION, we will follow a syntax : USER@REMOTE-HOST-IP-ADDR/HOSTNAME:/path/to/file-or-directory. It's always better to use IP Address of the remote host.
For a file synchronization,

$ rsync -zvh /home/mandar/osimages/ubuntu-trusty.iso root@192.168.1.11:/home/iso/
To sync a directory,

$ rsync -avz /home/mandar/osimages/ root@192.168.1.11:/home/iso/

3. Sync Files and Directories from a Remote Machine

This will be similar to the previous command, Remote host will be the SOURCE and our local machine will be the DESTINATION.

$ rsync -avz root@192.168.1.11:/home/mandar/docs/ /home/mandar/linux/docs/

4. Sync Files and Directories using SSH Protocol

When synchronizing critical files, you can ensure the transfer to be safe and secure by encrypting the data using SSH protocol. You can mention the protocol you wish to use using the option -e as follows:

$ rsync -avze ssh root@192.168.1.11:/home/mandar/projectX/ /home/mandar/imp/

5. Display Progress of the Operation

The --progress option will display the files that are being transferred or those which are already synchronized along with the timing details.

$ rsync -avz --progress root@192.168.1.11:/home/mandar/docs/ /home/mandar/linux/docs/

6. Include and Exclude Files and Directories

The --include and --exclude options will let you sync selected files/directories from a chunk of data. Both of these options expect a pattern, 'ubuntu*' and '*' here. The following command will sync all the files whose names start with ubuntu and leave alone all other files and directories.

$ rsync -avz --include 'ubuntu*' --exclude '*' /home/mandar/osimages/ root@192.168.1.11:/home/iso

7. Delete Already Existing Files and Directories at Destination

Sometimes, it might happen that, a file which is not present at the source, but is present at the destination. So, you might want the file to be deleted from the target host. In this case, you can use the option --delete.

$ rsync -avz --delete root@192.168.1.11:/home/mandar/projectX/ /home/mandar/imp/

8. Limit the Maximum Size of the Files

In order to avoid transfer of larger files, you can limit the maximum size of the files that are to be transferred using the option --max-size as follows:

$ rsync -avzr --max-size='200K' /var/logs/ root@192.168.1.11:/home/mandar/logfiles/

9. Remove Source Files after Replication

The --remove-source-files option will delete the data that is replicated to the destination host, after successful synchronization.

$ rsync -avz --remove-source-files root@192.168.1.11:/home/mandar/projectX /home/mandar/imp/

10. Limit the Bandwidth Usage

One can also allocate the maximum limit of bandwidth that should be used while synchronizing the data, using option --bwlimit. The bandwidth limit will be in terms of Kilo Bytes Per Seconds (KBPS).

$ rsync -avz --bwlimit=100 /home/mandar/osimages/ root@192.168.1.11:/home/iso

11. Not Overwriting Changed Files at Destination

If the modified version of some file is already available at the destination, you might not want to overwrite the already existing file at the destination. In this case, you can use the option -u.

$ rsync -avzu root@192.168.1.11:/home/mandar/projectX/ /home/mandar/imp/

12. Replicate Only Directories

In order to synchronize only the directories recursively; but not the files included in those directories, you can use option -d as follows:

$ rsync -avzd root@192.168.1.11:/home/mandar/projectX/ /home/mandar/imp/

That's all for this article. This is just the tip of the iceberg. There are plenty of options those are not included in this article, as this article was intended to highlight basic use of the RSync command. For more details, you can always refer to the man pages.

Thanks for reading and stay tuned for more articles on Linux Commands.


0 comments:

Post a Comment

    Total Pageviews