.shock - Fotolia
Manage Btrfs snapshots with the SUSE Snapper utility
Learn how to use Btrfs snapshots with Linux utilities, such as Snapper and GRUB2, to revert back to previous states in the event of a configuration mishap.
SUSE Linux Enterprise Server was among the first major Linux distributions to support the Btrfs file system. One of Btrfs' many features is the option to create snapshots.
To understand Btrfs snapshots, it's important to know how they are related to Btrfs subvolumes. A subvolume is an independent name space on a Btrfs file system -- a directory that can be mounted with specific attributes, and for which specific features can be enabled. Btrfs snapshots work on a subvolume level.
How snapshots work with CoW
Snapshots are based on the Btrfs copy-on-write (CoW) feature. This means that before data is modified, the original data blocks are securely kept. CoW makes it easier to return to a file's previous state.
Btrfs snapshots use the CoW feature to keep the status of files as they were in a specific point in time. Initially, the snapshot contains no actual data blocks, but grows as files are modified. The longer a snapshot is kept, the more modified blocks it contains -- and the bigger it becomes. Admins can mount the snapshot, which makes it easier to revert to a previous state of the file system or to compare the current state with the previous state.
Admins can also manually create snapshots of Btrfs subvolumes to ensure the state of a subvolume is frozen, and use the btrfs subvolume snapshot command to create a subvolume snapshot.
How snapshots work with Snapper
While the btrfs command provides an interface to manually create snapshots, the Snapper utility automatically creates snapshots based on events that occur in the SUSE configuration tool YaST or zypper. When you install software or use YaST to modify configurations, Snapper automatically creates snapshots, as well. In the event of a server configuration mistake, admins can use the Snapper snapshots to revert back to the previous state.
The Snapper tool offers a timeline of snapshots that you can use to easily revert to a specific moment in the state of the system. It automatically cleans up old Btrfs snapshots and provides a command-line interface to manage snapshots.
Another advantage of the SUSE Snapper utility is that it integrates with the GRUB2 boot menu. Through the boot menu, you can boot into an older state of a SUSE server and easily revert back to before changes were applied.
Here's how to boot into an older state on a SLES 12 server or OpenSUSE 12 and later:
1. From a root console, type snapper list. This will show currently existing snapshots;
2. Type snapper create -d "mysnapshot". This creates a snapshot with the name "mysnapshot";
3. Open an editor and add a line to the /etc/hosts file. It doesn't matter what you put in the line, but make sure you don't interfere with your current configuration. Confirm that the line starts with a comment sign (#);
4. Now start the YaST snapper module, using the YaST snapper command. Highlight the snapshot that you've just created and select "Show Changes." It will show you the /etc/hosts file, which clearly indicates how it has changed; and
5. Quit YaST, and type the snapper list command again. You'll see that a new snapshot with the name YaST snapper has been created. Notice the number of the mysnapshot snapshot and the number of the YaST Snapper snapshot. Now use snapper diff 2 -- depending on the snapshot number you're looking for -- and you'll see the differences between the snapshots being listed in Linux diff style.
Snapper offers a good command-line interface that admins can use to manually manage changes. The tool is particularly useful with GRUB2, allowing administrators to revert to a previously working state in the event servers won't boot anymore.