rsync Reference Sheet
One is confronted with a large set of options for the rsync
command for the simple need of copying something correctly. To make things easy, I’ll consider the two scenarios below. See usage examples, or this informative discussion.
The rsync
syntax is like this
rsync options source destination
Note the following on adding a trailing slash from the manual page
A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a
trailing / on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name", but in both cases the at‐
tributes of the containing directory are transferred to the containing directory on the destination. In other words, each of the following com‐
mands copies the files in the same way, including their setting of the attributes of /dest/foo:
rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo
Syntax
To copy and leave old versions of the same file in place, use the command
rsync -av source destination
``
with options such as the following
-v, --verbose increase verbosity
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
--no-OPTION turn off an implied OPTION (e.g. --no-D)
--update
--ignore-existing
--delete
Examples
1. Simple copy
The following copies from source to destination like the cp
command, while preserving attributes
rsync -av source destination
The following two scenarios are opposite to each other
2. Copying new files
Copying only new files and ignore the files that have a modified files in the source
rsync -av --ignore-existing source destination
3. One-way synchronization
Updating the repository
rsync -av --update source destination
4. Two-way synchronization (the “Dropbox setting)
Update if there is a new version in the source, delete if a file is deleted from the source, and add if there are new files.
rsync -av --update remote local
rsync -av --update local remote
5. Backups
Incremental backups
DAY=`date "+%A"`
rsync -av --delete source destination/$DAY
Explanation of -a
Let’s break down the option -a
:
-r, --recursive recurse into directories
-l, --links copy symlinks as symlinks
-p, --perms preserve permissions
-t, --times preserve modification times
-g, --group preserve group
--devices preserve device files (super-user only)
--copy-devices copy device contents as regular file
--specials preserve special files
-o, --owner preserve owner (super-user only)
-D same as --devices --specials
Option -a
can be too strong of a condition and requiring adminstrative privilege. The safe options can be just enough
rsync -hPvrt
and possibly -l
for symlinks, instead of -a
.
More options:
The following are commonly used options
-z, --compress compress file data during the transfer
-h, --human-readable output numbers in a human-readable format
--progress show progress during transfer
-P same as --partial --progress
The following are less commonly used options
-H, --hard-links preserve hard links
-E, --executability preserve the file\'s executability
--chmod=CHMOD affect file and/or directory permissions
If you want the destination folder to mirror the source folder, use the --delete
option. Let’s look at the manual again to understand this command.
--delete
This tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories
that are being synchronized. **You must have asked rsync to send the whole directory (e.g. "dir" or "dir/") without using a wildcard for
the directory’s contents (e.g. "dir/*") since the wildcard is expanded by the shell and rsync thus gets a request to transfer individual
files, not the files’ parent directory.** Files that are excluded from the transfer are also excluded from being deleted unless you use the
--delete-excluded option or mark the rules as only matching on the sending side (see the include/exclude modifiers in the FILTER RULES
section).
Prior to rsync 2.6.7, this option would have no effect unless --recursive was enabled. Beginning with 2.6.7, deletions will also occur
when --dirs (-d) is enabled, but only for directories whose contents are being copied.
This option can be dangerous if used incorrectly! It is a very good idea to first try a run using the --dry-run option (-n) to see what
files are going to be deleted.
If the sending side detects any I/O errors, then the deletion of any files at the destination will be automatically disabled. This is to
prevent temporary filesystem failures (such as NFS errors) on the sending side from causing a massive deletion of files on the destina‐
tion. You can override this with the --ignore-errors option.
The --delete option may be combined with one of the --delete-WHEN options without conflict, as well as --delete-excluded. However, if
none of the --delete-WHEN options are specified, rsync will choose the --delete-during algorithm when talking to rsync 3.0.0 or newer, and
the --delete-before algorithm when talking to an older rsync. See also --delete-delay and --delete-after.
Option -e
-e, --rsh=COMMAND specify the remote shell to use
--rsync-path=PROGRAM specify the rsync to run on the remote machine
--existing skip creating new files on receiver
--ignore-existing skip updating files that already exist on receiver
--remove-source-files sender removes synchronized files (non-dirs)
--del an alias for --delete-during
--delete delete extraneous files from destination dirs
--delete-before receiver deletes before transfer, not during
--delete-during receiver deletes during the transfer
--delete-delay find deletions during, delete after
--delete-after receiver deletes after transfer, not during
--delete-excluded also delete excluded files from destination dirs
--ignore-missing-args ignore missing source args without error
--delete-missing-args delete missing source args from destination
--ignore-errors delete even if there are I/O errors
--force force deletion of directories even if not empty
--max-delete=NUM don\'t delete more than NUM files
--max-size=SIZE don\'t transfer any file larger than SIZE
--min-size=SIZE don\'t transfer any file smaller than SIZE
--partial keep partially transferred files
--partial-dir=DIR put a partially transferred file into DIR
--delay-updates put all updated files into place at transfer\'s end