The Magic of Rsync

Without question, rsync is one of a handful tools required for any systems administrator to keep their heads above water. It hooks in wonderfully with other tools to allow synchronization of files across a wide variety of media.

COW, unionfs, aufs – they all rely on a read-only source, with any changes getting written to a separate area. From the perspective if the user, both areas appear to be a single source, all writable. For an operating system, a builder can create a master copy, allowing different types of hardware adjust where necessary: one copy, infinite combinations.

While rsync does not provide real-time access as described above, it does allow for a system administrator to look at two given trees and see what has changed over time.

For a basic example, look at the following:

example $ mkdir foo
example $ touch foo/{1,2,3,4,5}
example $ ls foo/
1  2  3  4  5

There are five files in $PWD/foo, 1, 2, 3, 4, 5. Now, using rsync to back them up:

example $ rsync -av foo/ backup/
sending incremental file list

sent 273 bytes  received 110 bytes  766.00 bytes/sec
total size is 0  speedup is 0.00
example $ ls backup/
1  2  3  4  5

Here, a perfect copy of the files 1, 2, 3, 4, and 5 are made from the source (foo) to the destination (backup).

For the next example, $PWD/foo gets new data, and only the delta should be placed in a new directory:

example $ touch foo/{a,b,c,d,e}
example $ ls foo
1  2  3  4  5  a  b  c	d  e
example $ rsync -av --compare-dest=$PWD/backup foo/ delta/
sending incremental file list

sent 323 bytes  received 110 bytes  866.00 bytes/sec
total size is 0  speedup is 0.00
example $ ls foo
1  2  3  4  5  a  b  c	d  e
example $ ls backup
1  2  3  4  5
example $ ls delta
a  b  c  d  e

Nice! If I overlay delta/ on top of backup/ then it will equal foo/. This is a pretty basic example, but it is apparent that if this is combined with cron, some pretty fancy things can happen.

For example, if you needed to know any file changes (and what they were) from any point in the past, this process would allow it.

Comments are closed.