Using the DD Linux Command To Backup & Restore

Background

I have been messing around with backing up a compact flash card and trying to put images onto an sd card for my Raspberry PI and thought I would jot down effective use of the Linux DD utility. The ‘dd’ command is one of the original Unix utilities and should be avaliable in all Linux distros. It can strip headers, extract parts of binary files and write into the middle of floppy disks; it is used by the Linux kernel Makefiles to make boot images. It can be used to copy and convert magnetic tape formats, convert between ASCII and EBCDIC, swap bytes, and force to upper and lowercase.

Backing Up

# The next dd statement will backup from hard drive /dev/hda to
# hard drive /dev/hdb
dd if=/dev/hda of=/dev/hdb

# The next dd statement will dump hard drive /dev/hda into an
# image file of /path/imagefile
dd if=/dev/hda of=/path/imagefile

# The next dd statement will dump hard drive /dev/hda into a
# compressed image file /file/imagefile.gz
dd if=/dev/hda | gzip > /path/imagefile.gz

Restoring

# The next dd statement will take image file /path/imagefile
# and dump the contents onto hard drive /dev/hda
dd if=/path/imagefile of=/dev/hda

# The next dd statement will take image a compressed 
# /path/imagefile.gz and uncompress and dump onto 
# hard drive /dev/hda
gzip -dc /path/imagefile.gz | dd of=/dev/hda

Please Note: line 8 uses gzip and the parameter -dc, ‘d’ means decompress, ‘c’ means send the gzip content through to stdout, this is then piped through stdout through ‘dd’ straight out to the /dev/hda.

Block Size

The parameter ‘bs’ can be used to specify the number of bytes that should be read and then written to at a time, during a copy process. e.g.

dd if=/dev/hda of=/dev/hdb bs=512
# bs=512 means copy 512bytes at a time.

dd if=/dev/hda of=/dev/hdb bs=1M
# bs=1M copy 1 Megabytes of data at a time

So I wanted to backup data off a compact flash card and put it into a compressed file. When I plugged my USB reader with a compact flash card already inserted into my Linux machine the reading it informed me that the device I had just plugged in was /dev/sdb.

Using this information I entered the following which backed my compact flash card up to file /path/mycompactflashcard.img.gz.

dd if=/dev/sdb | gzip > /path/mycompactflashcard.img.gz

For my Raspberry PI I wanted to dump an image called debian6-19-04-2012.img onto the sdd card I had on /dev/sdb copying in blocks of 1 Megabyte e.g.

dd if=/file/debian6-19-04-2012.img of=/dev/sdb bs=1M

Conclusion

After you have performed any read write operation on a device and before unplugging the media, it is wise to issue the following will make sure that the file caches are written.

sync

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.