Backing up your OS with dd
I love dd. Why mess with the clutter and bloat of backup software when all you really want is a simple image for those times when you accidentally wipe out something important or a hard drive goes plonk. dd to the rescue! It’s easy to use, very straight forward, and although there’s no pretty interface, that’s actually a major advantage! Stuck without a pretty gui or forced to connect through a serial cable? no problem!
While still up and running on your regular OS, use a simple ‘mount’ command to figure out which disks and partitions you’re using and write this down. In my example, sda1 is the boot partition and sdb2 is root. Disk ‘a’ has my windows system, and then my boot and swap for linux. Disk ‘b’ is one big partition with my root mount containing the bulk of my os (/home, /var, /usr, … etc.)
[user@hostname]# mount
/dev/sda2 on /boot type ext2 (ro,noatime)
/dev/sdb1 on / type ext3 (rw,noatime)
Find a live image or a gentoo installation disk and use it to boot your computer so you’re not using your regular storage devices. Just about any live distribution will do since you won’t need a gui or networking (keep it simple for now) Once you’re booted up, setup the device you’re going to use to store backups.
Figure out which disk it is by looking at dmesg or by looking at the end of your log file. In my example lets assume it gets connected as /dev/sdc.
[root@LiveCD]# tail -n 30 /var/log/messages
Create a partition and file system on the backup disk to get it ready to hold your backups. Lets just delete the old partitions and create a new one. Once you’re in fdisk, ‘p’ prints the current layout, ‘d’ lets your delete, ‘n’ creates a new partition, … easy stuff. Make a primary partition and select the default beginning and ending. You’ll end up with 1 big partition that’s the size of the whole disk.
[root@LiveCD]# fdisk /dev/sdc
Create a file system on the disk so you can store regular files on it. I usually choose ext2 or vfat if i’m using a usb stick.
[root@LiveCD]# mkfs.ext2 /dev/sdc1
Create a directory on your live cd system then mount your backup disk.
[root@LiveCD]# mkdir /z
[root@LiveCD]# mount /dev/sdc1 /z
Now you have your backup disk mounted at /z and your normal system not mounted at all. This is how you want it. You don’t want to modify anything you’re backing up while you’re in the middle of backing it up!
Now run dd and use the devices as your input and write out files as your backups.
First i’ll backup my boot partition, but since i’m just getting the partition, i’ll also backup the master boot record (MBR) from the beginning of the disk. You’ll want that so you can tell what else was on the disk, and know how big the swap partition was. Plus I just wanted to do it here instead of making a whole new article!
[root@LiveCD]# dd if=/dev/sda2 of=/z/mybackup.boot.sda2.dd
[root@LiveCD]# dd if=/dev/sda of=/z/mybackup.boot.sda.mbr.dd bs=512 count=1
Now i’ll backup the second disk as a whole, grabbing everything in one swoop.
[root@LiveCD]# dd if=/dev/sdb of=/z/mybackup.root.sdb.dd
At this point everything is backed up on my sdc disk. I can unmount it, disconnect it, and reboot my system. I’ll be back to my original OS with a nice snapshot of my system ready to write back to disk. Writing the image back to disk is pretty much the same thing in reverse. I could write it to a new set of disks, say sdd and sde already connected to this system or I could mount my portable device on another machine.
I’ll write out just the partition table without overwriting the unique disk signatures. So just bytes 446 to 509 of the first disk sector will be overwritten with these bytes from my 512 byte MBR backup.
[root@LiveCD]# dd if=/z/mybackup.sda.mbr.dd of=/dev/sdd skip=446 seek=446 bs=1 count=64
Now that the old partition table is on the new disk, I can write out the boot partition.
[root@LiveCD]# dd if=/z/mybackup.boot.sda1.dd of=/dev/sdd1
And i’ll write out the whole disk for the other backup. As long as the output disk is the same size or larger than the backup, I won’t have a problem.
[root@LiveCD]# dd if=/z/mybackup.root.sdb.dd of=/dev/sde















Recent Comments