12 Linux Rsync Options in Linux Explained
In this article, I would show the different rync options to sync file/directories to the destination server. The main advantages of the rsync are:
Speed: Initially copies the whole content between local and remote system. Next, it transfers only the changed blocks or bytes to the destination.
Security: Encrypted form of data that can be transferred using ssh protocol.
Less Bandwidth: rsync uses compression and decompression of the data block by block at both the ends.
Syntax:
#rsync [options] source-path destination-path
1) Rsync Over SSH and Port
With rsync, we can use SSH (Secure Shell) for data transfer, using SSH protocol while transferring our data you can be ensured that your data is being transferred in a secured connection with encryption so that nobody can read your data while it is being transferred over the wire on the internet. Also when we use rsync we need to provide the user/root password to accomplish that particular task, so using SSH option will send your logins in an encrypted manner so that your password will be safe.
a) Copy a File from a Remote Server to a Local Server with SSH
To specify a protocol with rsync you need to give “-e” option with protocol name you want to use. Here in this example, We will be using “ssh” with “-e” option and perform data transfer.
$ rsync -avz -e "ssh -p 922 " cgi@172.27.128.188:/home/cgi/km/rsynx.txt /home/cgi/km/MONITORING/
b) Copy a File from a Local Server to a Remote Server with SSH
$ rsync -avz -e "ssh -p 922 " TeamViewer_Setup.exe cgi@172.27.128.188:/home/cgi/km/
Red Hat Enterprise Linux Server release 6.2 (Santiago)
2) 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 -avhz -e "ssh -p 922 " --progress /home/cgi/km/POLP4 cgi@172.27.128.188:/home/cgi/km/
sending incremental file list
3) Use of –include and –exclude Options with rsync
These two options allows us to include and exclude files by specifying parameters with these option 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.
Here in this example, rsync command will include those files and directory only which starts with ‘R’ and exclude all other files and directory.
$ rsync -avhz -e "ssh -p 922 " --include 'P*' --exclude '*' cgi@192.27.128.188:/home/cgi/km/ /home/cgi/km/MONITORING/
4) Set the Max Size of Files to be Transferred
You can specify the Max file size to be transferred or sync. You can do it with “–max-size” option. Here in this example, Max file size is 200k, so this command will transfer only those files which are equal or smaller than 200k
[cgi@server02:km]$ rsync -avhz -e "ssh -p 922 " --max-size='200k' /home/cgi/km/POLP4 cgi@192.27.128.188:/home/cgi/km/MONITORING/
5) Automatically Delete source Files after successful Transfer
Now, suppose you have a main web server and a data backup server, you created a daily backup and synced it with your backup server, now you don’t want to keep that local copy of backup in your web server. So, will you wait for the transfer to complete and then delete that local backup file manually? Of Course NO. This automatic deletion can be done using ‘–remove-source-files‘ option.
$ rsync -e "ssh -p 922 " --remove-source-files -zvh TeamViewer_Setup.exe cgi@192.27.128.188:/home/cgi/km/MONITORING/
6) Set Bandwidth Limit and Transfer File
You can set the bandwidth limit while transferring data from one machine to another machine with the help of ‘–bwlimit‘ option. This options helps us to limit I/O bandwidth.
$ rsync -e "ssh -p 922 " --bwlimit=100 -avzh /home/cgi/km/POLP4/ cgi@192.27.128.188:/home/cgi/km/MONITORING/
7) Enable Compression
In the below command, rsysnc uses –z to enable compression, -v to verbose and -r to recursive. Here the command synchronizes two directories /home/aloft/ and /backuphomedir in the local system.
[root@localhost /]# rsync -zvr /home/aloft/ /backuphomedir
building file list ... done
.bash_logout
.bash_profile
.bashrc
sent 472 bytes received 86 bytes 1116.00 bytes/sec
total size is 324 speedup is 0.58
8) Preserve File/Directory Attributes
Here we have used –a option to preserve owner and groups, timestamp, symbolic links, permission and recursive mode.
[root@localhost /]# rsync -azvr /home/aloft/ /backuphomedir
building file list ... done
./
.bash_logout
.bash_profile
.bashrc
sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53
9) Synchronize local to remote host
Below option allows you to synchronize between local machines to the remote machine. You can see while synchronizing files to another system, it asks for password. While doing synchronization with the remote server, you need to specify user name and IP or Hostname of the remote system.
root@localhost /]# rsync -avz /home/aloft/ azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/
Password:building file list ... done
./
.bash_logout
.bash_profile
.bashrc
sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53
10) Synchronize remote to local host
The below options will synchronizes files from remote to local system.
[root@localhost /]# rsync -avz azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/ /home/aloft/
Password:
building file list ... done
./
.bash_logout
.bash_profile
.bashrc
sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53
11) Find the difference in files
Option -avzi helps to find any differences in the files or directories between source and destination.
[root@localhost backuphomedir]# rsync -avzi /backuphomedir /home/aloft/
building file list ... done
cd+++++++ backuphomedir/
>f+++++++ backuphomedir/.bash_logout
>f+++++++ backuphomedir/.bash_profile
>f+++++++ backuphomedir/.bashrc
>f+++++++ backuphomedir/abc
>f+++++++ backuphomedir/xyz
sent 650 bytes received 136 bytes 1572.00 bytes/sec
total size is 324 speedup is 0.41
12) To Take backup
rsync command could be used take linux backup.
You can schedule backup in cron using rsync command,
0 0 * * * /usr/local/sbin/bkpscript &> /dev/null
vi /usr/local/sbin/bkpscript
rsync -avz -e ‘ssh -p2093′ /home/test/ root@192.168.1.150:/oracle/data/