DD and the Mighty Disk Destroyer or Duplicator
As the title states dd is the Mighty Disk Destroyer, though it can also be your savior. Many Admins fear and even cower when the letters DD are mentioned together; fear not, DD can be your best friend. Being that you are a Linux Professional and or want to be a professional, you are sometimes faced with the need to clone, backup and or restore Hard Drive images On the Fly. Secondly, you many want to create backups of Hard Drives for future deployment. Lastly, sometimes you have a failing Hard Drive that needs to be migrated over to a good Hard Drive.
It is jokingly said that dd stands for "disk destroyer", "data destroyer", "death and destruction", or "delete data", since when used for low-level operations on hard disks, a small mistake, such as reversing the if and of (input and output) parameters, could result in the loss of some or all data on a disk (Wikipedia).
In the past I am sure you have used tools from Norton, or Acronis and while these tools do give you a nice GUI to see and enjoy it also comes with a bill, overhead, and require the use of a Third Parties API. If you have have ever wanted to enjoy the Freedom of Open Source this is your lucky day; DD is the one stop shop for Disk Duplication. The command to initiate anything DD is very simple. To begin I would like to point you to the dd Man pages. You can get there by entering the
# RTFM man dd
This is the official documentation for dd on your
Unix system. You can also read more about dd here. Now that you are more familiar with the command structure, you can really break dd down into a single command and then get more complicated from there:
dd if=/THE/LOCATION/OF/THE/FROM of=/THE/LOCATION/OF/THE/TO
Here is an example of the actual command in real life.
dd if=/dev/sdc of=/dev/sdd
This last command is a Disk Clone from Hard drive
SDC to Hard Drive
SDD<br /> This last command simply clones one Hard Drive to another. We did not specify block sizes, or anything else; This is a simple clone. It should be common knowledge but you need to
UNMOUNT the Hard Drives before you do any of this. What is really cool is you can use this to clone a Windows Hard Drive to another Hard Drive. For instance, if you have a Windows Computer (7, Vista, XP, 2000, ME, 98SE, ect...) you can load your system with a Linux Bootable Live CD/USB drive and then DD your one Hard Drive to another Hard Drive.
I can testify this works with windows, I have just done it. My old 250GB Seagate was slowing down so I bought a new drive and DD'd it. My new 1tb, 7200RM, Mo-Better Drive :D is powering my Windows 7 Computer as I write this Blog Post.
While I do know this works, I also know it can be cumbersome and if you do it wrong it can be terrible. Please note that when you DD something there is no output that says it is working, no GUI that reminds you that there is something happening, and there are no indicators that you are transferring the wrong way. Once you have issued this command the computer will just site there, though after a while it will complete and when it does you will have some output on how the operation went.
Some other very useful commands that you can use with DD is to clone a physical disk into an image file. Yes I said that right, you can save RAW disk data into an image file. When you save the image file to your hard drive, make sure there is enough space to save the RAW file, if not you will be out of space and that can cause problems for other things on your Computer. If you are ready to DD then you will issue this command:
dd if=/THE/LOCATION/OF/THE/FROM of=/THE/LOCATION/OF/THE/TO.img
If you are looking at this last command and realizing it look kind of familiar, you would be right. It is the same command structure as the last command that I had given. The only difference will be the
.img at the end. Here is a real example:
dd if=/dev/sdb of=~/Desktop/GoldImage.img
In this example I am saving the RAW Hard Drive data in a file on my Desktop, and I have named it GoldImage.img While all of this so far is cool, it is not all that practical yet. I mean who would want to save the RAW image size of a hard drive to there Desktop. In my case that would be a 250GB of data in a file on my desktop and I say that is just crazy. This is where the real power of any
LINUX computer comes into play. We can make use of Pipes. If you are not sure what piping is in
Unix, have a look here.
In Linux we use Pipes all of the time, we are able to string together lots of commands to make one big command. Using your knowledge of pipes we can Pipe our DD'd drive so that it is compressed into an image file saved on our hard drive. To do this here is a General Command:
dd if=/THE/LOCATION/OF/THE/FROM | gzip -COMPRESSION-LEVEL > /THE/LOCATION/OF/THE/TO.img
And here is a real world example:
dd if=/dev/sdb | gzip -6 > ~/Desktop/GoldImage.img
In the end there are a tun of things you can do with dd, the last example I will share with you is where you would take a compressed image of a hard drive then send it to a backup server in 1GB chunks, all over an SSH connection while creating a log file on the local machine where you are taking the image. Here is the command though I will leave the syntax for you to figure out:
DRIVE=sda; dd if=/dev/$DRIVE | gzip | ssh USERNAME@SERVER "split -b 1048m -d - $DRIVE-`date +%y%m%d`-`hostname -s`.img.gz" > ~/backup-$DRIVE-`date +%y%m%d`-`hostname -s`.log.txt 2>&1 &
I believe you will be able to use this command effectively when you know what the syntax does.
While this post may be cheerful, DD can be the destroyer of disks. If you are not careful you will lose everything. This is why I am saying that if you use anything mentioned here, backup your data first & do not blame me for your mistakes. If you do blame me I will morn your lose though it's not my fault.