HOWTO: Backup your data to an external usb hard drive with rsync

July 29, 2006

Linux has a wide range of applications to perform backups, some of them very powerful. In this howto, I’m going to introduce you to rsync, a powerful yet simple command line application. With rsync we can perform exact copies of directories, partitions or whatever, mantaining the file permissions, soft links, and all. So, you have an exact copy. Running rsync daily allows us to mantain a synchronized copy of a partition to an external hard drive. If something wrong happens, we will have the latest copy of that partition saved in an external device. In my case, I always have an exact synchronized copy of my /home directory. If I have to upgrade my OS, I install the latest OS and then just copy the files to my /home directory without losing permissions, obviously if I mantain the same username.

Backing up with rsync

Type:

rsync -av – -stats /sourcedirectory /targetdirectory

With this command, I’ll have an exact copy of sourcedirectory in targetdirectory. Take into account if you delete some file in sourcedirectory and it has been backed up previously it will not be deletec in the targetdirectory in future backups. To delete it, just type:

rsync -av – -delete – -stats /sourcedirectory /targetdirectory

If you want to exclude a directory from being backed up, then use the – -exclude command like follows:

rsync -av – -exclude=directorytoexclude – -delete – -stats /sourcedirectory /targetdirectory

where directorytoexclude is the directory’s name. It is not necessary to be the whole path.