Monday, January 3, 2011

How to use dd command ?

How to use dd command ?

DD - Disk Dump

1.  Create an empty file of 650MB size.

#dd if=/dev/zero of="empty_file" bs=1024k count=650

2. To Create ISO from CD OR DVD

#dd if=/dev/cdrom of=file.iso

3. Create an exact image of this floppy-disk by issuing the command

#dd if=/dev/fd0 of=floppy.img

4. Create backup of HDD or partition

#dd if=/dev/sdb of=backup.dd

You can check the backup image consistency to check for any problem with backup image

#fsck -y backup.dd

5. Creating backup of one hardisk in to second HDD.

#dd if=/dev/sda of=/dev/sdb conv=noerror,sync bs=4k

Note :  dd with conv=noerror writes nothing in the image in case of a bad block.

    You can check the backup image consistency to check for any problem with backup image

#fsck -y /dev/sdb

6. How to take backup of only MBR and MBR + Partition Table;

#dd if=/dev/sda of=mbr.bak bs=1 count=446 (Backup only MBR)

#dd if=/dev/sda of=mbr_part.bak bs=1 count=512 (Backup MBR + Partition Table)

   To restore,

#dd if=mbr.bak of=/dev/sda bs=1 count=446 (Restore only MBR)

#dd if=mbr_part.bak of=/dev/sda bs=1 count=512 (Backup only MBR)

7. To wipe the hard disk completely,

#dd if=/dev/zero of=/dev/sda

8. How to compress DD image ?

#dd if=/dev/hda conv=sync,noerror bs=64K | gzip -c  > /mnt/sda1/hda.img.gz

        Note that /mnt/sda1 is my backup device.

"dd" is the command to make a bit-by-bit copy of "if=/dev/hda" as the "Input File" to "of=/mnt/sda1/hda.img.gz" as the "Output File". Everything from the partition will go into an "Output File" named "hda.img.gz". "conv=sync,noerror" tells dd that if it can't read a block due to a read error, then it should at least write something to its output of the correct length. Even if your hard disk exhibits no errors, remember that dd will read every single block, including any blocks which the OS avoids using because it has marked them as bad. "bs=64K" is the block size of 64x1024 Bytes. Using this large of block size speeds up the copying process. The output of dd is then piped through gzip to compress it.

To restore your system:

#gunzip -c /mnt/sda1/hda.img.gz | dd of=/dev/hda conv=sync,noerror bs=64K

9. 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/hda > /mnt/sda1/


One of the disadvantages of the dd method over software specifically designed for the job such as Ghost or partimage is that dd will store the entire partition, including blocks not currently used to store files, whereas the likes of Ghost understand the filesystem and don't store these unallocated blocks. The overhead isn't too bad as long as you compress the image and the unallocated blocks have low entropy. In general this will not be the case because the emtpy blocks contain random junk from bygone files. To rectify this, it's best to blank all unused blocks before making the image. After doing that, the unallocated blocks will contain mostly zeros and will therefore compress down to almost nothing.

Mount the partition, then create a file of zeros which fills the entire disk, then delete it again.

# dd if=/dev/zero of=/tmp/ bs=8M; rm

And then,

#dd if=/dev/hda conv=sync,noerror bs=64K | gzip -c  > /mnt/sda1/backup.img.gz

10. You can also use the cat command and pipe it through gzip with the "-9" option (for maximum compression) and then redirect the output to your image file:

#cat /dev/sda | gzip -9 > hdbback.img.gz

11. Error Handling : Murphy's Law was postulated long before digital computers, but it seems it was specifically targeted for them. When you need to read a floppy or tape, it is the only copy in the universe and you have a deadline past due, that is when you will have a bad spot on the magnetic media, and your data will be unreadable. To the rescue comes dd, which can read all the good data around the bad spot and continue after the error is encountered. Sometimes this is all that is needed to recover the important data.

#dd bs=265b conv=noerror if=/dev/st0 of=/tmp/bad.tape.image