Skip to content

Instantly share code, notes, and snippets.

@MelonSmasher
Created November 28, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelonSmasher/6dacddbb86acdc1188d9fa8b46a3bf74 to your computer and use it in GitHub Desktop.
Save MelonSmasher/6dacddbb86acdc1188d9fa8b46a3bf74 to your computer and use it in GitHub Desktop.
How to capture an entire disk to a compressed file and then restore that file to a disk.

HDD Cloning in Linux / BSD / Solaris

Prerequisites:

Set disk & disk size:

Set a consistent set of variables across most *nix systems so the capture and restore commands are unified.

Linux:

# Set the target disk
DISK='/dev/sdb';
# Get disk size
fdisk -l | grep $DISK;
# Set the disk size
DISKSIZE='146810536448';

BSD:

# Set the target disk
DISK='/dev/da1';
# Get disk size
diskinfo -v $DISK;
# Set the disk size
DISKSIZE='146810536448';

Solaris:

# Set the target disk
D='c2t2d0';
# slice 2 is the entire disk.
DISK='/dev/dsk/'$D's2';
# Get disk size
iostat -En $D | grep bytes
# Set the disk size
DISKSIZE='146810536448'
Note: replace /dev/da1 or /dev/sdb or /dev/dsk/c2t2d0s2 with the disk identifier on your system.
Note: replace 146810536448 witht the size of the disk in bytes.

Copy a drive to a compressed image file:

pv -p -e -a -b -s $DISKSIZE $DISK | pigz -1 > /mnt/repo/disk.img.gz
Note: replace pigz -1 with a level of compression that works well for your situation. eg: pigz -9 for best compression.

Restore the image back to the drive:

method 1:

This method seems faster than method 2.

pigz -dc /mnt/repo/disk.img.gz | pv -a -p -e -b -s $DISKSIZE > $DISK

method 2:

This method offers more contol over method 1.

pigz -dc /mnt/repo/disk.img.gz | pv -a -p -e -b -s $DISKSIZE | dd of=$DISK bs=4194304
Note: Replace bs=4194304 with a block size in bytes that meets the needs of your situation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment