This article is dedicated to documenting methods of performing drive imaging (also called bare metal backups, or disk cloning). Drive imaging is a complete copy of all information on a drive, necessary to restore all of the data or entire operating system on a drive to the same state it was when the image was created. This is different from imaging a partition, where one is making a copy of an individual partition that resides on a drive, or backing up individual files and folders.
Please ensure you are comfortable with the information discussed before proceeding. Improperly executing a command may result in partial or complete data loss. Please double- or even triple-check your target device to avoid such catastrophic loss.
Here are the requirements for drive imaging using Ubuntu:
- Exclusive access to the drive being imaged (i.e. the drive being imaged shouldn't be mounted). Live operating system imaging methods, for example, physical-to-virtual (P2V), virtual-to-virtual (V2V), etc. are not covered here.
- The location (remote file share, external USB drive, internal drive, etc.) where the drive image is being backed up to should have the same or more free space then that of the drive being imaging. For example, if you are imaging a 160GB drive, you should have 160GB or more free space to back up to.
- The filesystem of the backup location needs to support the filesize necessary to backup the image as one file.
- An environment to perform the drive imaging. This can be a live environment where one images the data of the computers internal drive.
dd is a universal command line program used for low level copying of data. It will copy the entire drive, even if the used data is only consuming 10% of the beginning of the drive. For example, dd'ing a 100GB drive, where all the data is at the beginning, and is only 10GB is being consumed, the resulting file will be 100GB in size.
In order to find out which drive to clone, open a terminal and execute:
sudo fdisk -l
The output of the command will list each hard drive (ex. /dev/sda).
Backup with dd
The following example will create a drive image of /dev/sda, the image will be backed up to an external drive, and compressed. For example, one may use bzip2 for maximum compression:
sudo dd if=/dev/sda | bzip2 > /media/usb/image.bz2
However, one may use this same concept to change the compression type (gzip, zip, etc.) to one that best suites your needs (higher compression speed, preferred compression format, etc.).
Restoring a drive image
To restore a drive image, one will want to boot into a live environment. Restoration is quite simple, and really just involves reversing the if and of values. This will tell dd to overwrite the drive with the data that is stored in the file. Ensure the image file isn't stored on the drive you're restoring to. If you do this, eventually during the operation dd will overwrite the image file, corrupting it and your drive.
To restore the drive above:
bzcat /media/usb/image.bz2 | dd of=/dev/sda
When restoring the whole drive, the system will not automatically create the devices (/dev/sda1, /dev/sda2, etc.). Reboot to ensure automatic detection.
If you restored Ubuntu to a new drive, and the UUIDs (see UsingUUID for more) changed, then you must change the bootloader and the mount points. One will want to edit the following via a terminal:
sudo nano /boot/grub/menu.lst sudo nano /etc/fstab
To know what the new UUIDs for your drives are, use the following command:
From this list, you can cross-reference the information with that of fdisk to know which drive is which. Then simply update the UUIDs in both GRUB and fstab files.
Clone the contents of a whole hard drive onto another completely different drive
dd if=/dev/sda of=/dev/sdb