As with any aspect of Linux, there are myriad ways to do a single task. Everything from your desktop GUI, running a web server, checking logs, and backing up your system. Finding how to do a task that best suits your need can sometimes be a drawn-out process. But, when you’re looking for a tool to handle a bare metal backup, your quest might very well begin and end with the dd command.
The dd command is actually a reference to the DD statement from IBM’s Job Control Language and means Data Description. The primary purpose of dd is to convert and copy files. With dd you can easily copy a partition or an entire drive.
The feature list includes:
Backup and restore a partition or entire drive
Copy regions of raw device files
Convert data formats
Creating files with fixed sizes
Most importantly, however, the dd command is the oldest disk imaging tool still in use. Why is it still around? Because it’s powerful, reliable, and (most importantly) simple to use.
I will walk you through the process of using dd to create a clone of a drive so that you can then restore that drive from the written image.
Proceed with caution
The first thing you absolutely must know is that reversing the arguments within a dd command can lead to erasing all of your precious data. To that end, you must use caution when working with this tool. Make sure to know the location and names of both your source and your target.
You will also want to run the dd command from within a live Linux distribution. The dd command copies every byte (even unused space), so it’s an intensive process. Working with dd while booted into a production machine could cause data corruption (which is why we run it from a live disk). This means downloading your favorite distribution ISO, burning it to either a DVD or USB drive.
With those warnings in place, let’s get up to speed with dd.
Another very important issue is that the target drive must be either of identical size to the drive being cloned or larger. The dd command will copy both used and unused space from the target. Say, for example, you have Linux installed on a 250GB drive and it is currently only using 100GB of space. The dd command will not discern between the used and unused space, so the image will be of the full drive: 250GB. You can help yourself out with this by creating a compressed image (more on that in a bit).
The first thing you need to do is boot into your live distribution. It won’t matter which distribution you are using, because almost every flavor of Linux includes the dd command. I’ll be demonstrating from a beta release of Elementary OS Loki. Boot into that live distribution and then attach your external drive. The external drive you attach should not have any necessary data (as the dd command will overwrite it). Best to use an empty, formatted drive for this purpose.
Open up a terminal window (from within the live instance) and check to make sure the drive to be cloned is, in fact, labeled sda. I do this with the command grep sda /var/log/syslog. The output (Figure 1), should indicate that /dev/sda is the location of your source to be copied.
Next, you need to find the location of the attached external drive. To do this, issue the command mount. A good amount of data will fill the window, but you should be able to easily track down the line associated with your target device (the external drive). Most likely, it will be found at /dev/sdb.
Cloning the drive
For the sake of simplicity, let’s assume our source is /dev/sda and our target is is /dev/sdb1. You could copy a single partition from that drive (say, /dev/sda1), but we want to copy the entire drive. To clone sda to sdb1, we’ll issue the command:
sudo dd if=/dev/sda of=/dev/sdb1 bs=64K conv=noerror,sync
The arguments above are as follows.
bs: Set the block size for copy (the default is 512 bytes but doesn’t work well for larger drives)
noerror: Instructs dd to continue operation, ignoring all read errors
sync: Instruct dd to fill input blocks with zeroes if there were any read errors
Once you issue the command, step away from the computer, as this will take quite some time to accomplish. As the command works, you will see nothing printed on the screen; your prompt will seem to have locked. If you’d prefer to see the status of the command as it works, you can include the status=progress argument like so:
sudo dd if=/dev/sda of=/dev/sdb1 bs=64K conv=noerror,sync status=progress
The above addition will keep you informed as to how much has copied, in real time.
You can also opt to clone the disk to an image file. This can make the restoration process a bit easier and create a much smaller file on your target drive. To create an image, we’ll use the dd command like so:
sudo dd if=/dev/sda conv=sync,noerror bs=64K | gzip -c > /PATH/TO/DRIVE/backup_image.img.gz
Where /PATH/TO/DRIVE/ is the actual path (on my example, it would be /media/elementary/TOSHIBA/). The above command will create a compressed image of your drive.
My preference is to always clone to an image, as opposed to making a bit-by-bit copy and simply dumping to another drive. One of the benefits of this is the ability to move that image file to wherever you need it. If you are only planning on cloning a target to a source and then using the source as a replacement drive, then you don’t have to bother creating an image file. If, however, you want to backup your drive to an image that can then be used to create multiple copies or need to transport the image (for whatever reason), the latter process will be better suited for the task. Another benefit is that, by creating an image, the empty space will compress very nicely, so the end result will be a much smaller file.
Restoring the copy
I’ll demonstrate how to restore with dd using an image file. As with the copying process, this must be done while booted into a live distribution. I’ll assume the target drive has been installed into the machine, you’ve attached the USB drive (containing the backup image), and you’ve booted into a live distribution.
Once the above dd command completes, you’ll have a file called backup_image.img.gz, located on your external drive. To restore that image to a new drive (we’ll restore it to new drive sda, one that is the exact same size as the original), you first must gain su privileges. If you’re running a Ubuntu-based distribution, first issue the command sudo su, type your sudo password, hit Enter, and then run the restore command like so:
gunzip -c /PATH/TO/DRIVE/backup_image.img.gz | dd of=/dev/sda
Where /PATH/TO/DRIVE is the exact path to your backup_image.img.gz file located on the external device.
NOTE: If you are running a distribution that doesn’t make use of sudo (such as Fedora or openSUSE), you will gain administrator privileges with the command su. Type your administrator password and hit Enter. You can then run the restore command as shown above.
Once again, step away, because this process will take quite some time. When it does finish, you should now have a perfectly bootable drive that is an exact copy of the the original. Reboot, remove the live disk image, and fire up your cloned system.
You now have a working understanding of how to make use of the dd command to clone a drive. There is quite a lot more that dd can do. To get the most out of dd, make sure to read the man page by issuing the command man dd.
Learn more about backups and system management through the Essentials of System Administration course from The Linux Foundation.