Typically the word synchronize, in a information perspective, is used to mean the process of sharing information between resources (storage) to make sure that they are consistent. In fact, it’s making sure that a copy of the information on one storage pool is replicated on a secondary storage pool.
Rsync (Remote Sync) is a most commonly used for duplicating and syncing files and internet directories remotely as well as locally in Linux systems. With the help of rsync control you can copy and connect your information remotely and locally across directories,disks and networks, perform data backups and reflecting between two linux system machines.
Some advantages and features of Rsync command:
1. It effectively duplicates and synchronize data files to or from a remote system.
2. Supports duplicating links, devices, owners, categories and permissions.
3. It’s quicker than scp (Secure Copy) because rsync uses remote-update method which allows to exchange just the variations between two places of data files.
4. At first it duplicates the whole content of a data file or a listing from source to location but from next time, it duplicates only the modified blocks and bytes to the destination.
5. Rsync takes in less bandwidth as it uses compression and decompression method while delivering and getting data at both ends.
Basic syntax of Rsync command:
# rsync options source destination
Install rsync in your Linux machine:
While logged on as root, install rsync by executing the following command
yum -y install rsync
Using rsync to Synchronize Files from the Local server to a Remote server:
This command will sync a directory from a local machine to a remote machine. For example: There is a folder in your local server “rsynctest” which contains some files.
rsync -avze ssh rsynctest/ firstname.lastname@example.org:/home/
Copy/Sync a Remote Directory to a Local Machine:
This command will help you to sync a remote directory to a local directory. Here in this example, a directory/home/cpanelp/rsynctest which is on a remote server is being copied in your local computer in /tmp/backup.
rsync -avzhe ssh email@example.com:/home/cpanelp/rsynctest /tmp/backup
Show Progress While Transferring Data with rsync:
To show the progress while transferring the data from one machine to a different machine, we can use ‘–progress’ option for it. It displays the files and the time remaining to complete the transfer.
rsync -avzhe ssh --progress /rsynctest firstname.lastname@example.org:/home/
Use of –include and –exclude Options:
These two options allows us to include and exclude files by specifying parameters, These options helps us to specify those files or directories which we want to include in our sync and exclude files and folders which we don’t want to be transferred.
sync -avze ssh --include 'R*' --exclude '*' email@example.com:/var/lib/rpm/ /root/rpm
Set the Max Size of Files to be Transferred:
We can specify the Max file size to be transferred or sync. We can do it with “–max-size” option.
rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ firstname.lastname@example.org:/root/tmpcr
So this command will transfer only those files which are equal or smaller than 200k.
Automatically Delete source Files after successful Transfer:
Now, assume you have a main web server and a data back-up server, you created a daily back-up and synced it with your back-up server, now you don’t want to keep that local duplicate of back-up in your web server.
So, you don’t need to wait around for exchange to complete and then eliminate those local back-up data file manually. This automated removal can be done using ‘–remove-source-files‘ option.
rsync --remove-source-files -zvh backup.tar /home/backups/
Do a Dry Run with rsync:
If you are a beginner and using rsync and don’t know what exactly your command going to do. Rsync could really ruin the things in your destination directory and then doing an reverse can be a boring job.
Use of this choice will not create any changes only do a dry run of the command and reveals the outcome, if the outcome is exactly same you want to do then you can eliminate ‘–dry-run‘ option from your command and run on the terminal.
rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/
That’s all with Rsync.