RSync Guide

rsync Summary

rsync is a file syncing utility, think Dropbox but manual instead of automatic. The rsync utility's main use is to mirror a directory locally or over a network. As the name implies, rsync synchronizes two directories. Key benefits of the utility include:

rsync Cheat Sheet

Basics and where to put the Freaking Slash

To copy a directory from one directory to another, the basic structure is:

rsync -options --otherOptions sourceDir targetDir

Example 1

Copy ~/src into the ~/target directory.

rsync -vaz ~/src ~/target

This example recursively copies the ~/src directory and makes a copy in the ~/target directory. Note: There is no "/" after src. With rsync including or not including the /, matters.

Command Line Options

A few command line options are listed in the previous example. Each of these options is described below. Key rsync Options Option Description

Example 2

This example copies only the files in ~/src/ directory to the ~/target directory.

rsync -vaz ~/src/ ~/target

The ~/target only stores the files

Copying over ssh

To use ssh to copy the files over the network, just add the --rsh option to the command line. Specify the ssh command line as shown in the example. Also, to specify another machine as a target, precede the directory target with a host name and a colon.

Example 3

rsync -vaz --rsh="ssh -l username" ~/src targetHost:~/target

After typing the command line, you will be prompted for your password. After entering the password, the command executes and your files are copied. You can also set the ssh command using the RSYNC_RSH environment variable. You can also avoid entering the ssh password if you use ssh keys.

The --exclude Option

This option allows you to exclude certain files and directories from the copy process. You can exclude by specific names or by using wildcards.

Example 4

rsync -vaz --exclude=log/ --exclude=*.xml ~/bk targetHost:~/test

In this example, the log directory is excluded from the copy as well as any file with a .xml extension.

The --delete Option

This option deletes any files that exist in your target directories but that do not exist in the source directory struction. Using this option truly keeps your files synchronized. However, use this option with caution. It can delete a lot of stuff on the target machine if you aren't careful.

Author: MikeW Date: 2020-02-09