Home » linux-commands » Backup, Restore, Clone hard-drive or partition with dd command.

Backup, Restore, Clone hard-drive or partition with dd command.

Today a lot of backup substitute available, but the easy backup tool is also already available with every Linux distribution. The ‘dd’ tool merely copies regular input to regular output, read in 512-byte blocks.
Command dd can copy a file to another file, as well as a partition to another partition, or file to partition. This makes dd quite unique, and splendid for cloning partitions and drives.
For instance, to make an exact clone of one partition


Here we are using /boot partition to a backup file, as below.

[root@linuxpcf ~]# dd if=/dev/sda1 of=/mnt/boot.img

The above command will copies the contents of the partition /dev/sda1 ( which is mounted as /boot) to the output file /mnt/boot.img.
Note:- The dd command copies “empty” space too, suppose to if the partition is 256MB in size, and it has only contains 128MB of data, the output file will be 256MB in size.

[root@linuxpcf ~]# df -hT | grep sda1
/dev/sda1     ext2    254M   120M  128M  47% /boot
[root@linuxpcf ~]# dd if=/dev/sda1 of=/mnt/boot.img
427517+0 records in
427517+0 records out
215825024 bytes (256 MB) copied, 2.07951 s, 128 MB/s
[root@linuxpcf ~]# du -sh /mnt/boot.img
204M   /mnt/boot.img

If you would like to be compressed then use below command:

[root@linuxpcf ~]# dd if=/dev/sda1 | bzip2 -9f >/mnt/boot3.img.bz2
427517+0 records in
427517+0 records out
215825024 bytes (215 MB) copied, 31.5072 s, 6.8 MB/s
[root@linuxpcf ~]# du -sh boot3.img.bz2
111M   boot3.img.bz2

Similarly, partitions can be restored from these backup copies:

[root@linuxpcf ~]# dd if=/mnt/boot.img of=/dev/sda1
[root@linuxpcf ~]# bunzip2 -dc /mnt/boot3.img.bz2 | dd of=/dev/sda1

Further if you would like to duplicate an existing drive to another one, Assuming you want to copy /dev/sda and the destination drive is /dev/sdb, first use fdisk to recreate the appropriately-sized (the in a drive should be the same or larger) size partitions, then use dd to do the actual cloning:

[root@linuxpcf ~]# dd if=/dev/sda of=/dev/sdb bs=512 count=1
[root@linuxpcf ~]# dd if=/dev/sda1 of=/dev/sdb1
[root@linuxpcf ~]# dd if=/dev/sda2 of=/dev/sdb2


The first dd call copies the MBR from /dev/sda to /dev/sdb. This will permit the secondary disk to be booted, when it replaces the primary. The first 512 bytes are copied with this command; that is the boot code we need.
This process could take some time it’s Depending on the size of the disk and partitions,, but the end result will be a perfectly cloned system.

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry from last 7 years.

Leave a Reply

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

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud