Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.
Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks for files that have changed in size or in last-modified time. Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file’s data does not need to be updated.
Some of the additional features of rsync are:
- support for copying links, devices, owners, groups, and permissions
- exclude and exclude-from options similar to GNU tar
- a CVS exclude mode for ignoring the same files that CVS would ignore
- can use any transparent remote shell, including ssh or rsh
- does not require super-user privileges
- pipelining of file transfers to minimize latency costs
- support for anonymous or authenticated rsync daemons (ideal for mirroring
# rsync options source destination
Some of the Commonly Used Options :
- –delete : delete files that don’t exist on sender (system)
- -v : Verbose output
- -e “ssh options” : specify the ssh as remote shell
- -a : archive mode
- -r : recurse into directories
- -z : compress file data
Example : 1 Sync files & Directories on local system
[root@linux ~]# rsync -zvr /usr/ /root/sync-data/
Above Command will copy or sync all files & directories of /usr folder to /root/sync-data folder.
Example:2 Sync files Locally along with preserve permissions using “rsync -a”
-a options do the followings : it provide recursive features & preserves permissions,symblic links,users & group information
[root@linux ~]# rsync -azv /usr/ /root/sync-data/
Example:3 Synchronize / Copy Files From Local Machine to Remote Server
[root@linux ~]# rsync -avz /root/sync-data/ firstname.lastname@example.org:/tmp
Above command will copy the data of /root/syc-data folder to the remote machine with the root user crendetails in /tmpfolder.
Example:4 Synchronize / Copy Files From Remote Server to Local Machine
[root@linux ~]# rsync -avz email@example.com:/tmp/src /opt firstname.lastname@example.org's password: receiving file list ... done src/ src/debug/ src/kernels/ sent 38 bytes received 99 bytes 10.96 bytes/sec total size is 0 speedup is 0.00
Above Command will copy remote server’s /tmp/src folder to local machine’s /opt folder.
Example:5 Remote synchronization Over SSH
rsync allows us to synchronize files to remote machine & vice versa over ssh i.e secure communication. Use “rsync -e ssh” to specify which remote shell to use. In our case, rsync will use ssh.
[root@localhost ~]# rsync -avz -e ssh email@example.com:/var/lib/rpm /opt The authenticity of host '192.168.2.149 (192.168.2.149)' can't be established. RSA key fingerprint is 45:fc:99:57:34:ba:6a:bb:e3:6f:00:bb:ca:15:3d:c3. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.2.149' (RSA) to the list of known hosts. firstname.lastname@example.org's password: receiving file list ... done rpm/ rpm/.rpm.lock rpm/Basenames rpm/Conflictname ............ rpm/Triggername sent 422 bytes received 12521670 bytes 115410.99 bytes/sec total size is 35467264 speedup is 2.83
Example:6 View Progress while synchronization using “- -progress” option
[root@localhost ~]# rsync -avz --progress email@example.com:/usr /opt firstname.lastname@example.org's password: receiving file list ... 44609 files to consider usr/ usr/tmp -> ../var/tmp usr/bin/ usr/bin/.fipscheck.hmac 65 100% 63.48kB/s 0:00:00 (xfer#1, to-check=44605/44609) usr/bin/.ssh.hmac 65 100% 63.48kB/s 0:00:00 (xfer#2, to-check=44604/44609) usr/bin/GET 14519 100% 248.75kB/s 0:00:00 (xfer#3, to-check=44603/44609) usr/bin/HEAD 14519 100% 232.44kB/s 0:00:00 (xfer#4, to-check=44602/44609) ............
Example:7 include & exclude options in rsync
rsync allows us to specify the pattern that we want to include and exclude files or directories while doing synchronization.
# rsync -avz --include 'P*' --exclude '*' email@example.com:/var/lib/rpm/ /opt firstname.lastname@example.org's password: receiving file list ... done ./ Packages Providename Provideversion Pubkeys sent 129 bytes received 9395561 bytes 695977.04 bytes/sec total size is 28999680 speedup is 3.09
In the above example, it includes only the files or directories starting with ‘P’ and excludes all other files. (using rsync exclude ‘*’ )
Example:8 Delete files at the target if the files are not Present at Source.
With the help of “–delete” option in rsync , we can delete the files created at the target if the same files are not present at source.
[root@localhost ~]# rsync -avz --delete email@example.com:/var/lib/rpm/ /opt
Example:9 View the Changes between Source & Target using “-i” option
[root@localhost ~]# rsync -avzi firstname.lastname@example.org:/var/lib/rpm/ /opt email@example.com's password: receiving file list ... done >f+++++++ .rpm.lock >f+++++++ Basenames >f+++++++ Conflictname >f+++++++ Dirnames >f+++++++ Filedigests >f+++++++ Group >f+++++++ Installtid >f+++++++ Name >f+++++++ Obsoletename >f+++++++ Requirename >f+++++++ Requireversion >f+++++++ Sha1header >f+++++++ Sigmd5 >f+++++++ Triggername sent 328 bytes received 3126214 bytes 297765.90 bytes/sec total size is 35467264 speedup is 11.34
Example:10 Limit the transfer file size using “- -max-size”
You can force rsync not to transfer files that are greater than a specific size using “–max-size” option.
[root@localhost ~]# rsync -avz --max-size='200k' firstname.lastname@example.org:/var/lib/rpm/ /opt
Above command makes rsync to transfer only the files that are less than or equal to 200K. We can indicate M for megabytes and G for gigabytes.