Backing up is something we should all do, but we never seem to remember to do it. Then, when something breaks, we aren't happy.
Actually, I'm fairly good about backing up. I do it once a month (on my main desktop machine) and I backup everything important to a removable 200 GB drive. At the moment, I'm still using a proprietary backup solution. It backs up Windows and Linux partitions from within a Windows GUI interface.
However, you don't have to use proprietary backup solutions and neither do you have to have a Windows dual boot to backup your Linux stuff.
Today, I'm going to show you how to use dd. It's a low-level copying tool that allows you to do many things - but in this tutorial we're going to use it to make backup images of whole partitions, and restore them too!
Just a word of warning - this tutorial is geared more towards Linux users that have had some experience doing command line stuff and that know a little bit about hard drives and partitions. If you don't know how to su to root, or you don't know what an IDE channel is, then this tutorial probably isn't for you. It's geeky, you've been warned. Also, be careful. dd is powerful and if you accidentally type the wrong thing, you could lose data. Be sure you know what you are doing, and think before you type (or at least before you press Enter to execute a command).
For this whole tutorial, we're going to be using root privileges, as we're going to be doing things at a low level. Don't login as the root user, simply open a terminal and type the following:
Enter your root password when prompted. Now you are root inside this terminal (Ubuntu users, you can't do this, so prefix every command in this tutorial with sudo).
Have enough disk space!
If we're making image files of whole partitions, you will obviously need to make sure you've got enough disk space to fit them. dd copies the bits exactly as they are - it ignores filesystems and the like so your images will be exactly the size of your partitions, regardless of whether you've used that much space. Get yourself a backup hard drive, or use a removable one or something. There is no point (and it won't work) backing up a partition to an image file stored on that same partition.
Identifying your partitions
First of all, you need to know which partition is which. If you understand what hda, hdb and hda5 are, then you don't need to read this bit, jump to the next heading.
For IDE hard disks, each physical drive on your machine is given a letter (starting from a). You'll usually only have up to 4 IDE disks anyway (including CD/DVD drives), so for the sake of this tutorial we'll call them hda (first IDE channel master), hdb (first IDE channel slave), hdc (second IDE channel master) and hdd (second IDE channel slave).
If you've got SCSI or SATA drives, they may start with sd instead of hd - for this tutorial, I'll focus on IDE.
This letter identifies your physical drive, then there is a number for the partition.
You can have up to four primary partitions, so they will be numbered from 1 to 4. There's also extended partitions, which essentially are a 'container' for logical drives. Extended partitions are usually number 4.
Logical drive numbers start at 5 (regardless of how many primary partitions you have).
So, here's a quick table showing some of the possible identifiers for different partitions:
Identifier | Description |
---|---|
hda1 | First primary part on IDE channel 1 master |
hda2 | Second primary part on IDE channel 1 master |
hda5 | First logical drive on IDE channel 1 master |
hda7 | Third logical drive on IDE channel 1 master |
hdb4 | Usually the extended partition on IDE channel 1 slave |
Do you get the idea? (If not, this Wikibook might help.)
Good. Now let's look at what Linux thinks our drives and partitions are like. Run the following command (be careful, as fdisk is very powerful and potentially dangerous):
You'll get a list of partitions. From this, work out which partition you want to back up from the first column (/dev/hdxY).
But anyway, that's enough of that. With a bit of luck you should now be able to find out which partition you want to back up.
Making an image
Now, let's get making some images!
To start, let's cd to the folder where we want to back up to (you've mounted your backup drive, right?). Replace /mnt/backupdrive with your backup location.
Now, let's make an image. For this demo, I'll use /dev/hda1.
As you can see, dd takes two arguments - if (input file) and of (output file). dd will then chug away for a while, and it will display some stats about how much data has been pumped through it before quitting to the terminal when it finishes.
Please be warned - it's not a good idea to back up the same partition the OS is running off, as data might change on the drive during the backup. Boot off a Linux Live CD, or use an alternative OS on the same machine.
If you have other drives/partitions, rinse and repeat. That's it - your backups are done.
Restoring backups
To restore your backups, you'll need to have a partition exactly the same size as the image. Write down the number of cylinders (take the second column from the first from fdisk -l) so you can create a partition this size later.
Once you're partition is made, don't format it. We simply dd the image back. Be warned - if you perform this step now, you will actually restore the data. Do not perform this step unless you really want to restore your backup and never restore the same partition the OS is currently running from. It will not be pretty.
Backup and restore, the geeky way
I think that just about covers it. Provided you understand the way Unix represents files/partitions, it's actually frighteningly simple.
If you're really geeky, you could set up some shell scripts to automate this for you, and then schedule them via cron, but that's another tutorial. Enjoy.