Rsync

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

rsync is a differential, recursive file transfer utility. rsync copies files to or from a remote host, or locally on the current host. For remote transfers, rsync must be installed on both the source and destination machines. rsync does not support copying files between two remote hosts.

rsync may connect to the remote system either using a remote-shell programs, such as ssh or ssh, or directly to the rsync daemon running on the remote system, via TCP. The remote-shell transport is used whenever there is a ":" after the host specification. If not configured otherwise with RSYNC_RSH environment variable or with -e command line option, rsync uses ssh by default. The rsync daemon transport is used when there is a "::" separator after host specification, or when "rsync://" is explicitly specified. If neither the source or destination path specify a remote host, the copy occurs locally.

rsync [options] src [src ... src] dest

If the destination directory does not exist, it will be created.

If a single source argument is specified without a destination, the files are listed in an output format similar to "ls -l".

Trailing Slash Semantics

By default, if the source argument is a directory without a trailing slash, rsync will create that directory in the target directory specified as argument. If the source directory has a trailing slash, only the contents of the source directory will be copied, and no directory matching the top level source directory will be created in the destination directory.

Use Cases

Pull Files from a Remote Directory

Recursively pull all files from the directory /remote-dir on the machine "remote-host" into the /data/local-dir directory on the local machine. The files are transferred in "archive" mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer. Additionally, compression will be used to reduce the size of data portions of the transfer.

rsync -avz remote-host:/remote-dir /data/local-dir

Push Files to a Remote Directory

The reverse operation, recursively transfer the content (all files and directories) of the local 'local-dir' into the remote 'remote-dir'.

rsync -avz /data/local-dir/ remote-host:/remote-dir 

What if I Locally Delete a File and Want to Push

There are situations when I want to keep the remote file area in sync with my local one, including the case when I locally delete a file. I can do that by using:

--delete-during

Excluding Files

Excluding File Patterns

--exclude <pattern>

Example:

--exclude .svn

Multiple patterns can be specified using multiple "--exclude" instances:

--exclude .svn -exclude blah -exclude blah2

Excluding Individual Files

Use the file name as you would use a pattern.