Create ext4 image skipping free space

To backup an ext4 partition the easy way is just to use cat or dd:

cat /dev/sdXn > backup.img
dd if=/dev/sdXn of=backup.img

This of course is just a low level copy, so also the unused disk space will be backed up to the image (and unless the unused space is zeroed before creating the image, it will waste space also if the image is compressed).

Is there an easy way to do a more smart ext4 backup (at file system level, so not just a tar of the content of the folders and files), skipping the unused space, without resorting to tools like clonezilla?


Solution 1:

You can use e2image -r which will create a sparse file of only the metadata and data blocks which are in use. This can then be compressed, but when you uncompress the compressed raw image, you may then get much larger file because it is not sparse. To get around this problem, you can use e2image -Q which will create a qcow2 file.