rsync2u‎ > ‎rsync tutorial‎ > ‎

How rsync works

~/demo1$rsync is efficient.  The first time rsync is run, destination is created and the full source is copied to destination.  Thereafter, only changes in source are copied to destination.  If the --link-dest option is used, unchanged files are hard linked to the previous backup.

A hard link is a pointer to a file.  Hard links have the advantage of using very little memory.  There is an illustrated explanation of hard links on http://blog.interlinked.org/tutorials/rsync_time_machine.html > scroll down to “Hard-Links”.

Here is how the "rsync --link-dest=DIR" algorithm creates files in destination:

    if destination does not exists,
        create destination

    if DIR exists (where DIR is the previous backup),
        compare source to DIR
        hard link unchanged files to DIR
        copy changed files from source
    else
        copy all files from source

By default, rsync does not compressed files.  Restoring files is as simple as a cp command.

rsync syntax

The syntax for making a local backup is:

    rsync [OPTION...] SOURCE... [DESTINATION]

OPTION
This tutorial will use the -ain options.  Here are the option definitions from the rsync man page:

    -a, --archive mode; equivalent to -rlptgoD
        -r        recursive
        -l        preserve links
        -p        preserve permissions
        -t        preserve times
        -g        preserve group 
        -o        preserve owner
        -D        preserve device files, special files
    -i, --itemize-changes   output a change-summary for all updates
    -n, --dry-run           perform a trial run with no changes made
    --link-dest=DIR         hardlink to files in DIR when unchanged


SOURCE
Source is the directory that is backed up.  At least one source is required.  A trailing slash on a source path means "copy the contents of this directory".  Without a trailing slash it means "copy the directory".

DESTINATION
Destination is the directory that source is copied to.  Trailing slash on the destination directory doesn't matter.

Setup source directory for example 1

The tutorial example uses a small directory.  Create a demo1 directory in your home directory:

    user> mkdir demo1

Change to the newly created rsync directory:

    user> cd demo1

Create the source directory:

    ~/demo1$ mkdir source

Now populate the source directory with files:

    touch source/fileA source/fileB

Your source directory should look like this:

    ~/demo1$ ls -AFR source
    source:
    fileA  fileB

In the following example, all commands are from the rsync directory.

Example 1 "link-dest"

This example makes an initial backup, and then makes a second backup (analogous to making full backup and then an incremental backup).  Make sure that destinations are removed:

    ~/demo1$ rm -r dest0 dest1 dest2

Run rsync to make the initial backup (dest1):

    demo1> rsync -ai --link-dest=../dest0 source dest1
    --link-dest arg does not exist: ../dest0
    cd+++++++++ source/
    >f+++++++++ source/fileA
    >f+++++++++ source/fileB

Display the results:

    ~/demo1$ ls -l source/
    -rw-r--r-- 1 wolf wolf 0 2010-02-26 20:00 fileA
    -rw-r--r-- 1 wolf wolf 0 2010-02-26 20:00 fileB

    ~/demo1$ ls -l dest1/source/
    -rw-r--r-- 2 wolf wolf 0 2010-02-26 20:00 fileA
    -rw-r--r-- 1 wolf wolf 0 2010-02-26 20:00 fileB

Tracing the "rsync --link-dest=DIR" algorithm at the top of this page we see what happened.  Since the dest1 directory did not exist, rsync created it.  Since dest0 did not exist, rsync copied all source files to dest1.

On the second backup, we will show that only changed files are copied destination.  Use the touch command to change the modification time of a source file:

    ~/demo1$ touch source/fileB

Run rsync to create a new destination (dest2), refer link-dest to the previous destination (dest1), and backup to the new destination (dest2):

    ~/demo1$ rsync -ai --link-dest=../dest1 source dest2
    >f..t...... source/fileB

Display the results:

    ~/demo1$ ls -l source/
    -rw-r--r-- 1 wolf wolf 0 2010-02-26 20:00 fileA
    -rw-r--r-- 1 wolf wolf 0 2010-02-26 20:03 fileB

    ~/demo1$ ls -l dest1/source/
    -rw-r--r-- 2 wolf wolf 0 2010-02-26 20:00 fileA
    -rw-r--r-- 1 wolf wolf 0 2010-02-26 20:00 fileB

    ~/demo1$ ls -l dest2/source/
    -rw-r--r-- 2 wolf wolf 0 2010-02-26 20:00 fileA
    -rw-r--r-- 1 wolf wolf 0 2010-02-26 20:03 fileB

Tracing the "rsync --link-dest=DIR" algorithm at the top of this page, we see what happened. Since the dest2 directory did not exist, rsync created it.  Since dest1 exited, dest1 was compared to source.

Since fileB changed, dest2/source/fileB was copied from source.  This can be verified by observing the modification time.

Since fileA has not changed, dest2/source/fileA was hard linked from dest1/source/fileA.  dest1/source/fileA and dest2/source/fileA actually point to the same file.  This can be proven by updating a modification time:

    ~/demo1$ touch dest1/source/fileA

Now verify that both fileAs have the same modification time:

    ~/demo1$ ls -l dest1/source/fileA
    -rw-r--r-- 2 wolf wolf 0 2010-02-26 20:06 dest1/source/fileA
    ~/demo1$ ls -l dest2/source/fileA
    -rw-r--r-- 2 wolf wolf 0 2010-02-26 20:06 dest2/source/fileA

dest1/source/fileA and dest2/source/fileA share the same modification time because they are hard links to the same file.

Next: The "exclude-from" option