Post by jamba on Oct 6, 2009 6:37:51 GMT -5
blog.someone.exofire.net/2009/10/creating-a-hard-drive-backup/
Creating a hard drive backup
06 October 2009 | Geek, Linux, Tips | Tags: Backup
The main options to be concerned about are if= (input file) and of= (output file). By default, dd reads from stdin and writes to stdout. Here is an example of a use for dd:
# dd if=/dev/md0 of=/dev/hda conv=noerror,sync bs=4k
This command is used often to create a backup of a drive (/dev/md0) directly to another hard drive (/dev/hda). This works only if the hard drive has enough storage to accommodate the source drive’s filesystem. The advantage of this is that you do not have to mount the hard drive to make a backup and the only reference to hda is in /dev and in the command which is usually in a script in cron.
The option “bs=4k” is used to specify the block size used in the copy. The default for the dd command is 512 bytes: use of this small block size can result in significantly slower copying. However, the tradeoff with larger block sizes is that when an error is encountered, the remainder of the block is filled with zero-bytes. So if you increase your block size when copying a failing device, you’ll lose more data but also spend less time trying to read broken sectors. Tools like dd_rescue and dd_rhelp can provide a more flexible solution in such cases, combining the speed of a large block size for the regions without errors with finer-grained block-copies for regions with errors.
Creating a hard drive backup image
# dd if=/dev/hda | gzip > /mnt/hdb1/system_drive_backup.img.gz
Here dd is making an image of the first harddrive, and piping it through the gzip compression program. The compressed image is then placed in a file on a seperate drive. To reverse the process:
# gzip -dc /mnt/hdb1/system_drive_backup.img.gz | dd of=/dev/hda
Here, gzip is decompressing (the -d switch) the file, sending the results to stdout (the -c switch), which are piped to dd, and then written to /dev/hda.
Copy floppy
# dd if=/dev/fd0 of=/tmp/floppy.img bs=10240
That will copy the contents of the floppy to a file. Then, to put the image onto a new floppy, swap “if” and “of” params.
# dd if=/tmp/floppy.img of=/dev/fd0 bs=10240
Backing up your Master Boot Record (MBR).
You should do this before you edit your partition table so that you can put it back if you mess things up.
# dd if=/dev/hda of=/root/hda.boot.mbr bs=512 count=1
If things mess up, you can boot with Knoppix, mount the partition containing /root (hda1 in this example) and put back the MBR with the command:
# dd if=/mnt/hda1/root/hda.boot.mbr of=/dev/hda bs=512 count=1
Getting around file size limitations using split
When making images, it’s quite easy to run up against various file size limitations. One way to work around a given file size limitation is to use the split command.
# dd if=/dev/hda1 | gzip -c | split -b 2000m – /mnt/hdc1/backup.img.gz.
1. This example is using dd to take an image of the first partition on the first harddrive.
2. The results are passed through to gzip for compression
* The -c option switch is used to output the result to stdout.
3. The compressed image is then piped to the split tool
* The -b 2000m switch tells split how big to make the individual files. You can use k and m to tell switch kilobytes and megabytes (this option uses bytes by default).
* The – option tells split to read from stdin. Otherwise, split would interpret the /mnt/hdc1… as the file to be split.
* The /mnt/hdc1… is the prefix for the created files. Split will create files named backup.img.gz.aa, backup.img.gz.ab, etc.
To restore the multi-file backup, do the following:
# cat /mnt/hdc1/backup.img.gz.* | gzip -dc | dd of=/dev/hda1
1. Cat recombines contents of the compressed and split image files to stdout, in order.
2. Results are piped through gzip for decompression.
3. And are then written to the first partition of the hard drive with dd.
Creating empty disk images
To create an empty disk image, to be used as the disk for an emulator for example, one can get data from /dev/zero. To create a 10mb image:
$ dd if=/dev/zero of=myimage bs=1024 count=10240
A clever alternative is:
$ dd of=myimage bs=1024 count=0 seek=10240
Here we don’t write anything, not even zeroes, we just seek 10mb into the file and close it. The result is a sparse file that is implicitly full of 10mb of zeroes, but that takes no disk space. ls -l will report 10mb, while du and df will report 0. When the file is written to, either as an emulator disk or a loopback device, Linux will allocate disk space for the data. ls will still show 10mb, while du will gradually approach 10mb.
For swap images, where it’s more important to reserve the data than to save disk space, a non-sparse file is better.
Jargon File Entry
This is what the Jargon File has to say about dd:
[Unix: from IBM JCL] Equivalent to cat or BLT. Originally the name of a Unix copy command with special options suitable for block-oriented devices; it was often used in heavy-handed system maintenance, as in “Let’s dd the root partition onto a tape, then use the boot PROM to load it back on to a new disk”. The Unix dd(1)was designed with a weird, distinctly non-Unixy keyword option syntax reminiscent of IBM System/360 JCL (which had an elaborate DD “Dataset Definition” specification for I/O devices); though the command filled a need, the interface design was clearly a prank. The jargon usage is now very rare outside Unix sites and now nearly obsolete even there, as dd(1) has been deprecated for a long time (though it has no exact replacement). The term has been displaced by BLT or simple English “copy”.
Although deprecated, dd is still widely in use on many systems.