Saturday, July 2, 2016

How To Create Snapshots And Restore Your Linux System Using Btrfs

Btrfs, which can be pronounced as “Butter FS”, “Better FS”, or
“B-Tree FS”, is a modern file system that began development back in
2007. It was merged into the mainline Linux kernel in the beginning of
2009 and debuted in the Linux 2.6.29 release. Btrfs is GPL-licensed but
currently considered unstable. Thus, Linux distributions tend to ship
with Btrfs as an option but not as the default. Btrfs exists because the
developers wanted to expand the functionality of a file system to
include pooling, snapshots, and checksums among other things. Btrfs is
not a successor to the default Ext4 file system used in most Linux
distributions, but it can be expected to replace Ext4 in the future.
Btrfs is expected to offer better scalability and reliability. It is a
copy-on-write file system intended to address various weaknesses in
current Linux file systems. Primary focus points include fault
tolerance, repair, and easy administration.
In this article we will show that how you can roll back to the
previous system state with apt-btrfs-snapshot on your Linux system that
uses the btrfs file system. Any failed operation takes away much of the
pain system administrators have to deal with normally as
apt-btrfs-snapshot creates a snapshot of the system before any apt
operation. In this way we will be able to easily restore the previous
system state which is one of the greatest features of the btrfs file
system.

Prerequisites:

In order to create snapshots and restore your Linux system using
btrfs, you need to install the whole system on a btrfs file system and
there should be no separate /boot partition on an ext file system. If
you use a separate /boot partition and apt installs anything in that
partition , you cannot undo changes to the /boot partition with
apt-btrfs-snapshot as only changes on the btrfs partition can be
reverted.
Once your file system is on btrfs then login your servers using root user credentials to get started.

$ sudo -i

Installing Btrfs Package:

Let’s run the following command to install ‘apt-btrfs-snapshot’ package on your Linux server before using it.

# apt-get install apt-btrfs-snapshot

Once the package is installed, check if apt-btrfs-snapshot is able to
create snapshots on apt operations by running the following command.

# apt-btrfs-snapshot supported

If it display ‘Supported’ in its output then all is set to go. If it
doesn’t then your btrfs subvolume layout probably differs from Ubuntu’s
server default layout.
You can check the default layout of btrfs subvolumes on Ubuntu server with below command.

Creating Snapshot with Apt Operation:

To test the rollback operation to the previous state, we are going to
run some apt operation like apt-get upgrade using below commands as
shown.
Run below commands to update your package database and upgrade your system.

# apt-get update

# apt-get upgrade

Press ‘Y’ key to continue, during the system upgrade process, you
will see that an ‘apt-btrfs-snapshot’ has automatically created as
snapshot of our system before the upgrade as shown in the image.
You can also check the list of your current snapshots with following commands.

# btrfs subvolume list /

# apt-btrfs-snapshot list

Restoring Snapshot:

Now if you want to restore the previous system state in case of any
failure during the system upgrade and you want to do a rollback.
To do so let’s mount the btrfs filesystem to a separate location, fol example /mnt .

Where ‘@apt-snapshot-2016-05-24_02:18:31′ 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 @ . After renaming reboot your
system and you will be glad to see that your system is at the previous
state of before system upgrade.
You can confirm by repeating the ‘apt-get upgrade’ command which will show the same packages to be upgrade.

Deleting The Snapshot:

After successfully restoring the file system, now if you wish to
delete this volume to make some free space on your system. Then run the
below command after mounting the volume.

# mount /dev/sda1 /mnt/

# btrfs subvolume delete /mnt/@_latest-root/

# umount /mnt

Conclusion:

A Btrfs snapshot is actually a subvolume that shares its data with
some other subvolume, using Btrfs’ copy-on-write capabilities, and
modifications to a snapshot are not visible in the original subvolume.
Once a writable snapshot is made, it can be treated as an alternate
version of the original file system. For example, to rollback to a
snapshot, a modified original subvolume needs to be unmounted and the
snapshot needs to be mounted in its place. So, at that point, the
original subvolume may also be deleted. Btrfs is becoming increasingly
important to the future of Linux systems as its possible to dynamically
resize mounted filesystems which can span physical volumes, with
optional RAID support. While physical volumes can be added to and
removed from mounted filesystems. More over you can take the snapshots
including read-only and read-write.