Update 10/25/2010: Corrected the zero file input file command.

UPDATE 11/16/2009: You may want to read this summary article on a variety of backup/restore and reimaging options available for different operating systems.

UPDATE 12/20/2010: Read how to use DD on Mac OS

Ok, this is a geeky post for sure….
Some time ago, I encountered a tool that’s available on GNU/Linux known as “dd“. After taking one look at it, I ran in the other direction. It scared me in that it seemed it was too easy to wipe out stuff. However, now that I’ve had a chance to play around with different tools, I feel more comfortable attempting it.
As I shared in this blog entry, I’m looking for an easy way to backup and restore a netbook’s hard drive. dd allows me to do this pretty quickly. A little about dd :

dd is a Linux command that can be used for the low-level copying and conversion of data & files. It is commonly used to copy regions of raw device files, e.g. backing up the boot sector of a hard disk, or to read fixed amounts of data from special files…dd works byte for byte. Data from deleted files that may still be present on a disk are not visible through the file system (Source)

With Linux dd command, you can save backups onto a cd or dvd [or external USB hard drive]. More important is that, we can backup the whole hard disk device or the whole system using dd command. Yes, Linux dd command can do that.
Read Source

dd options:

-if Specifies the input device or partition (or) file from which data is to be dumped (In File)
-of Specifies the output device or partition (or) file to which data is to be dumped (Out File)
-ibs Specifies how many bytes is to be readed from a input file at a time during the dumping process
-obs Specifies how many bytes is to be written to the output file at a time during the dumping process
-bs Specifies how many bytes is to be readed and written at a time during the dumping process
-count=[bytes] Specifies how many bytes is to be dumped from ‘if’ to ‘of’

Trying another way to make an easy backup of a netbook hard drive. . .using dd on linux.
My first step was to format a 500gig external USB drive to “ext3” format:

mkfs.ext3 /dev/sdb1

and then, boot up off my handy UbuntuLinux USB Flash drive, drop to Terminal, and then type in the commands to make a backup/restore (make sure all devices being backed up are unmounted)

sudo dd if=/dev/sda of=/media/backup/sda.backup

sudo dd if=/media/backup/sda.backup of=/dev/sda

Will it work? I’ll let you know. I do hope that this will be the easiest way to get it done.
UPDATE: Yes, the commands above worked like a charm…although it took hours. I definitely need to investigate the tip at the bottom of this post!

Here’s another approach to backup and restore that compresses the file:

BACKUP with Compression:

dd if=/dev/sda1 | gzip -c > backup_partition1_image.dd.gz

RESTORE from Compressed File:

cat backup_partition1_image.dd.gz | gzip -d | dd of=/dev/sda1 

dd if=/dev/cdrom of=/tmp/image.iso bs=2k

One of the nifty commands that’s come in useful, especially with netbooks, is backing up the Master Boot Record. This is especially helpful when dealing with netbooks that have had GNU/Linux and Windows set up as dual boot, but then, you realize you want to go back to the way things were (Windows only or whatever). Here’s the command I use:

dd if=/dev/sda of=/mnt/udrive/MBRboot.image bs=512 count=2

Other commands:

dd if=/dev/sda of=/dev/sdb bs=4096 conv=notrunc,noerror
dd if=/dev/sdb2 of=/home/sam/partition.image bs=4096 conv=notrunc,noerror
Restore a Partition:
dd if=/home/sam/partition.image of=/dev/sdb2 bs=4096 conv=notrunc,noerror


Backup Partition to a Gzipped File
dd if=/dev/sdb2 ibs=4096 | gzip > partition.image.gz conv=noerror

Restore Gzipped File to Partition
dd if=partition.image.gz | gunzip | dd of=/dev/sdb2

Or, if you need to zero out space on your image (Thanks to Tony A Russo for the tips), try this…note that you can increase the “bs=###” to where the ### is greater than the total capacity of the drive to ensure you wipe out all “free space.”

Step 1 – Zero out content to make the image smaller:

dd count=1 bs=1024K if=/dev/zero of=/media/udrive/zero.file

or try this command: 

dd bs=1M if=/dev/zero of=/media/udrive/zero.file 

Step 2 – Delete the Zero File:

rm zero.file

Step 3 – Unmount the NTFS drive that’s mounted

umount /media/udrive

Step 4 – Unmount the NTFS drive that’s mounted

dd if=/dev/sda of=/media/udrive/hdbackup.img

Other DD Tutorials and stuff:
  • Learn the DD Command
  • DD Usage
  • Other possible commands
    bzip contents:
    dd if=/dev/sda | bzip2 > /output/file.img.bz2

    or for encrypted backup:
    dd if=/dev/sda | bzip2 | gpg –encrypt –recipient=myself –output=/media/backup/image.imj.bzip.gpg

From SystemRescue CD info:

If you really need a complete NTFS Write support, you will have to use Ntfs-3g. It’s very easy to use:
ntfs-3g /dev/sda1 /mnt/windows

Great suggestions via a G+ post:

A simple way to find the status(size copied) of dd command.Assuming you are running a single instance of dd, for eg, say on terminal 1 you are running this:
$  dd if=/dev/zero of=output.img bs=8k count=128k  
The above command will take around 15 secs to create a 1GB+ image, to know the status of dd open a new terminal(say, terminal 2) and type:
$  kill -USR1 `pgrep ^dd$`                                       
This won’t actually kill any job. You won’t see any output for the above command. This will spit the status of dd on the terminal where dd is actually running(terminal 1). Its very handy while dd’ing large data’s.

Subscribe to Around the Corner-MGuhlin.org

Everything posted on Miguel Guhlin’s blogs/wikis are his personal opinion and do not necessarily represent the views of his employer(s) or its clients. Read Full Disclosure