disk_dump

dd (Disk Dump)

dd (Disk Dump) is a powerful UNIX utility that allows you to copy all or part of a disk in blocks of bytes (the small storage unit of a filesystem). Unlike the « cp » command, the « dd » command allows you to reproduce disk blocks.

Data loss is a disaster, it will have a financial impact on businesses and as a system administrator it can take a toll on your job. So backing up data is a very important thing that you must do as a system administrator.

In this article we will see 7 practical examples of using the dd command to back up the Linux system.

Be careful about using the dd command as you may lose your data.

Copy one disk partition to another on the same disk :

[root@server ~]# dd if=/dev/sdb1 of=/dev/sdb2 bs=512

-if (Input File): indicates the source, ( sdb1 partition in the example above)

-of (Output File): indicates the destination ( sdb2 in the example above)

-bs (Block Size): Indicates the size of the block.

You can also add the parameter « conv = noerror » which allows to continue even in case of error :

[root@server ~]# dd if=/dev/sdb1 of=/dev/sdb2 bs=512 conv=noerror

Remember to change the UUID of one of the partitions to avoid conflict because copying another partition to one partition also copies the UUID. Below are the UUIDs of the two partitions after copying the data :

[root@server ~]# blkid 
/dev/sdb1: UUID="083604d6-ed62-43fe-8d49-3f830ba22bb3" TYPE="ext4" 
/dev/sdb2: UUID="083604d6-ed62-43fe-8d49-3f830ba22bb3" TYPE="ext4"
/dev/sdc1: UUID="083604d6-ed62-43fe-8d49-3f830ba22bb3" TYPE="xfs"
/dev/sde1: UUID="u4M1vo-z01R-4UGk-pz4V-pDOz-dLwK-c1N0Jc" TYPE="LVM2_member"

You can change the UUID with the tune2fs command :

[root@server ~]# tune2fs -U random /dev/sdb2
[root@server ~]# blkid 
/dev/sdb1: UUID="083604d6-ed62-43fe-8d49-3f830ba22bb3" TYPE="ext4" 
/dev/sdb2: UUID="4f282bb3-6dd5-4032-8dd7-0c5dd5cfa8cc" TYPE="ext4"

Copy one disk partition to another on another disk :

[root@server ~]# dd if=/dev/sde1 of=/dev/sdf1

Remember to change the UUID of one of the partitions to avoid the conflict.

Clone an entire hard drive :

We will copy the entire disk (sde) to another (sdf) :

[root@server ~]# dd if=/dev/sde of=/dev/sdf conv=noerror

Create an image of a disc :

Instead of copying one disk to another, you can also create an image of a disk and save it to storage media to restore it afterwards. I will take a practical example so that you can understand :

I have /dev/sdg disk (which size is 20G) in which I created /dev/sdg1 partition whose size is 500MB :

[root@server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdg    8:96  0  20G   0  disk
└─sdg1 8:97  0  500M 0  part

After creating a filesystem (here ext4) on the partition and mounting it (on / data), I put some data in it to make sure the restore will be successful :

[root@server ~]# cp -r /etc/ /data
[root@server ~]# ls -lh /data/
total 5K
drwxr-xr-x. 76 root root 5,0K 16 juin  00:45 etc

Now I create an image of the /dev/sdg disk and put it in the root / :

[root@server ~]# dd if=/dev/sdg of=/image.iso

The image was successfully created :

[root@server ~]# du -sh /image.iso
2,0G    /image.iso

Note that the size of the iso image equals the disk size (20G) and not the partition size (500M) because I have imaged the entire disk and not just the partition.

I will now restore this image to a new /dev/sdd drive :

[root@server ~]# lsblk
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
sdd 8:48 0 20G 0 disk

I will now check the restoration status :

[root@server ~]# lsblk
sdd               8:48   0   20G  0 disk
└─sdd1            8:49   0  500M  0 part

As you can see the sdd1 partition was created automatically with a size of 500MB exactly like that of sdg1.

And the data has been restored :

[root@server ~]# mount /dev/sdd1 /data/
[root@server ~]# ls -lh /data/
total 18K
drwxr-xr-x. 76 root root 5,0K 16 juin 00:45 etc

Back up the MBR of a hard drive :

The size of the MBR is 512 bytes, the command below copies the first 512 bytes of the disk and saves them in a file named MBR :

[root@server ~]# dd if=/dev/sdg of=/MBR bs=512 count=1

count = 1: count indicates the number of blocks (here we have indicated a single block)

Save the boot loader of a hard drive:

The boot loader is located in the first 446 bytes so :

[root@server ~]# dd if=/dev/sdg of=/Bootloader bs=446 count=1

Back up the partition table of a hard drive :

The size of the partition table is 64 bytes and located after the 446 bytes of the boot loader so you have to tell the « dd » command to avoid the first 446 bytes. This is possible with the skip option :

[root@server ~]# dd if=/dev/sdg of=/PartitionTable bs=64 count=1 skip=446