rsync

From RaySoft

rsync is a software application for Unix systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction. rsync can copy or display directory contents and copy files, optionally using compression and recursion.[1]

Documentation

Syntax

rsync [PARAMETER ...] SRC [SRC ...] [DST]
rsync [PARAMETER ...] [USER@]HOST:SRC [SRC ...] [DST]
rsync [PARAMETER ...] SRC [SRC ...] [USER@]HOST:DST

Parameters

-a, --archive
This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything.
-b, --backup
With this option, preexisting destination files are renamed as each file is transferred or deleted. You can control where the backup file goes and what (if any) suffix gets appended using the --backup-dir and --suffix options.
--backup-dir=DIRECTORY
In combination with the --backup option, this tells rsync to store all backups in the specified directory. This is very useful for incremental backups. You can additionally specify a backup suffix using the --suffix option (otherwise the files backed up in the specified directory will keep their original filenames).
-C, --cvs-exclude
This is a useful shorthand for excluding a broad range of files that you often don't want to transfer between systems. It uses the same algorithm that CVS uses to determine if a file should be ignored.
The exclude list is initialized to: RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS .make.state .nse_depinfo *~ #* .#* ,* _$* *$ *.old *.bak *.BAK *.orig *.rej .del-* *.a *.olb *.o *.obj *.so *.exe *.Z *.elc *.ln core .svn/ .git/ .hg/ .bzr/
--chmod=PERMISSION
This option tells rsync to apply one or more comma-separated chmod strings to the permission of the files in the transfer. The resulting value is treated as though it was the permissions that the sending side supplied for the file, which means that this option can seem to have no effect on existing files if --perms is not enabled.
In addition to the normal parsing rules specified in the chmod manpage, you can specify an item that should only apply to a directory by prefixing it with a D, or specify an item that should only apply to a file by prefixing it with a F.
--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.
-D, --devices
This option causes rsync to transfer character and block device files to the remote system to recreate these devices. This option has no effect if the receiving rsync is not run as the super-user.
-e COMMAND, --rsh=COMMAND
This option allows you to choose an alternative remote shell program to use for communication between the local and remote copies of rsync. Typically, rsync is configured to use ssh by default, but you may prefer to use rsh on a local network.
--exclude=PATTERN
This option is a simplified form of the --filter option that defaults to an exclude rule and does not allow the full rule-parsing syntax of normal filter rules.
--exclude-from=FILE
This option is related to the --exclude option, but it specifies a FILE that contains exclude patterns (one per line). Blank lines in the file and lines starting with ; or # are ignored. If FILE is -, the list will be read from standard input.
--existing, --ignore-non-existing
This tells rsync to skip creating files (including directories) that do not exist yet on the destination.
-f RULE, --filter=RULE
This option allows you to add rules to selectively exclude certain files from the list of files to be transferred. This is most useful in combination with a recursive transfer.
You may use as many --filter options on the command line as you like to build up the list of files to exclude. If the filter contains whitespace, be sure to quote it so that the shell gives the rule to rsync as a single argument.
-g, --group
This option causes rsync to set the group of the destination file to be the same as the source file. If the receiving program is not running as the super-user, only groups that the invoking user on the receiving side is a member of will be preserved. Without this option, the group is set to the default group of the invoking user on the receiving side.
-h, --human-readable
Output numbers in a more human-readable format. This makes big numbers output using larger units, with a K, M, or G suffix.
--include=PATTERN
This option is a simplified form of the --filter option that defaults to an include rule and does not allow the full rule-parsing syntax of normal filter rules.
--include-from=FILE
This option is related to the --include option, but it specifies a FILE that contains include patterns (one per line). Blank lines in the file and lines starting with ; or # are ignored. If FILE is -, the list will be read from standard input.
-l, --links
When symlinks are encountered, recreate the symlink on the destination.
-L, --copy-links
When symlinks are encountered, the item that they point to (the referent) is copied, rather than the symlink.
-n, --dry-run
This makes rsync perform a trial run that doesn't make any changes. It is most commonly used in combination with the -v, --verbose and/or -i, --itemize-changes options to see what an rsync command is going to do before one actually runs it.
-o, --owner
This option causes rsync to set the owner of the destination file to be the same as the source file, but only if the receiving rsync is being run as the super-user.
-p, --perms
This option causes the receiving rsync to set the destination permissions to be the same as the source permissions.
--partial
By default, rsync will delete any partially transferred file if the transfer is interrupted. In some circumstances it is more desirable to keep partially transferred files. Using the --partial option tells rsync to keep the partial file which should make a subsequent transfer of the rest of the file much faster.
--progress
This option tells rsync to print information showing the progress of the transfer. This gives a bored user something to watch. Implies --verbose if it wasn't already specified.
-q, --quiet
This option decreases the amount of information you are given during the transfer, notably suppressing information messages from the remote server. This flag is useful when invoking rsync from cron.
-r, --recursive
This tells rsync to copy directories recursively.
--stats
This tells rsync to print a verbose set of statistics on the file transfer, allowing you to tell how effective the rsync algorithm is for your data.
--suffix=SUFFIX
This option allows you to override the default backup suffix used with the --backup option. The default suffix is a ~ if no --backup-dir was specified, otherwise it is an empty string.
-S, --sparse
Try to handle sparse files efficiently so they take up less space on the destination.
-t, --times
This tells rsync to transfer modification times along with the files and update them on the remote system.
-u, --update
This forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file.
-v, --verbose
This option increases the amount of information you are given during the transfer.
-z, --compress
With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted - something that is useful over a slow connection.

Examples

Sync two directories and write old files in a backup folder
for dir in 'full' 'incr'; do
  [[ -d "${bkp_path}/${dir}" ]] || mkdir -p "${bkp_path}/${dir}"
done

rsync \
  --archive \
  --backup \
  --backup-dir="${bkp_path}/incr/$(date '+%F_%H-%M')" \
  --chmod='Fugo-x' \
  --cvs-exclude \
  --delete-during \
  --delete-excluded \
  --exclude='.DS_Store' --exclude='.localized' \
  --human-readable \
  --prune-empty-dirs \
  --ignore-missing-args \
  --stats \
  --verbose \
  "${src_path}" "${bkp_path}/full/"
Only sync files with a given file extension e.g. *.md[2]
[[ -d "${bkp_path}" ]] || mkdir -p "${bkp_path}"

rsync \
  --include='*/' --include='*.md' --exclude='*' \
  --archive \
  --delete-during \
  --delete-excluded \
  --human-readable \
  --prune-empty-dirs \
  --stats \
  --verbose \
  "${src_path}" "${bkp_path}/"
Only sync files which are older than 30 days
[[ -d "${bkp_path}" ]] || mkdir -p "${bkp_path}"

find "${src_path}" -type 'f' -mtime '+30' -print0 \
| sed --null-data "s|${src_path%/*}/||" \
| rsync --archive --from0 --files-from=- --verbose "${src_path%/*}" "${bkp_path}"

References