Snapshotting with btrfs
We’ve seen that btrfs can be used without conventional partitioning at the device level, but it can also provide it’s own data-level divisions in the form of subvolumes. On the one hand, these behave like directories, but since they can also impose quotas and can be mounted outside of the main volume, the partition metaphor works too. The main filesystem is a subvolume too, called the top-level subvolume.
Among the many exciting features of btrfs are Snapshots. As the name suggests, these are a copy of the filesystem at a given in time. As the name doesn’t suggest, they are just a special kind of subvolume, one that’s a copy of some part (possibly all) of the parent filesystem. Thanks to the miracle of Copy on Write (CoW), the first snapshot you take occupies hardly any space. However, as the original data is changed, these changes are factored into the snapshot and it grows accordingly.
Snapshots can be automated and so an extreme case might see you generating them daily. This would mean that system-wide changes can be reverted to, for example, last Tuesday. This is probably overkill, but taking a snapshot before some wide-sweeping change can never hurt. To snapshot the entire filesystem (you can restrict to specific subvolumes too) run the following:
$ sudo btrfs subvolume snapshot / /snap.$(date +%Y-%m-%d)
This can be explored as a directory, or, if something goes wrong, reinstated with something like:
$ sudo btrfs subvolume snapshot snap.20170214 /
For more information on btrfs, check the official wiki at https://btrfs.wiki.kernel.org, but we warned – there’s a lot of it.