Creating a Disk Image
Drives are often given a name such as /dev/sda, /dev/sdb, or /dev/sdc. You can find out the names using a partition editor. Or, since you're already in the terminal, you can use the lsblk command.
Creating a Disk Image
One of way to clone a drive is to create a disk image that you can move around and restore as you would do with a bootable USB.
Creating image files allows you to save multiple backups to a single destination, such as a large portable hard drive. Again, this process only requires one command:
dd if=/dev/sdX of=path/to/your-backup.img
To save space, you can have dd compress your backup.
dd if=/dev/sdX | gzip -c > path/to/your-backup.img.gz
This command shrinks your backup into an IMG.GZ file, one of the many compression formats Linux can handle.
Restoring a Drive With dd
if the system crashes, you can easily recover your system by entering one command:
dd if=/dev/sdY of=/dev/sdX
When restoring from an image file, the same concept applies:
dd if=path/to/your-backup.img of=/dev/sdX
If your image file is compressed, then things get a little different. Use this command instead:
gunzip -c /path/to/your-backup.img.gz | dd of=/dev/sdX
To be clear, gunzip is "g unzip," as in the opposite of "g zip." This command decompresses your backup. Then dd replaces the existing drive with this image.
Parameters to Consider
You can extend the command by inserting an additional parameter. Since copying can be time consuming, you can speed up the process by increasing the block size. Do so by adding bs= at the end.
dd if=/dev/sdX of=/dev/sdY bs=64
This example increases the default block size from 512 bytes to 64 kilobytes.
conv=noerror tells dd to continue despite any errors that occur. The default behavior is to stop, resulting in an incomplete file. Keep in mind that ignoring errors isn't always safe. The resulting file may be corrupted.
conv=sync adds input blocks with zeroes whenever there are any read errors. This way data offsets remain in sync.
You can combine these last two as conv=noerror,sync if you so desire. There is no space after the comma.