How to backup Linux OS using “dd” Command?

dd” command can be really handy when it comes to taking an Operating System backup to clone the disk the OS is installed on. Here are few examples of using dd command for taking OS backup.

 

NOTE: The target drive must be either of identical size to the drive being cloned or larger. The dd command will copy both used and unused space from the target.

 

1. Backup Entire Hard disk To another DISK

 

1. In Below example we want to clone disk “sda” and have an identical disk on the server as “sdb”.

# fdisk -l
Disk /dev/sda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00010897

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1567 12069888 8e Linux LVM

Disk /dev/sdb: 12.9 GB, 12884901888 bytes
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
 

 

2. To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command. The UNIX device name of the source hard drive is /dev/sda, and device name of the target hard disk is /dev/sdb, sync option allows to copy everything using synchronized I/O.

# dd if=/dev/sda of=/dev/sdb conv=noerror,sync
25165824+0 records in
25165824+0 records out
12884901888 bytes (13 GB) copied, 453.846 s, 28.4 MB/s 

 

Here,

if: source disk drive (/dev/sda)

of: destination disk drive (/dev/sdb)

bs: read and write BYTES at a time (default is 512 Bytes, You can use bs=64k for bigger disks)

noerror: continue after read errors.

sync: use synchronized I/O for data, also for metadata

 

3. If want To restore data to original disk you need to repeat the previous step with the correct source(sdb) and destination(sda). You can even remove sda and boot from sdb in this case.

 

CAUTION: Reversing the arguments within a dd command can lead to erasing all of your precious data. Make sure to know the location and names of both your source(if=) and your target(of=).

 

2. Backup Entire Hard disk To disk image

 

1. You can create an image file of the hard disk and save it in other storage devices. There are many advantages to backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe. Use the command below to backup the hard disk /dev/sda to a disk image file.

 # dd if=/dev/sda of=/var/tmp/sda_disk.img

 

Here,

if: source disk drive (/dev/sda)

of: destination disk drive (/dev/sdb)

 

2. To restore a hard disk with the image file of an another hard disk, use the following dd command example.

# dd if=sda_disk.img of=/dev/sdc 

 

Here,

if: source disk drive (/dev/sda)

of: destination disk drive (/dev/sdb)

 

3. Backup Entire Hard disk To disk image on NFS and restoring it

 

1. Check if there is enough space on nfs server available.

# showmount -e nfs_server_IP
Export list for xx.xx.xx.xx:
/nfsshare * 

 

# df -h
Filesystem                Size   Used  Avail  Use%  Mounted on
xx.xx.xx.xx:/nfsshare     16G    44M   15G    1%    /nfs_test 

 

2. Create the image. Make sure you have enough bandwidth available.

 # dd if=/dev/sda of=/nfs_test/sda_disk.img
25165824+0 records in
25165824+0 records out
12884901888 bytes (13 GB) copied, 263.396 s, 48.9 MB/s

 

Here,

if: source disk drive (/dev/sda)

of: destination disk drive (/dev/sdb)

 

3. To restore from the backup disk image on nfs (such as if there is a disk failure on disk sda and the system can’t boot), boot into rescue mode with networking.

 

4. Mount the nfs share. Let’s say newly created directory /nfsshare.

 

5. Restore sda using disk image backup.

 # dd if=/nfsshare/sda_disk.img of=/dev/sda

 

Here,

if: source disk drive (/dev/sda)

of: destination disk drive (/dev/sdb)

 

6. Reboot the server.

 # shutdown -r now

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)