Now I will show an example of how --backup-dir works. By the way, all this is in the rsync manual. If the manual is not sufficiently clear, you can experiment with the rsync command on a small directory to figure out how it works.
I have a directory called "a" which I intend to transfer to a directory called "b".
Also I will use the --delete switch, so that files deleted from the source directory will be deleted by rsync on the destination directory. Some people call this mirroring; after running rsync with --delete, the destination directory will reflect all changes to the source directory, including any deletions that may have occurred since the last rsync.
Code:
$ ls
a
$ ls a
x y z
$ rsync -aviS --delete a/ b/
sending incremental file list
created directory b
cd+++++++++ ./
>f+++++++++ x
>f+++++++++ y
>f+++++++++ z
sent 233 bytes received 72 bytes 610.00 bytes/sec
total size is 28 speedup is 0.09
$ ls
a b
$ ls b
x y z
$
Now I will delete one file and repeat the rsync command:
Code:
$ rm a/x
$ rsync -aviS --delete a/ b/
sending incremental file list
.d..t...... ./
*deleting x
sent 66 bytes received 15 bytes 162.00 bytes/sec
total size is 23 speedup is 0.28
$ ls b
y z
$
You see that it deleted b/x. Let's delete another file and use the --backup and --backup-dir options. Please note, --backup-dir without --backup does nothing.
Code:
$ rm a/y
$ rsync -aviS --delete --backup --backup-dir="c" a/ b/
sending incremental file list
.d..t...... ./
*deleting y
sent 56 bytes received 15 bytes 142.00 bytes/sec
total size is 11 speedup is 0.15
$ ls b
c z
$ ls b/c
y
$
Now you see that the file b/y has been moved to the backup directory b/c.