Linux How to Image a SD Card or Hard Drive
Using the Linux dd command to create and restore device images
Find the source device location with either the 'mount' or 'fdisk -l' command. Note the name of the source. Here we'll assume it is /dev/sdf. Make sure you have the proper device path when restoring an image because it may not be the same from when the image was created.
Make sure the source is not mounted during the backup process. Unmount it if it is with 'umount /dev/name'.
To create an image:
# dd if=/dev/sda conv=sync,noerror status=progress bs=4M | gzip -c > filename.img.gz
To restore the image:
# gunzip -c filename.img.gz | dd of=/dev/sda conv=sync,noerror bs=4M
Store extra information about the drive geometry necessary in order to interpret the partition table stored within the image. The most important of which is the cylinder size.
# fdisk -l /dev/sda > filename.info
If we zero out unused disk space before creating the disk image, we'll avoid backing up deleted files and fragments. This will allow for a smaller image file during the creation process.
# dd if=/dev/zero of=/tmp/zeros.tmp bs=8M; rm zeros.tmp