How to clone your linux HardDrive with DD

Simon Bennett
Simon Bennett · Mar 07, 2020
How to clone your linux HardDrive with DD Artwork

There are several reasons you may need to clone your hard disk or partition. With cloned hard disk, you don't need to reinstall and configure the applications again. dd is a simple and powerful tool for a Linux-based operating system used to create a perfect copy of drives, partitions, and filesystems.

With dd command, you can backup the boot sector of a hard disk and also obtaining a fixed amount of random data.

Note : Be careful before using DD command, because a small mistake can destroy your whole hard disk. So you should double-check before running any command.

Prerequisites

  • A system with Linux installed.

  • Two hard disk attached to the system.

  • Make sure both hard disks have the same storage size or destination hard disk must be greater than source harddisk.

For the purpose of this tutorial, we are using the Linux system with the following hard disks:

/dev/sdb : 2GB hard disk, we will use this disk as a source disk.

/dev/sdc : 2GB hard disk, we will use this disk as a destination disk.

Basic Syntax Of DD Command

The basic syntax of the dd command is shown below:

dd if=source-disk of=destination-disk [option]

A brief explanation of each option is shown below:

  • if : Stands for an input file.

  • source-disk : This is a source disk from where files are to be cloned.

  • of : Stands for the output file.

  • destination-disk : This is the destination disk to which files are cloned.

  • option : Used to specify the format of the file and speed of the data transfer.

Clone Harddisk with DD Command

In this section, we will clone a hard disk /dev/sdb to /dev/sdc

Before starting, check the size of both hard disk with the following command:

fdisk -l /dev/sdb /dev/sdc

You should see that both hard disk have 1GB of storage:

Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xe7856969 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 2097151 2095104 1023M 83 Linux Disk /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes

Now, create a clone copy of hard disk /dev/sdb to /dev/sdc with the following command:

dd if=/dev/sdb of=/dev/sdc

Once the process has been completed, you should see the following output:

2097152+0 records in 2097152+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 164.828 s, 6.5 MB/s

The above command will copy the hard disk /dev/sdb with its partitions on the harddisk /dev/sdc. You can verify it with the following command:

fdisk -l /dev/sdb /dev/sdc

You should see the following output:

Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xe7856969 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 2097151 2095104 1023M 83 Linux Disk /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xe7856969 Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 2097151 2095104 1023M 83 Linux

The above command can not display the progress and status of the dd command. You can use "status=progress" option with dd command to monitor the progress of the dd command. It gives you a visual display of the total data transferred.

dd if=/dev/sdb of=/dev/sdc status=progress

You should see the progress bar in the following screen:

Clone Harddisk Partition with DD Command

Cloning a hard disk partition is similar to cloning a hard disk. But, you will need to specify a partition that you want to clone. Before cloning a partition, make sure no partitions on that disk are mounted.

For example, to clone a partition /dev/sdb1 to /dev/sdc1 with the following command:

dd if=/dev/sdb1 of=/dev/sdc1 status=progress

Once the process has been completed, you should get the following output:

1072265216 bytes (1.1 GB, 1023 MiB) copied, 172 s, 6.2 MB/s 2095104+0 records in 2095104+0 records out 1072693248 bytes (1.1 GB, 1023 MiB) copied, 172.65 s, 6.2 MB/s

Backup and Restore Harddisk

In this section, we will create a backup image of hard disk /dev/sdb on the first machine and restore this image on hard disk /dev/sdb on the second machine.

On the first machine, connect the USB drive (/dev/sdc1) and mount it on /mnt directory with the following command:

mount /dev/sdc1 /mnt

Next, run the dd command to create a backup image of harddisk /dec/sdb inside /mnt direcotry:

dd if=/dev/sdb of=/mnt/backup.img status=progress

Once the backup image has been created, you should see the following output:

1063304704 bytes (1.1 GB, 1014 MiB) copied, 72 s, 14.8 MB/s 2097152+0 records in 2097152+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 72.7901 s, 14.8 MB/s

Next, disconnect the USB drive and connect it to the second machine.

On the second machine, mount the USD drive (/dev/sdc1) to the /mnt directory:

mount /dev/sdc1 /mnt

Next, run the dd command to restore the image located inside /mnt directory to the harddisk /dev/sdb:

dd if=/mnt/backup.img of=/dev/sdb status=progress

Once the image has been restored, you should see the following output:

1071669760 bytes (1.1 GB, 1022 MiB) copied, 146 s, 7.3 MB/s 2097152+0 records in 2097152+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 146.594 s, 7.3 MB/s

Create a Compressed harddisk Image With DD

If your harddisk is a bit big then you can create a compressed image of hard disk.

For example, to create a backup image of harddisk /dev/sdb with gzip compression run the following command:

dd if=/dev/sdb status=progress | gzip -c > /mnt/backup.img.gz

Once the image has been created, you should see the following output:

1067799552 bytes (1.1 GB, 1018 MiB) copied, 120 s, 8.9 MB/s 2097152+0 records in 2097152+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 120.878 s, 8.9 MB/s

If you want to restore from a compressed image run the following command:

gunzip -c /mnt/backup.img.gz | dd of=/dev/sdb status=progress

You should see the following output:

1068929536 bytes (1.1 GB, 1019 MiB) copied, 162 s, 6.6 MB/s 2097152+0 records in 2097152+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 163.076 s, 6.6 MB/s

Clone/Backup Harddisk on Local Machine to Harddisk on Remote Machine

If you want to clone the hard disk /dev/sdb attached to the local machine to the hard disk /dev/sdc attache to the remote machine with IP address 172.20.10.10 run the following command:

dd if=/dev/sdb status=progress | ssh root@172.20.10.10 dd of=/dev/sdc

Provide the password of the root user on the remote machine and hit Enter to continue:

root@172.20.10.10's password: 1071619072 bytes (1.1 GB, 1022 MiB) copied, 271 s, 4.0 MB/s 2097152+0 records in 2097152+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 271.616 s, 4.0 MB/s 2097152+0 records in 2097152+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 255.8 s, 4.2 MB/s

This method will help you when you need to do migration between different datacenter with the same storage space.

Conclusion

In the above guide, we learned how to clone the hard disk and partition with dd command. We also learned how to clone the hard disk over the network. I hope the dd command will help you to save lots of time.