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 specifications 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 the 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)

Syntax

The basic syntax of rsync command is:

# 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

rsync command examples in Linux

sync file and directories on local system

# rsync -zvr /usr/  /root/sync-data/

Above Command will copy or sync all files & directories of /usr folder to /root/sync-data folder.

sync files locally preserving the permissions

-a options do the followings : it provide recursive features & preserves permissions, symblic links, users & group information.

# rsync -azv /usr/ /root/sync-data/

synchronize/copy files from local machine to remote server

# rsync -avz /root/sync-data/ [email protected]:/tmp

The above command will copy the data of /root/syc-data folder to the remote machine with the root user credentials in the /tmp folder.

synchronize/copy files from remote server to local machine

# rsync -avz [email protected]:/tmp/src /opt 
[email protected]'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.

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.

# rsync -avz -e ssh [email protected]:/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.
[email protected]'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

View progress with synchronization

We can use the option “–progress” to view the synchronization progress.

# rsync -avz  --progress [email protected]:/usr   /opt 
[email protected]'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)
............

Include and 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 protected]:/var/lib/rpm/  /opt 
[email protected]'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

The above example includes only the files or directories starting with ‘P’ and excludes all other files. (using rsync exclude ‘*’ )

Delete files at the target if the files are not present at the source

With the help of the “–delete” option in rsync, we can delete the files created at the target if the same files are not present at the source.

# rsync -avz --delete [email protected]:/var/lib/rpm/  /opt

EXAMPLE:9 VIEW THE CHANGES BETWEEN SOURCE & TARGET USING “-I” OPTION

View the changes between source and taregt

You can view the changes between soruce and target files using the “-i” option. For example:

# rsync -avzi [email protected]:/var/lib/rpm/ /opt
 [email protected]'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

Limit the transfer file size

You can force rsync not to transfer files that are greater than a specific size using “–max-size” option.

# rsync -avz --max-size="200k" [email protected]:/var/lib/rpm/ /opt

The 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.