Rollback To A Working State With btrfs + apt-btrfs-snapshot On Ubuntu 12.10

On this page

This tutorial explains how you can revert failed apt operations (like apt-get upgrade) and roll back to the previous system state with apt-btrfs-snapshot on an Ubuntu 12.10 system that uses the btrfs file system. apt-btrfs-snapshot creates a snapshot of the system before the apt operation. Being able to easily restore the previous system state after a failed apt operation takes away much of the pain system administrators have to deal with normally and is one of the greatest features of the btrfs file system.

I do not issue any guarantee that this will work for you!

1 Preliminary Note

In this tutorial I have installed the whole system on a btrfs file system, i.e., there's no separate /boot partition on an ext file system. If you use a separate /boot partition and apt installs anything in that partition (like a new kernel), you cannot undo changes to the /boot partition with apt-btrfs-snapshot- only changes on the btrfs partition can be reverted.

My hard drive is named /dev/sda in this tutorial, my system partition is /dev/sda1.

A note for Ubuntu users:

Because we must run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing

sudo su

2 Install apt-btrfs-snapshot

apt-btrfs-snapshot can be installed as follows:

apt-get install apt-btrfs-snapshot

To check if apt-btrfs-snapshot is able to create snapshots on apt operations, run

4 Rollback

Now let's assume the last apt operation turned our working system into one that isn't working as expected anymore. That's why we want to restore the previous system state, i.e., we want to do a rollback.

Therefore we mount the btrfs filesystem to a separate location, e.g. /mnt:

@apt-snapshot-2012-11-22_11:50:38 is a snapshot of our working root filesystem (@) before the apt operation. In order to make the system boot from that working snapshot instead of from the current subvolume, we rename @ to something else and then @apt-snapshot-2012-11-22_11:50:38 to @:

7 Links

Falko Timme is an experienced Linux administrator and founder of Timme Hosting, a leading nginx business hosting company in Germany. He is one of the most active authors on HowtoForge since 2005 and one of the core developers of ISPConfig since 2000. He has also contributed to the O'Reilly book "Linux System Administration".

On newer versions of ubuntu, you nolonger need to mount the filesystem to roll back to an apt-btrfs-snapshot. Just run:

~$ sudo apt-btrfs-snapshot list

to list the available snapshots and then run the following to revert to an older snapshot:

~$ sudo apt-btrfs-snapshot set-default @<name-of-snapshot>

Reboot for the changes to take effect.

Also, deleting snapshots is now much more reliable and simple. Again, to list snapshots, run:

~$ sudo apt-btrfs-snapshot list

Then, to delete a snapshot, run:

~$ sudo apt-btrfs-snapshot delete @<name-of-snapshot>

To list by date older than two days, for example, run:

~$ sudo apt-btrfs-snapshot list-older-than 2d

To delete all snapshots older than two days, for example, run:

~$ sudo apt-btrfs-snapshot delete-older-than 2d

The cool thing is that your old default will not be deleted so you can still roll back to that one if you have one set.

Deleting snapshots should take effect immediately.

Finally, if you ever run into the "no space left on device" problem when you boot up, insert a blank pin drive or external hard drive and mount the blank drive at /tmp using the command line. After doing so, you should be able to proceed with deleting older snapshots.

mchid is wrong. I am using Kubuntu 16.04 LTS and when I trie what he said I get "Sorry, your system lacks support for the snapshot feature" plus I had to sudo apt-install apt-btrfs-snapshot, to install the package.