Copying and restoring the entire disk in Linux with dd

shape
shape
shape
shape
shape
shape
shape
shape

All Linux distributions include a program that gives us the possibility of making reliable copies of the entire disk or partition, as well as restoring it. This is the “dd” command.

Copying disk A to disk B:

dd if=/dev/sda of=/dev/sdb bs=64K conv=noerror,sync

In this example, we are copying the entire “/dev/sda” disk to the “/dev/sdb” disk.

“bs=64K” is the block size. The larger the block size, the faster the transfer, but if there is an error, the entire block will be compromised. The size of 64K is enough to give us a good transfer speed and at the same time reduce the propagation of an error, if there is one.

“conv=sync,noerror” ignores errors and fills with zeros in case of an error.

Copying the disk from a remote server to the local PC:

ssh [email protected] "dd if=/dev/sda conv=sync,noerror bs=64K | gzip -c " | dd of=/root/backup.img.gz

In this example, the entire disk of the remote server “/dev/sda” is copied to the local computer and compressed with gzip.

Restoring an image file with dd:

In the previous example, we copied the disk to the image file in “/root/backup.img.gz”. To restore this image file to disk with dd we run:

gunzip -c /root/backup.img.gz | dd of=/dev/sdb

NOTE: Note that in this example we are assuming that the system has two disks and is installed on another partition, other than /dev/sdb. If you overwrite the same disk on which your system is installed and running, the system will be compromised. The correct thing to do would be to boot up a Live Media containing the backup, and then restore the disk.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest news

Latest news directly from our blog.