How to rsync your Ubuntu home directory to an external hard drive
This is not hard to do, but backing up my laptop is not something I do often enough to remember exactly how I did it the last time. Using rsync in this way is by no means specific to Ubuntu—but I imagine this guide will be particularly useful for Ubuntu users.
Setup
Plug in your external USB hard drive. Mine shows up with a mount point of /media/0435f0ab-9dfd-4d9d-ae8b-53101d419ac8
, so that’s what I’m going to use below. Yours will be different. You can find out by running the command ls /media
. It’s also very likely that it will show up as an icon on your desktop. Copy the UUID of the drive (that crazy string of numbers and letters) and use it in place of mine in the examples below.
If you’re only ever going to backup one computer, I’d recommend creating a directory on the external hard drive called “backup” to separate your backup from any other files you might have or might want to put on it.
cd /media/0435f0ab-9dfd-4d9d-ae8b-53101d419ac8 mkdir backup
Because at one time or another I’ve had multiple computers, I’ve tended to create directories named after the computer’s model number—x200 in the case of my Lenovo X200.
Backup
Here’s a template of the rsync command I use to backup my home directory to an external hard drive.
rsync -av --delete --exclude=".*/" /home/your_username/ /media/your_uuid/backup
Usage notes:
- Replace your_username with your Ubuntu username and your_uuid with the UUID of your external hard drive. Of course also make sure that the “backup” directory already exists (if that’s where you choose to backup your home directory).
- The “a” flag in
-av
signifies archive mode, a shortcut for a handful of other settings. You almost always want to use the “a” flag. - The “v” flag in
-av
signifies verbose mode, outputting a summary of the files as they are copied. - The
--delete
flag tells rsync to delete any files at the destination (in this case, under/media/your_uuid/backup
) that are not currently at the source (in this case, your home directory). - I don’t like copying over hidden directories (those that start with “.”) which is why I add
--exclude=".*/"
. If you really wanted an exact mirror copy of your entire home directory, then leave this out. - The slash (/) after your_username is important to prevent a directory called your_username from being created in
/media/your_uuid/backup
.
Here’s the actual command I use:
rsync -av --delete --exclude=".*/" /home/jwatt/ /media/0435f0ab-9dfd-4d9d-ae8b-53101d419ac8/x200
Update: don’t use the “z” flag
In the past I also included the “z” flag (as in -avz
) which you’ll often see included by default in most rsync guides and tutorials. This tells rsync to compress (aka gzip) the files before sending them, and then decompress (gunzip) them after sending them. It’s a great feature for textfiles, especially when you’re sending data over a network (which tends to be slower and more costly)
However I noticed that when I was running rsync (with the “z” flag), the CPU on my laptop would shoot up to 100% and the temperature would hit 100°C. I couldn’t understand why, until I realized that most of the files I was backing up were jpegs (photos), large files which can’t be compressed, and that copying them from my local machine to the external drive was causing my CPU to compress and decompress one right after another, which was both slow, pointless, and extremely processor intensive. Leaving out the z flag speed up the operation by more than an order of magnitude, not to mention making my CPU cooler and happier.
Note: Apparently there’s a --skip-compress
flag that allows you to specify the extensions of files you don’t want compress (i.e. those that are already compressed), but I couldn’t get it to work. So I submitted a bug. YMMV.
Well, if you exclude .*/ then what is the point of having a backup of the home dir? Most of the programs put their config into dirs beginning with a dot.
In my experience I’ve found that moving a home dir from one install to another, complete with hidden files and directories, results in a broken user account.
Plus for me, it’s always been the data I create that’s the most important, not the program settings and etc.
Thank you :)
Updated the text (primarily for readability and discussion of drive UUIDs), and also added a section about avoiding the “z” flag.
Interesting and informative article – thanks!! Much more straightforward than a lot of the other “rsync” articles out there.
As I see it, rsync is sort-of like “cp” on steroids. It would be interesting to hear your comments comparing rsync with cp, showing how and where they might be most effective.
Correct me if I am wrong, but the main difference between rsync and cp -a -v -u [source, target] in this case is that it removes anything that was removed from the original source, creating a more-or-less exact replica.
thanks, this is a great tutorial. additionally it would be nice to show how an script will be created which calls this command automatically say every day or so…
Chris, the easiest way would be to set up a cronjob. But since my external hard drive is only attached to my computer for the purposes of backing it up, I don’t bother.
Useful article, thanks. According to the man page for rsync 3.0.9 it now excludes quite a few file suffixes from compression by default. The list includes jpg & jpeg.