Rsync: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 30: Line 30:


  rsync ... -e "ssh <''ssh-options''>" ...
  rsync ... -e "ssh <''ssh-options''>" ...
Example:
rsync -avzhe "ssh <''ssh-options''>" ... root@192.168.1.2:/volume1/tmp ./rsync-target


ssh command line options:
ssh command line options:
Line 40: Line 36:
* "-p" specifies the ssh port.
* "-p" specifies the ssh port.
* "-i" specifies the ssh identity file.
* "-i" specifies the ssh identity file.
Example:
rsync -avzhe "ssh -l <''ssh-user''> -p <''ssh-port''> -i <''ssh-identity-file''> ..." ... 192.168.1.2:/volume1/tmp ./rsync-target


Note that Synology NAS servers do not react well to the "ssh" command, and the rsync path has to be explicitly specified as shown here: {{Internal|rsync with ssh on Synology NAS|rsync with ssh on Synology NAS}}
Note that Synology NAS servers do not react well to the "ssh" command, and the rsync path has to be explicitly specified as shown here: {{Internal|rsync with ssh on Synology NAS|rsync with ssh on Synology NAS}}

Revision as of 01:25, 27 December 2018

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

Installation

Mac

brew install rsync

Source Argument 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.

rsync over ssh

All the usual ssh arguments (-p, -i, etc.) can be passed on the rsync command line as follows:

rsync ... -e "ssh <ssh-options>" ...

ssh command line options:

  • "-l" specifies the ssh user.
  • "-p" specifies the ssh port.
  • "-i" specifies the ssh identity file.

Example:

rsync -avzhe "ssh -l <ssh-user> -p <ssh-port> -i <ssh-identity-file> ..." ... 192.168.1.2:/volume1/tmp ./rsync-target

Note that Synology NAS servers do not react well to the "ssh" command, and the rsync path has to be explicitly specified as shown here:

rsync with ssh on Synology NAS

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 we want to keep the local and remote areas in sync, including the files that have been deleted, locally or remotely. This can be done 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.

Option Reference

-a

Archive mode, same as -rlptgoD (no -H). Archive mode ensures that symbolic links, devices, attributes, timestamps, permissions, user and group ownerships, etc. are preserved in the transfer. Archive mode copies files recursively, so explicit -r is not necessary.

-v

Verbose.

-z

Compress data during transfer.

--delete

Delete local files that don't exist on sender system.

-h

Human-readable, output numbers in a human-readable format.

--rsync-path

Specifies the rsync to run on remote machine.

rsync ... --rsync-path=/usr/syno/bin/rsync ...

Also see:

rsync with ssh on Synology NAS