Have you ever thought of cloning an entire disk or a single partition on a disk? Why will someone need to copy a disk or a partition? Well! Sometimes it is essential to backup an entire disk or a single partition while maintaining its file structure. There could be more than one situations where a disk clone will be required.
Introduction to DD Utility
In Linux, we have the ‘data duplicator’ (dd) utility which is a very powerful utility and is used to convert and copy data. This tool can be used to achieve several objectives including:
- To backup or restore a whole disk or a certain disk partition on a disk.
- To convert the data format on the disk.
- To create new (empty) files of fixed size.
- To copy regions from a raw device files. For example copying a boot sector from a disk.
As mentioned above, we can also use it to make a bit by bit copy of a physical disk. In this article, we will talk about using ‘dd’ utility to copy an internal disk partition to an external USB disk and thus making a full backup of it.
How DD works?
The ‘dd’ utility comes to Linux from the original set of utilities in Unix operating system. By default, the ‘dd’ command reads from the standard input and writes to the standard output if no command line parameters for input and output are mentioned. Therefore the output of this ‘dd’ command can be piped into other commands or processes.
If command line parameters are mentioned, the ‘dd’ command copies data from the input file (or disk) and writes it into the output file (or disk) while optionally converting the format of data if desired.Now let’s use ‘dd’ practically:
Step- 1 Login to the shell (command line)
Since ‘dd’ is an administrative mode command therefore we need to login with administrative privileges. If you are using an ordinary login then you may use ‘sudo’ before ‘dd’. Connect to your linux system where you want to perfom cloning operation using SSH protocol (Launch Termincal Application for localhost or Putty for remote host).
Step-2 Run dd –help
Linux has a beautiful feature, it lets you view the help or user manual for all installed utilities. Below is the exact code syntax to see help for “dd” utility, in this way you will get familiar with different available options that can be used with this command.
An output similar to below will be displayed:
Step-3 Plug-in the USB external hard disk
Prepare and plug in the external USB drive you want to use to store backup to your Linux system. Please make sure that external disk you attach should be greater in size to the partition you are attempt to clone, otherwise you will run into “not enough space on disk” errors during cloning process.
Step-4 Verify that the USB external disk has been detected and installed
Once you have attached external USB disk to your system, verify that your system has properly detected it. Use the following command to list the available disks and partitions:
If the new disk is detected successfully, you will see something as shown in the following screenshot.
Alright, we are all set to initiate the actual clone process. Assume we want to make a clone of the partition ‘/dev/sda2′ onto the external usb disk ‘/dev/sdb’. Following command line parameters used with ‘dd’ utility should take care of this task.
This will synchronize the partition /dev/sda2 to /dev/sdb1. You must verify that the size of /dev/sdb1 should be larger than /dev/sda2.
The output should be something like below:
That’ it, unplug external disk and you have a restore-able backup. You can use this USB to restore your partitions/data on other systems for analysis etc.
There could be many scenarios in which you often need to restore your data from backups or need to create exactly the same copies of partitions/disks. DD utility is one of the most widely used Linux command line tool which caters such needs. There is lot of help available for DD and you can do anything with your disks/partitions using this utility. Hope you enjoyed the article, do let us know in comments please!