NAS, Part 4: Time machines | Automatic snapshotting with btrfs-snapshot
In the last part in this series, I compared ZFS with Btrfs. I ended up choosing Btrfs because it was easier to install and came with a number of advantages. Since last time, I've now put Btrfs to work and have about ~1.3 TiB of data stored in it (much of which is from various devices across the network automatically backing up to it). Before we continue, here's a list of the parts in the series so far:
- NAS, Part 3: Decisions | Choosing a Filesystem
- NAS, Part 2: Assembly and Installation
- NAS, Part 1: We need a bigger rocket
In this post, I'm going to talk about the automatic snapshotting I've setup. Btrfs supports creating snapshots, which are defined as subvolumes that are seeded with data from another subvolume (boundaries between subvolumes are not crossed). Most of the time, these are created to be read-only. In addition because of the copy-on-write system Btrfs uses, a snapshot takes no disk space on its own (other than that required to store the fact that it exists) - it only starts to consume disk space when files that it contains are modified in the original subvolume.
To this end, we can efficiently keep a rotating series of snapshots to serve as an initial safety net should a someone accidentally delete a file. Of course, we can't assume that snapshots will be ok as the only backup (I use Restic for that - I'm in the process of reconfiguring it for my new setup) - but they are still useful things to have.
To take a Btrfs snapshot, you can do this:
sudo btrfs subvolume snapshot -r path/to/source_subvolume path/to/target
The problem here, of course, is that you also need a way to delete old snapshots too. While I could roll my own solution for this, I figured that someone has already solved this problem - so it might save me some effort if I look for a pre-existing solution first.
After doing a bit of searching without success, I asked on Reddit, and the helpful folks there gave me a number of suggestions:
Of these 3, snapper
seemed to be the most popular. From some reading, it appeared to be powerful and flexible - at the cost of being easy to understand. btrbk
seemed to be feature-packed too, but in the end I decided on btrfs-snapshot
.
btrfs-snapshot
is designed to be used with cron
. For example, I have something like this for one of my subvolumes in root
user's crontab:
0 * * * * /root/btrfs-snapshot-rotation/btrfs-snapshot path/to/subvolume path/to/subvolume/.snapshots hourly 8
0 2 * * * /root/btrfs-snapshot-rotation/btrfs-snapshot path/to/subvolume path/to/subvolume/.snapshots daily 4
0 2 * * 7 /root/btrfs-snapshot-rotation/btrfs-snapshot path/to/subvolume path/to/subvolume/.snapshots weekly 4
Given a subvolume at path/to/subvolume
, it creates the following snapshots in a nested subvolume in path/to/subvolume/.snapshots
(which needs to be created manually: sudo btrfs subvolume create path/to/subvolume/.snapshots
):
- 8 x hourly snapshots
- 4 x daily snapshots
- 4 x weekly snapshots
I find the system so beautifully simple and easy to understand. This is important for me in a system like this, as it has to be easy for me to understand when I inevitably come back to it months or even years later when I've forgotten how it works. The arguments to btrfs-snapshot
are easy to understand, and are in the form path/to/source path/to/target tag_name number_of_snapshots_to_keep
.
This has the added bonus that if a user deletes a file accidentally in our shared drive, they can retrieve it on their own from the .snapshots
directory - without my intervention.
With this in place and the data (mostly) moved over, my NAS project is almost complete. The final task I have left to do is to setup a proper backup system with Restic to either a remote (e.g. Backblaze B2) or offline location (such as an external HDD).
The latter might prove to be a problem though, since the maximum amount of data I can store right now is 5.5 TiB and is only going to grow from there. Portable external hard drives I've seen online don't appear to go up that high, so I suspect I'll need to choose another plan.
Should I encounter some interesting issues when setting this final backup step up, I'll make an additional post in this series. If not though, this will probably be the last entry in this series. If you have any questions about my setup, please comment below! I'll dod my best to answer any questions.