Snapshots are a built-in backup method that data center admins shouldn't overlook. Here's an easy way to create snapshots on a btrfs (or Butter FS) filesystem to back up files and folders.
The btrfs (often called Butter FS) is a modern copy on write filesystem that offers some seriously handy features and focuses on fault tolerance and repair. The developers of btrfs have also gone to great lengths to make it easy to use. No other aspect of btrfs highlights all of this more than the ability to take snapshots of a subvolume and roll back to a previous working state.
I'll show how easy it is to do just that on an external flash drive (for the sake of simplicity). You might want to test this process on a similar device before you're ready to take this to a production machine.
Creating the btrfs filesystem
I'll demonstrate this on a Ubuntu 16.04 machine. If you are using a Linux distribution that doesn't work with sudo, you'll need to first su to the root user and then remove sudo from any commands you see listed.
If you don't already have a drive formatted with btrfs, insert the drive, open a terminal window, and follow these steps (please make sure there is no necessary data on the drive).
- Open a terminal window.
- Locate the drive name (this can be done by issuing the command dmesg and locating the name of the attached drive in the output).
- Issue the command sudo mkfs.btrfs -f /dev/sdX (X is the letter associated with the drive to be formatted) and allow the formatting to complete.
In order to roll back with btrfs, we'll need to create a subvolume. To do this, you should make sure the btrf drive is mounted. Chances are, the drive will automatically mount once the file system is created. If it doesn't, you can either open your file manager and click the btrfs drive to mount it, or manually mount it from the command line like this (X is the letter associated with the drive, and /PATH/TO/MOUNT is the exact path where you'll mount the drive):
sudo mount /dev/sdX /PATH/TO/MOUNT
For the sake of this example, let's say I've mounted the btrfs drive to /media/jlwallen/btrfs (you'll want to change this path accordingly).
With the drive mounted, you can create a subvolume with the command:
sudo btrfs subvolume create /media/jlwallen/btrfs/testing1
We can create more subvolumes just as easily:
- sudo btrfs subvolume create /media/jlwallen/btrfs/testing2
- sudo btrfs subvolume create /media/jlwallen/btrfs/sub1
- sudo btrfs subvolume create /media/jlwallen/btrfs/sub2
You can then dump dummy files into your subvolumes (for testing purposes). It is important that you list out the ID of the subvolume (you'll need that later). For this, issue the command:
sudo btrfs subvolume list /media/jlwallen/btrfs
The above command should reveal the ID numbers for each of your subvolumes (Figure A).
A btrfs subvolume listing.
Mounting a subvolume
Before creating a snapshot, we have to learn how to mount a subvolume (as opposed to mounting the entire btrfs drive); this is where the subvolume ID is required. To mount only a subvolume (we'll mount testing1, whose ID is 260), do the following.
- Open a terminal window.
- Unmount the btrfs drive with the command sudo umount /media/jlwallen/btrfs.
- Mount the subvolume with the command sudo mount -o subvolid=259 /dev/sdb /media/jlwallen/btrfs/.
- Issue the command ls /media/jlwallen/btrfs/.
When you run the ls /media/jlwallen/btrfs you should see the dummy files listed that you added to the sub1 subvolume. You can unmount the subvolume by issuing the command sudo umount /media/jlwallen/btrfs.
Creating a snapshot
Let's create a snapshot of the sub1 subvolume. To do this, mount the btrfs drive (or only the subvolume that will be used for the snapshot), and issue the command:
btrfs subvolume snapshot /media/jlwallen/btrfs/sub1 /media/jlwallen/btrfs/08162016_snapshot
Note: I created the snapshot with the date August 16, 2016.
If you list the contents of /media/jlwallen/btrfs, you should see a folder with the same name as the snapshot. Issue the command sudo btrfs subvolume list /media/jlwallen/btrfs and you will see the new snapshot has its own unique ID. Guess what? You can now mount that snapshot as if it were the original subvolume.
Make it work for you
Naturally, this only works if you create regular snapshots—if you don't take snapshots, you won't have anything to roll back to. You can always get creative and write a shell script for the creation of snapshots (and then call that shell script from cron). There are also tools that can handle this task for you.
In future posts, I'll cover some of the ways you can work with btrfs to automatically create snapshots. Until then, remember to run the snapshot command so you always have an up-to-date copy of your btrfs subvolume.
- How to install Ubuntu server 16.04 and the Webmin GUI (TechRepublic)
- How to move VirtualBox VMs from one drive to another (TechRepublic)
- How to install CentOS Web Panel for easy server administration (TechRepublic)
- How to enable WebDAV on your Ubuntu server (TechRepublic)
- Building the Software Defined Data Center (ZDNet/TechRepublic special feature)
- SDN and the data center: Deployment plans, business drivers, and preferred vendors (Tech Pro Research)