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.
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.
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
- 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
-avsignifies archive mode, a shortcut for a handful of other settings. You almost always want to use the “a” flag.
- The “v” flag in
-avsignifies verbose mode, outputting a summary of the files as they are copied.
--deleteflag 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
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.