HOW TO: Clone a Drive

  • Post last modified:April 2, 2024
  • Reading time:8 mins read
  • Post comments:0 Comments

Clone a Drive using DD

Cloning a drive using dd in Linux involves creating an identical copy of one storage device and transferring it to another device or saving it as an image file. The dd command, which stands for “data duplicator,” is a powerful utility that reads and writes data at the block level.

dd is a standard utility and comes preinstalled on most Linux distributions. It is part of the GNU Core Utilities, making it available by default in terminal environments. Therefore, you can typically use dd without the need for additional installations.

 

There is typically two different ways to clone a Drive. Creating and saving an image file of the Drive, and directly cloning a Drive to another Drive. Here are the pros and cons of each:

Creating an Image to Clone:

Pros:

  • Backup Flexibility: An image file allows you to create a backup that can be stored separately. You can keep multiple versions, easily transfer it to another system, or even share it with others.
  • Incremental Backups: Some tools allow you to create incremental backups, saving only the changes since the last backup. This can save storage space and time.
  • Compression and Encryption: Image files can be compressed and encrypted, providing additional security and saving storage space.

Cons:

  • Additional Storage Required: Image files may consume more storage space compared to a direct clone since they include both used and unused space on the source drive.
  • Two-Step Process: Creating an image involves two steps—first, creating the image file, and then restoring it to the destination drive. This may take more time compared to a direct clone.

 

Directly Cloning a Drive:

Pros:

  • Simplicity and Speed: Direct cloning is a straightforward process and can be faster since it involves a single step of copying data directly from one drive to another.
  • Preserves File System and Partition Information: Direct cloning maintains the file system and partition structure of the source drive.

Cons:

  • Limited Flexibility: Direct clones are specific to the destination drive. You can’t easily transfer the clone to a different drive or share it with others.
  • No Compression or Encryption: Direct cloning doesn’t inherently provide compression or encryption. If these features are needed, additional steps or tools may be required.

 

Considerations:

  • Purpose of the Clone: If you’re creating a backup for disaster recovery, direct cloning might be more suitable. If you need flexibility in storage, versioning, and distribution, creating an image may be preferable.
  • Storage Space: Consider the available storage space. If you have limited storage, direct cloning may be more efficient. If storage is not a concern, creating an image may offer more features.
  • Tool Selection: The tools you choose for cloning or imaging can impact the process. Some tools provide both cloning and imaging capabilities, allowing you to choose based on your needs.

 

Directly clone from source Drive:

Follow Step 1 below to Identify the name of both Drives you’ll be cloning to and from, and use the names with:

sudo dd if=/dev/sdX of=/dev/sdY bs=4M status=progress

**Do not remove either drive during cloning progress.


 

Clone and Image file:

**Before cloning any drive you must first unmount the drive.

Unmounting a drive before cloning or performing other operations is crucial for several reasons:

  • Data Integrity:
    • Unmounting a drive ensures that no data is being read from or written to the filesystem during the cloning process. This helps maintain data integrity and prevents inconsistencies that could arise from changes happening while the cloning operation is in progress.
  • Filesystem Consistency:
    • The unmount process includes flushing any buffered data to the disk and ensuring that the filesystem is in a consistent state. Cloning a mounted filesystem could lead to incomplete or corrupted data if there are ongoing read or write operations.
  • Avoiding Open Files Issues:
    • Files that are open or in use by processes can cause issues during cloning. Unmounting the drive ensures that all files are closed and not actively being accessed by applications.
  • Preventing Data Corruption:
    • Performing actions like cloning on a mounted filesystem can potentially lead to data corruption because the underlying data is changing while the cloning process is trying to copy it.
  • Exclusive Access:
    • Unmounting gives you exclusive access to the device, preventing any interference from other processes or applications that might be accessing the drive.
  • Safety Measure:
    • Unmounting is a safety measure to avoid accidental modifications to the data during the cloning process. It reduces the risk of unintentional changes that could result from interactions with the mounted filesystem.

 

 

STEP 1: Identify and Unmount Drive(disk) and Partitions:

To find the Drive(disk) and partition names:

sudo lsblk


NAME    MAJ:MIN    RM    SIZE    RO    TYPE     MOUNTPOINTS
sda      8:0       0     465.8G   0    disk
├─sda1   8:2       0     512M     0    part     /boot/efi
└─sda2   8:1       0     232.4G   0    part     /
sdb      8:16      0     465.8G   0    disk
nvme0n1  259:0     0     465.8G   0    disk

Look for the name of the Drive(disk) sd(x). For this tutorial we will Unmmount the 512GB(465.8G) Drive(disk) named: sda, and it’s two partitions sda1, and sda2.
**Notice additional partition names eg. sda1, sda2. They will need to be Unmounted separately.

 

For a better more human reading of the drive list, use the command:

sudo fdisk -l

This will give a Disk model to better identify the dive if you have more than one drive the same size and will also show the Drive(disk) name sd(x)

 

Once you have identified which Drive(disk) you want to Unmount and Clone, type:

sudo unmount /dev/sd(x)1 #Replace x with proper Drive(disk) name.

Do this for each partition number eg. sda1, sda2 etc…

 

Once the partitions have been Unmounted, Unmount the Drive(disk):

sudo unmount /dev/sd(x)

 

STEP 2: Clone the Drive and save the .img file:

*The .img file being made will be the same size as the Drive(disk) you’re cloning, so make sure there’s enough space in the directory you’re saving the .img file to.
**Do not save the .img file to the “New” Drive you want to use the cloned .img file on.

After you have the Drive(disk) name and it has been unmounted, we can now clone the Drive(disk) using the Drive(disk) name sd(x):

sudo dd if=/dev/sdX of=/path/to/image-file.img bs=4M status=progress
  • Replace /dev/sdX with the source drive.
  • Specify the path where you want to save the image file (e.g., /path/to/image-file.img).
  • Adjust the block size (bs) as needed.
  • status=progress provides progress information.

 

STEP 3: Flash the .img file to another Drive:

Once you have the .img file saved, you can flash the file to another drive:

sudo dd if=/path/to/image-file.img of=/dev/sd(Y) bs=4M status=progres
  • Replace /path/to/image-file.img with the path to your image file.
  • Replace /dev/sdY with the destination drive. Again use sudo lsblk to find the Drive(disk) name you want to flash to.
  • Adjust the block size (bs) as needed.
  • status=progress provides progress information.

 

Congrats, your Drive has been cloned.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.