When backing up files, I used to just go with cp
or scp
. However, when transferring large amount of data, rsync
turns out to be a better option as it provides additional features like incremental backup to minimize data transfer. This post summarizes some common use cases of rsync.
Most Common use case:
The simplest use case is:
rsync -azP dir1/ dir2
-a
is similar to-r
. It’s more recommended as it preserves symbolic links, special and device files, modification times, groups, owners, and permissions.-z
reduces the network transfer by adding compression. Typically omitted in local backup.-P
combines the flags--progress
and--partial
. This first flag provides a progress bar for the transfers, and the second flag allows you to resume interrupted transfers:
To just compare two directories:
# Dry run, send incremental file list
rsync -anv dir1/ dir2
Here, option -n
means dry run, same as --dry-run
.
Others interesting features:
By default rsync
does not remove files in destination. In order to really sync between source and destination, --delete
option can be used.
rsync -aP --delete source destination
Option --exclude
can also be used to
rsync -aP --exclude=pattern_to_exclude source destination
References
License
