Backup Script

I back up my computer near-daily, and certainly before taking it out of the house. I used to be lazy enough to just cp -Rv to an external drive, but after a while I got too lazy to wait. I reformatted the drive and cracked open the manual for rsync.

I run Linux and Windows and want to back up both. As with any interoperability issue between the two, open source wins, so I formatted an external Seagate drive to ReiserFS – just one big partition.

I have Windows split into two partitions. One is My Documents, and the other is everything else. These are mounted to /media/windows_home/ and /media/windows/, respectively. The only thing I back up from the main Windows partition is my Application Data directory.

I have only one partition for Linux. It's clearly mounted at /. The external drive gets mounted at /media/usbdisk/.

Now, this script isn't complicated. It doesn't do incremental backups, but it does save loads of time over the more naive method described above. Basically, if you don't back up at all and have a drive lying around, you might as well drop this in and forget about it. Copying this is faster than reading a man page.

#!/bin/sh

# -a is for "archive," which sets good options like "recursive"
RSYNC="rsync -av --delete --delete-excluded --progress"
TARGET="/media/usbdisk"

# --perms preserves permissions, which we overwrite with --chmod.
# Otherwise, Read Only files in Windows are made Read Only in Linux, which
# prevents rsync from copying or deleting them in a later pass
$RSYNC --perms --chmod=Da+x,a+rw --exclude=/Downloads/ /media/windows_home/ $TARGET/windows_home/

# Nothing to explain here
$RSYNC /home/tom/ $TARGET/linux_home/

$RSYNC --perms --chmod=Da+x,a+rw /media/windows/Documents\ and\ Settings/Tom\ Lieber/Application\ Data/ $TARGET/application_data/

Backing Up Over the Network

If you opened up the man page for rsync anyway, you know it has support for synchronizing files over an SSH connection. This is what I am doing temporarily at the moment while I wait for an RMA on my external drive.

Fortunately, this was a simple change. Change the TARGET variable in the above script to something like remote_host:/backup/location.


Comments

Click here to view the comments on this post.