Backing up your Linux system to an image without including partition's free space?

How I survived working in Windows is the built in backup image feature Windows 7, and 8.1 have. I could generate a current snapshot of the whole C: drive and then restore to that snapshot if with things went awry, like registry corruption or sticky malware and viruses. Windows generates a disk image in such a way that free space is excluded, so I was able to keep multiple snapshots on an external hard drive.

I'm looking for a similar way to back up Ubuntu. I found that the create image tool in the disk manager does what I want and so does the dd command, but I want a snapshot of the currently used space on the whole drive, not a clone including all the 230 GB free space accounted for on the .img file as well, and an easy way to restore it from the Ubuntu USB live media.

Anyone know of such a way to achieve this?

I recently used Clonezilla (installed onto and run from a USB drive) to backup my boot/os ssd. The drive is 128GB but the backup is only 18Gb so this would seem to match what you want. This would appear to match what you want to achieve.

@hardboiledphil: Okay thanks. I've been investigating what DD and the tools in Ubuntu do exactly and I think they don't include free space but account the full partition size in file properties anyway? Still need to look at it some more.

Very confusing. Playing around with the disk tool in a Ubuntu VM, I created a 3.5 GB drive and a 16 GB drive that the 3.5 GB drive will be saved as an image to. Ubuntu cloned the entire 3.5 GB partition but since it's a VHD, my Windows 8.1 host system reports the VHD file on the drive as 339,308 KB in size after the write operation, because dynamically expanding VHD. At first I thought it is excluding free space, but in Ubuntu it says it's still 3.5 GB size on disk. I mounted that VHD which has the image in it to my Windows host and Windows too reports it as being 3.5 GB on disk.

Sounds like VHD's are just adding to the confusion as they're supposed to be dynamically sized anyway. It's presumably proper partitions you want to back up? I just tried creating disk image from disk tool and it's a full copy as you suggest - basically dd on one partition. Difference with Clonezilla is it backs up all partitions on the drive so you can easily get back to a working system if one or more partitions are lost from a drive.

If you're running Ubuntu in a VM, you can snapshot the VM. If that's set to dynamic overlay allocation, only the actual used storage will be snapshot.

If you're running on bare metal, what you usually do in Linux, is save your /home folder, because it's the one that holds your data and your specific user applications with their configs and stuff (including steam and whatever). The easiest way to backup is with rsync. You can restore any /home folder to any Linux install, even others than the one you were using it before, and it will just work. Everything in Linux is portable by nature.

You can however snapshot your install as such also. Advanced enterprise-like distros like OpenSuSE have this functionality built-in, to a point where you can even boot off of a previous snapshot in case something really bad happens (like experiments with proprietary graphics drivers, those often tend to be flaky). The utility in opensuse is called snapper, and there might be a way to also install it in a DEB-distro, there usually is, but I just don't know because I tend to not use those in systems where snapshotting is a requirement for me. I run all servers in a kvm or xen container, and use the snapshotting features of the hypervisors for those.

1 Like

+1 Snapper has saved my bacon a few times. It is like an undo button for Linux lol.

Rsync, Deja Dupe, Clonezilla, Backula, and Crashplan all rock. If you have a server Backula and Crash plan really shine. Also self hosted cloud storage like Owncloud and Pydio can be used for backup.

If you want to go completely custom a DD cron job will work too.

And don't forget Timeshift the back in time for Linux with cloning bonus.