Rsync

Notes about rsync.

Basic usage

To backup /the/source directory in to /the/destination/…

rsync -avP --delete --exclude='a/dir/to/exclude' /the/source /the/destination/

The a option causes rsync to use ‘archive’ mode, which preserves . v increases verbosity.

P is short for --partial --progress, to keep partially transferred file in case the transfer is interrupted and show progress.

The --delete option allow files to be deleted from /the/destination/source if they no longer exist in /the/source

You can exclude multiple paths by using --exclude='whatever' multiple times.

Where is my file during transfer?

Rsync will keep a temporary hidden file during the transfer. E.g. if transfering file.txt you may find a file called .file.txt.Ut8Ban (or similar) while the file is being transferred.

Exclude usage

The --exclude option will pattern-match against the source paths.

The pattern is relative to the files you are syncing, so don’t use the full path as the exclude pattern. Also, from my tests it seems that the pattern will match at and point in the relative path.

Some examples will best illustrate these points.

The intention is to sync ~/rsynctest/source into ~/rsynctest/destination, excluding ~/rsynctest/source/accounts but including ~/rsynctest/source/test/accounts. Got it? Ok…

Excluding a full path will not work:

me@pc ~/rsynctest $ rsync -av --exclude='~/rsynctest/source/accounts' source/ destination
sending incremental file list
./
test.txt
accounts/
accounts/test.txt
test/
test/accounts/
test/accounts/test.txt

sent 306 bytes  received 84 bytes  780.00 bytes/sec
total size is 0  speedup is 0.00
me@pc ~/rsynctest $ ls destination/
accounts  test  test.txt
me@pc ~/rsynctest $ ls destination/test
accounts
me@pc ~/rsynctest $

So the accounts directory in ~/source/accounts was included in the sync when we wanted to exclude it.

Excluding only accounts will also exclude the subdirectory that we want to keep:

me@pc ~/rsynctest $ rsync -av --exclude='accounts' source/ destination
sending incremental file list
./
test.txt
test/

sent 125 bytes  received 38 bytes  326.00 bytes/sec
total size is 0  speedup is 0.00
me@pc ~/rsynctest $ ls destination
test  test.txt
me@pc ~/rsynctest $ ls destination/test
me@pc ~/rsynctest $

The correct way to achieve what we want is to use a pattern that only matches the directory we want to exclude. We can do this by adding a forward slash, to specify only the accounts directory at the root of the source:

me@pc ~/rsynctest $ rsync -av --exclude='/accounts' source/ destination
sending incremental file list
./
test.txt
test/
test/accounts/
test/accounts/test.txt

sent 213 bytes  received 61 bytes  548.00 bytes/sec
total size is 0  speedup is 0.00
me@pc ~/rsynctest $ ls destination/
test  test.txt
me@pc ~/rsynctest $ ls destination/test
accounts
me@pc ~/rsynctest $ 

Rsync over SSH via a port

rsync -avz --rsh='ssh -p8990' 192.168.1.30:/mnt/shared/ .

The port in this case is 8990 and we’re connecting to 192.168.1.30 to backup /mnt/shared to the current directory.

Last modified: 17/02/2013 Tags:

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top