Friday, May 18, 2007

Save a partition directly to a compressed file

To save a partion directly to a compressed file in linux is very easy regardless of the filesytem, you can save for example a NTFS (Windows NT File System) whith this.

To save the partition and later restore it, we are going to use three programs the first is 'dd' the which is used to make exact copies of partitions and restore them and the others are 'bzip2' which is used to compress and 'bunzip' to uncompress.

In order to save a partition to a compress a partition to a compress file, just do:


# dd if=/dev/hda2 bs=10M | bzip2 > /.../hda2_save.bz2


Where '/.../' must be replaced with the path to the dir where you want your partition compress save to be.

Has you can see above 'dd' has no 'of' (output file) so it's output will be the standart output (the shell), wich we redirect with '|' (pipe) to 'bzip2' standart input (which if not redirected is the keyboard), buzip2 will continuously compress the stream of data coming from 'dd', and that's it.




In order to save a partition to a compress a partition to a compress file, just do


# bunzip2 /.../hda2_save.bz2 --stdout | dd of=/dev/hda2


Where '/.../' must be replaced with the path to the dir where you want your partition compress save is.

Has you can see above 'bunzip2' has the option '--stdout' wich means that it will uncompress the file to the standart output (the shell) wich we redirect with '|' (pipe) to 'dd' standart input (which if not redirected is the keyboard).