Thursday, April 21, 2011

Backup and Restore using rsync

I backup my data onto a second hard disk in my PC, like this:

rsync -av --delete /disk1/my/data /disk2/backup > backup.log 2> backup.err

This creates a folder called data in /disk2/backup and copies the contents of the folder /disk1/my/data to it.

If I rerun this command, it will copy only the changed files/directories from /disk1/my/data into /disk2/backup/data.

Restore is simple; /disk2/backup/data is like a mirror. Just browse and copy back whatever is needed.

Note: Using /disk1/my/data/ (with the trailing slash) instead of /disk1/my/data does not cause the directory data to be created. The contents of data are copied directly into backup.

For a one-time copy to an external hard disk, I did this:

rsync -avW --no-compress --delete --modify-window=2 /path/to/src  /path/to/dst > bakup.log 2> bakup.err

Compression is useful for network transfers but can be skipped for local copying. The 'modify-window' option was required since the destination was on an NTFS drive, and there could be time stamp issues when copying from ext3 to NTFS. The 'W' option (copy whole files) might speed things up since partial file transfer need not be supported.