Disks can be cloned (byte for byte copy) to other disks or to files (image files) with many tools.

I prefer using the basic tools because they just work very well. Nothing beats the simplicity of some of the basic tools available out of the box with most GNU/Linux distributions.

I recently had to make a full backup of someone’s personal computer and I thought about making my notes publicly available. I have a simple 1 bay USB dock that makes it easy to plug the most common types of drives on my system.

Warning: These tools can be misused and mistakes may cause data loss. Read the manpages if you are unsure about something.

Disable Auto-Mounting

Some desktop environments will offer auto-mounting of partitions as a convenience feature. By default, partitions will be mounted with full read and write capabilities.

If you are using a non-dedicated system for cloning, you may want to disable this feature to prevent issues, especially if the disks are starting to fail. I use the MATE desktop environment so here is how:

gsettings set org.mate.media-handling automount false

Cloning Disks

In this example, we will use the drive /dev/sdc as the disk that needs to be cloned to an image file (/path/to/my/imagefile.img). I prefer using image files because I find it is easier to manage them after for archival.

All commands below require administrative permissions, either via sudo or by running as root.

With DD

DD is a utility that is part of the GNU core utilities (coreutils package in Debian). There is a very strong chance that it will be available on whatever system.

To clone a disk to a file with dd:

dd if=/dev/sdc of=/path/to/my/imagefile.img

More details about the options used:

  1. if=/dev/sdc: read from /dev/sdc instead of stdin
  2. of=/path/to/my/imagefile.img: write to /path/to/my/imagefile.img instead of stdout

There are a few other options that may be useful, especially for larger disks:

dd if=/dev/sdc of=/path/to/my/imagefile.img bs=32M conv=sync,noerror status=progress

More details about the options used:

  1. if=/dev/sdc: read from /dev/sdc instead of stdin (same as above)
  2. of=/path/to/my/imagefile.img: write to /path/to/my/imagefile.img instead of stdout (same as above)
  3. bs=32M: read and write up to BYTES bytes at a time (default: 512); overrides ibs and obs
  4. conv=sync,noerror: convert the file as per the comma separated symbol list
    • sync: pad every input block with NULs to ibs-size; when used with block or unblock, pad with spaces rather than NULs
    • noerror: continue after read errors
  5. status=progress: The LEVEL of information to print to stderr;

If there are errors during the transfer caused by a failing disk, it is preferable to use DDRescue instead.

With GNU DDRescue

DDRescue is a utility that is part of the GNU data recovery tool (gddrescue package in Debian). This tool is more useful in the case of disks that are starting to fail since it has a few options that can help with those situations.

ddrescue -d -r3 /dev/sdc /path/to/my/imagefile.img /path/to/my/imagefile-gddrescue.log

More details about the options used:

  1. -d: use direct disc access for input file (short for --idirect)
  2. -r3: exit after retry passes (-1=infinity) [0]) (short for --retry-passes=<n>)

However, this can be destructive (and finish off the damaged drive) and ddrescue will rarely restore anything new after three complete passes. If the drive is seriously damaged, we may want to skip the scraping phase:

ddrescue -d -n /dev/sdc /path/to/my/imagefile.img /path/to/my/imagefile-gddrescue.log

More details about the options used:

  1. -d: use direct disc access for input file (short for --idirect) (same as above)
  2. -n: skip the scraping phase (short for --no-scrape)

Mounting Disk Images

To mount disk image(s) and any of the partitions within it, we can use the kpartx tool (kpartx package part of the multipath-tools source package in Debian). Mounting partitions without this tool is possible but you need to get the proper offsets when mounting. kpartx will map image partitions using /dev/mapper which we can then mount directly instead (let’s say we want to mount the second partition from the drive we cloned):

kpartx -a /path/to/my/imagefile.img
mount -o loop /dev/mapper/loop0p2 /mnt

When we are done, we can unmount everything:

umount /mnt
kpartx -d /path/to/my/imagefile.img

References