Copying your data as well as backing them up is a day-to-day tasks that we perform regularly. Thus we need a utility to perform these tasks. And as in case of Linux, we can do what we want in various ways using different utilities. Then, our utility today is dd command. As we know everything in Linux is a file and is treated according to this rule even block devices. Which makes dd is useful to copy and backup our data.
What is dd command?
DD is a very powerful and useful utility available on Unix and Unix-like operating systems. As stated in its manual, its purpose is to convert and copy files.
- On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files.
- dd can also read and/or write from/to these files
- As a result, we use dd for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data.
- The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.
How dd works?
DD syntax is a bit different from most of Unix-based utilities, instead of the formula of command -option value, in dd we use the formula of dd option=value. By default, dd reads from stdin and writes to stdout, but we can change this by using the if (input file) and of (output file) options.
Examples
In this section, we’ll see most of the common uses of the command
Backing up the master boot record MBR
The length of this sector is usually 512 bytes: it contains the stage 1 of the grub bootloader and the disk partition table. Suppose we want to backup the MBR of /dev/sda disk. All we have to do is to invoke dd with the following syntax:
In the last example we see the following.
Firstly, we need privileged user to run the command. This is for access the /dev/sda
Instead of standard input and output, we used if and of to specify the source and destination files
bs is for specifying the block size or the amount of data that should be read at a time. This number is in bytes by default and 512 is the default number
count is for the count of blocks to read
Backing up the entire hard disk
To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown.
As a result for the previous example, hard drive mounted in sda will be available in hard drive mounted in sdb
We need to be careful specifying the source and target files so we don’t lose our data
Create image for hard disk
Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices. This method is easier and faster than other types of backups, make it quick to restore data. It creates the image of a hard disk /dev/hda
To restore hard disk using image
As we mentioned, backing up using image is useful for quick restores. We can restore an image file to another hard disk
Backing up a partition
We can backup only a partition instead of the entire hard disk. Also backing up using an image is available for partition as well
Create CDROM backup
dd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.
Burn ISO image to USB
We can burn ISO image, say an OS image, on USB to use it to install OS on other machines.
First navigate to your ISO image directory
After inserting the USB, run the dd command as follows
sudo dd if=archlinux-2017.iso of=/dev/sdb bs=1024k status=progress
Depending on the size of the ISO, it is going to take a while for the process to complete. Do not panic as dd does not show any progress feedback so just wait patiently.
Conclusion
dd is a very powerful and flexible utility for copying, backing up and restore your data, in addition to other uses as burning ISO image on USB.
Know more about this utility by reading its manual and documentation.