Installing apt-btrfs-snapshotTo enable automatic snapshots when performing tasks with apt, you have to install the package apt-btrfs-snapshot. Also make sure that the package python3-distutil is installed since it depends on that.
sudo apt install apt-btrfs-snapshot python3-distutil
Once those packages are installed, the system will automatically make a snapshot each time apt performs an action that modifies packages. The catch here is that the snapshots will eat up your disk space if you don't manually remove them from time to time. I'll also show you, how to manually make snapshots, delete them and revert to a previous state.
Accessing snapshotsTo access the snapshots, you have to mount the partition with the btrfs filesystem using the option "subvolid=0" somewhere. Personally I use the folder /snapshots for that since it represents our purpose quite well, doesn't it? Another good alternative would be /subvolumes I think 😁️
sudo mkdir /snapshots sudo mount -t btrfs -o subvolid=0 /dev/sdXX /snapshots
Now you can change into that directory to see all subvolumes and snapshots. The letter @ is used by convention as the path separator for btrfs subvolumes, and the @ at the beginning simply stands for the root directory. E.g. if you had a separate subvolume for /home/someuser, you'd call it @home@someuser (by convention - obviously you can call it whatever you want theoretically).
cd /snapshots ls @ @apt-snapshot-2020-07-14_13:36:46 @apt-snapshot-2020-07-15_12:20:57 @apt-snapshot-2020-07-15_13:02:51 @apt-snapshot-2020-07-15_13:46:26 @apt-snapshot-2020-07-15_15:18:55
@ is the active subvolume that's mounted by Ubuntu by default at / and if you don't have a separate home partition, you'll also find a subvolume called @home which is mounted at /home.
Managing snapshotsTo manage subvolumes and snapshots, btrfs offers the
Let's create a snapshot, delete it and revert to a previous system state.
sudo btrfs subvolume snapshot @ @2020-08-01 sudo btrfs subvolume delete @2020-08-01 sudo mv @ @discard && sudo btrfs subvolume snapshot @apt-snapshot-2020-07-14_13:36:46 @
btrfs subvolume snapshotcommand takes as first argument the subvolume you want to take the snapshot from and the second argument specifies the name for the new snapshot.
btrfs subvolume deletecommand deletes a snapshot. Theoretically you can also use
rm -rfto get the same result, but this command is way more efficient.
To revert your currently running system to a previous state, you first have to rename the currently mounted subvolume (i named it @discard in the example). You'll notice that you keep working on that subvolume even though its name changed. After that, you take a snapshot of the snapshot you want to restore, calling it @. Now, once you restart the system, you'll be back in time working on a snapshot of the previous version. Now you can safely delete /snapshots/@discard or how you called the newer state that you wanted to discard.
Things to look out forThe snapshots created by apt-btrfs-snapshot will contain the packages that were downloaded with each update, so I recommend to delete all .deb archives in /var/cache/apt/archives/ in all subvolumes since you usually don't want them laying around, eating precious disk space. Remember that you can freely modify the files inside the subvolumes (Try cd'ing into the subvolume/snapshot "folders").
The other thing to look out for is to regularly delete old snapshots. I already mentioned it before, but I can't stress it enough. I ran several times out of space on my machines in the past simply by forgetting about it.