Rsync is a very flexible network-enabled syncing tool for replicating and synchronizing documents and Directories remotely and in addition locally in Linux system. With the assistance of rsync command, you can copy and synchronize your data remotely and locally crosswise over directories, crosswise over disks and network, perform data backup and mirroring between two Linux machines. Know more about rsync security advisories.
Some of the additional features of rsync are:
- Support for copying links, devices, owners, groups, and permissions
- Can use any transparent remote shell, including ssh or rsh
- Does not require super-user privileges
- Support for anonymous or authenticated rsync daemons
- rsync allows encryption of data using ssh protocol during transfer.
- Bandwidth used by rsync will be always less compared to other file transfer protocols.
- No special privileges are required to install and execute rsync
# rsync options source destination
Some common options:
-r: copies data recursively
-a: archive mode
-z: compress file data
-h: human-readable format
You can install rsync package using the following command:
# yum install rsync
# apt-get install rsync
Sync Two Directories in a Local Server:
Using the following command you will sync two directories on a local machine from one location to another location.
# rsync -avzh /home/techoism /backups/techoism
Sync Files and Directory to Remote Location:
With the help of rsync command, you will sync a directory from a local machine to a remote machine. Use the following command:
$ rsync -avz /home/techoism/ email@example.com:/home/
Copy/Sync a Remote Directory to a Local Machine:
This command will help you sync a remote directory to a local directory.
# rsync -avzh firstname.lastname@example.org:/home/techoism /tmp/
Rsync Over SSH:
rsync allows you to specify the remote shell which you want to use. You can use rsync ssh to enable the secured remote connection.
# rsync -avzhe ssh email@example.com:/root/anaconda-ks.cfg /tmp/
Same as copy a file from a Local Server to a Remote Server with SSH
# rsync -avzhe ssh techoims.tar.gz firstname.lastname@example.org:/backup/
Show Progress While Transferring Data with rsync:
Use the following command to show the progress while transferring the data from one machine to another machine:
# rsync -avzhe ssh --progress /home/techoims email@example.com:/backup/
Use of –include and –exclude Options:
You can include and exclude files by specifying parameters using rsync command. It helps us to specify those files or directories which you want to include in your sync and exclude files and folders with you don’t want to be transferred.
# rsync -avze ssh --include '*' --exclude 'b*' firstname.lastname@example.org:/backup/ /tmp
Use of –delete Option:
If file or directory does not exist on the source, but already exists at the destination and you might want to delete that existing file/directory at the destination while syncing, then use the following command:
# rsync -avz --delete email@example.com:/var/spool/mail/ .
Set the Max Size of Files to be Transferred:
You can specify the Max file size to be transferred or sync.
# rsync -avzhe ssh --max-size='200k' /backup/code firstname.lastname@example.org:/tmp
Automatically Delete source Files after successful Transfer:
If you want to delete a file from source then use the following command:
# rsync --remove-source-files -zvh techoism.txt /tmp/
Set Bandwidth Limit and Transfer File:
You can set the bandwidth limit while transferring data from one machine to another machine.
# rsync --bwlimit=150 -avzhe ssh /var/spool/mail email@example.com:/tmp/