Fedora System Restore Root Snapshots Using Btrfs-Assistant
May 29 2023 10:23pm • Est. Read Time: 7 MINDid you know you can take Fedora system snapshots to recover from common errors? Restoring from a system snapshot does not fix the problem. Instead, it allows you to get back to work and sort out what is wrong later on when time allows.
For example:
You run a series of updates. They were just released. You installed them, rebooted, and now your Wi-Fi is no longer working. Restoring a previous snapshot, you can get back to work and sort out what may have happened later. If you took a little extra time to set up Btrfs-Assistant ahead of time, then you're all set - simply rolling back to a previously working state is super easy.
Note: This article assumes you are using a default installation of Fedora Workstation, which uses Btrfs as the default file system. This article is also best suited for a fresh Fedora Workstation install.
Tip: Snapshots vs user data backups?
- Snapshots are best for a restore point when a system configuration goes wrong. Updates break something, oddball issues that happened after you changed something. This gets you back to a working state to sort out the problem later.
- Backups are best for your home directory contents. Application data such as browser or email data you want kept safe. Pictures, documents, etc. It's recommended to use an application called Backups for home directory backups.
Advanced Fedora users - please note
If you'd rather learn how to take Btrfs snapshots from a CLI prompt and avoid using a GUI altogether, this guide from Fedora Magazine is among the best out there. This Fedora Magazine article will give you a starting point if you have an interest in learning how Btrfs snapshots work at a deeper level.
By contrast, this Btrfs-Assistant guide below presented below is designed for those looking for a simple-to-use GUI for Snapper to restore your snapshots on Fedora.
To be clear - this Framework guide is not designed to be a in-place alternative for extensive guides on using Btrfs/Snapper snapshots or volume management - it's designed to help newer Fedora users get their Framework computers back up and running quickly as easily as possible if the need to roll back to a previous state presents itself.
Install and setup Btrfs-Assistant
Begin by installing Btrfs-Assistant onto your Fedora installation. From activities, search for and open a terminal. Once the terminal is open, we'll run the following command:
sudo dnf install btrfs-assistant -y
This will install Btrfs-Assistant. This is a graphical front-end to managing the Btrfs file system.
For the sake of this article, we'll be focusing on its core feature set - creating and restoring Snapper snapshots. In short, if there is a feature you don't understand, skip it and stick to what is included in this article only.
Configure Btrfs-Assistant
Once Btrfs-Assistant is installed and has been launched from Activities, you will see something like this below:
Now we immediately want to click on the Snapper Settings, click New, and create a new config called root. You should also select a backup path - choose / (which is the root) as the path. Then click the Save button, as shown in the second image below.
This is going to be our snapshots profile for our root directory. It will not affect your /home directory. Your home directory user data will remain untouched. With the profile created, you will notice that your snapshot retention has already been set up for you. It's recommended to stick to these defaults unless you understand what this means. Those coming from TimeShift will likely feel right at home.
Create your first root snapshot.
For the sake of this article, we'll assume this is taking place on a fresh installation of Fedora Workstation. If it's not, that's fine, but you will see entries most likely appearing in your Snapper section. Okay, go ahead and click on the Snapper tab. Under the New/Delete tab, you will see Select Config. It is already set to root. You only need to create your first snapshot by clicking the New button. Give the snapshot a name, and click OK.
Pro tip: Do not wait until there is a problem to create the first snapshot - do so with a working installation.
From this point on, if you install something, delete something in the / directory, or have a bad update that introduces new issues, you can restore to a previous point you created. It's recommended to create new restore points anytime before running software updates.
Restore your first root snapshot
To restore to a previous snapshot, open Btrfs-Assistant, browse to the Snapper tab, then Browse/Restore. Click on the restore point you wish to restore from, and click on the Restore button. You do not need to name the saved backup, but you can if you like. Next, click on the Yes button.
The next screen will look like the one below, telling you to reboot immediately. Click Ok. Then immediately reboot.
Dealing with snapshot restoration and different kernels
Let's say Fedora notifies you that there are updates available. So you create a new manual snapshot. Then you run your Fedora updates. You may notice there is a kernel update being installed. Making a mental note of this, you proceed with the updates. Upon rebooting your Framework computer, you're then booted into the new kernel - oh no, something is not quite right. It could be a regression; something is working differently. Regardless, you decided to restore to a previous snapshot.
You reboot only to find it's trying to boot you into a kernel that no longer exists. Yes, this can happen. Not to worry. Hold the power button down until it's completely powers off. Then power back on the Framework computer, and you should be presented with a grub list of kernels from which to choose. Choose the grub entry below the top one. The second entry. This will be your previous kernel.
To make this older kernel the default, there are a multitude of ways of doing this. Rather than making this confusing, we will run a few commands to ensure your default kernel selected in the grub menu (regardless if it appears) will be the kernel before the one you ended up with in the update.
If you open a terminal window from Activities, type in the following:
cat /etc/default/grub
You'll be presented with your grub details. Note the third line GRUB_DEFAULT=saved - this will be something we will be working with.
We're going to run the following set of commands now. It's recommended that you use this exact command set below, as it has been tested repeatedly to work. Most importantly, it backs up your original grub file.
Kernel rollback #1 - second kernel in grub
sudo cp /etc/default/grub /etc/default/grub.bak && sudo sed -i '/GRUB_DEFAULT=saved/d' /etc/default/grub && sudo sed -i '/GRUB_DEFAULT="Fedora Linux/d' /etc/default/grub && sudo grubby --info=ALL | grep -E "Fedora Linux (.*)-(.*)" | sed 's/title=//' | tail -n 2 | head -n 1 | xargs -I {} sudo sed -i '2s#.*#GRUB_DEFAULT="{}"#' /etc/default/grub && sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
Copy and paste this directly into the terminal. Press the enter key on your keyboard and then your user password when prompted.
Now let's make sure everything works correctly before rebooting.
cat /etc/default/grub
Your kernel version in GRUB_DEFAULT may differ, but it should be older than your latest one after your update. Now let's make sure our backup of your grub file is also intact:
cat /etc/default/grub.bak
So what does that complex command do?
- Backs up grub into a bak file for easy restoration.
- Removes SAVED from the grub file.
- Detects and inserts the reference to the second to newest kernel to become your new default kernel on your Framework laptop.
- Updates grub.
Fantastic. When we reboot, the default kernel you will be booted into will always be the one listed in the current grub file. In my image example above, it was 6.2.9 as the default.
Note: When you boot up your Framework laptop and you see a grub screen, the second kernel should be highlighted for you - all you would do is press enter or wait for it to boot. If it simply boots without a grub menu, same, it will boot into the kernel shown in your cat /etc/default/grub results.
Additional Grub Options
If you are ready to run new updates and see if there is yet another kernel that will fix this one. Let's first create a new Btrfs-Assistant snapshot. Again, we recommend doing this manually to ensure everything is done correctly.
Now let's run our updates and restore grub to a natural state simultaneously. This will tell Fedora we want to update and boot to the latest kernel available.
sudo dnf upgrade && sudo cp /etc/default/grub.bak /etc/default/grub && sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
Now you'll reboot, and you should be booted into whatever the latest kernel happens to be. If the updates offer a new kernel, you'll be booted into that.
Some things to note:
- No kernel update, but you're back in that problem kernel again - which is why you rolled back your kernel in the first place. If this happens, re-run the kernel rollback #1 script above.
- A new kernel is presented, but it also has issues - you need to go back two kernels from the current one. No problem, you can run this below, and it will rollback the default kernel to the third down - what was previously the second down. Fedora will keep three kernels available by default.
Kernel rollback to third kernel
Kernel rollback #2 - third kernel in grub
sudo cp /etc/default/grub /etc/default/grub.bak && sudo sed -i '/GRUB_DEFAULT=saved/d' /etc/default/grub && sudo grubby --info=ALL | grep -E "Fedora Linux (.*)-(.*)" | sed 's/title=//' | tail -n 3 | head -n 1 | xargs -I {} sudo sed -i '3s#.*#GRUB_DEFAULT="{}"#' /etc/default/grub && sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
The above has all the same features as kernel rollback #1, except it will default the third kernel down instead of the second one. Remember, the rollback kernel option is the same.
The kernel rollback #2 command is similar to the previous one but selects the third most recent kernel as the default. Here's a breakdown of each command being executed:
- Backs up grub into a bak file for easy restoration.
- Removes SAVED from the grub file.
- Detects and inserts the reference to the third to newest kernel to become your new default kernel on your Framework laptop.
- Updates grub.
Restore original grub to original settings
With updates:
sudo dnf upgrade && sudo cp /etc/default/grub.bak /etc/default/grub && sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg
or without updates
sudo cp /etc/default/grub.bak /etc/default/grub && sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg