Compressing free space when working with 'dd'.

When creating a disk image with 'dd' you'll find that there's really no way of telling 'dd' to skip free space. A 250gb partition is going to take up 250gb of space.

To get around this you can fill the partition with a large file of zeroes, delete the file and 'dd' it directly afterwards while piping it to a compression utility. Creating the zeroes will drastically help compression.

In this example I'm backing up my Windows partition:

# Calculate how many blocks of free space there are
BLOCKS=$(df -B512 -l /dev/sda1 | awk 'NR==2 {print $4}')
 
# Fill the partitions free space with a large file of zeroes.
dd if=/dev/zero of=/media/hd/zerofill bs=512 count=$((BLOCKS-2))
# Delete the zero filled file.
rm /media/hd/zerofill
# Save the image of the partition; compressing the output before saving.
sudo dd if=/dev/sda1 bs=512 | bzip - >/media/backup/windows/`date +%F`.img
compressing_free_space_when_working_with_dd.txt · Last modified: 2011/12/17 13:55 by siigna
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki