Absurd: Manual Backups with dd


There are several different ways to make backups of data for any operating system. In the "glory days" of UNIX people would usually write a cron job that would create a Tape ARchive of their system and write that TAR to a tape drive. Well, those days are long gone, as are those scripts. What is here? DD. DD is very powerful and rather under appreciated tool. Do note that dd does NOT have a progress indicator. As such, I recommend that you apt-get install pv.

So, a simple backup?

dd if=/dev/sdX | pv | dd of=/dev/sdY

The above assumes that you have access to another hard disc of the exact same make, and that you wish to mirror your drive in use onto the other drive (pv is just the progress indicator). Not your cup of tea? Ok. Now, this is the command I use for backup. It's faster than bit by bit, though not quite as safe. It also will not quit when it encounters a disc error. I compresses your output, and instead of outputting to a disc, it will output to a file. The restore on this one is pretty much the same.

dd if=/dev/sdX bs=10M conv=noerror,sync | pv | gzip -c -9 > sdX.img.gz
gunzip -c sdX.img.gz | pv | dd of=/dev/sdX conv=sync,noerror bs=10M

Alright, so now you want to store your backup on your server? No problem, dd can handle networks too... with the help of SSH.

dd if=/dev/sdX bs=64k conv=noerror,sync | pv | gzip -c -9 | ssh [email protected] dd of=sdX.img.gz

Other gems of the Disk Destroyer:

Floppy copy:

dd if=/dev/fd0 of=floppy.img bs=2x80x18b conv=notrunc

CD ISO copy:

dd if=/dev/sr0 of=mycd.iso bs=2048 conv=notrunc

MBR Copy:

dd if=/dev/sda of=mbr.img bs=512 count=1

MBR Wipe:

dd if=/dev/zero of=/dev/sda bs=512 count=1

Disk Wipe:

dd if=/dev/zero of=/dev/sda bs=10M

(could follow with if from random/urandom and then another zero, but you may not be paranoid)

Getting rid of a Macintosh GPT is little tricky but dd can do it:

fdisk -s /dev/devicename

This gets the blocksize of the device. Make the last five digits of this number zeros. Example: "fdisk -s /dev/sda" will show an error about GPT, and then the block size: 39078144. Change to 39000000 which equals our blockcount

dd if=/dev/zero of=/dev/devicename bs=1k seek=blockcount
dd if=/dev/zero of=/dev/devicename bs=1k count=20

Swap file:

dd if=/dev/zero of=/swapfile.img bs=1M count=2048 && \
chown root:root /swapfile.img && chmod 600 /swapfile.img && \
mkfs.swap /swapfile.img && swapon /swapfile.img && \
echo "/swapfile.img  none  swap  sw  0  0" >> /etc/fstab

So, as you can see, dd is a very powerful and very useful command. You can use it for far more than what I have shown, but these are the most common uses I could think up. Also, note that the device names /dev/sdx could be replaced with any file. The only real difference in that case is that you need to know the file size (ls -l). The file size makes up your bs value. You would do a count value of 1, and you would also need to pass a conv value of notrunc. This would allow you to securely delete any file by first writing either zeros or random numbers to it.

With dd, you should ALWAYS exercise extreme caution before hitting enter. The wrong command could forever wipe your data, or at least waste quite a bit of your time.

⇠ back

© MMIX - MMXXII, absurd.wtf

Licentiam Absurdum