Nextcloud-snap and BTRFS

written by ushills on 2019-03-11

The BTRFS is a copy-on-write (COW) alternative filesystem type to the more traditional Linux filesystems of ext3/ext4 and is now considered to be sufficiently stable to be included with the majority of Linux Operating Systems and is the default now with OpenSuse.

BTRFS has a number of significant advantages compared to more traditional filesystems and a couple of these are great for server use, particularly with a Nextcloud instance. The most significant feature, in my opinion, is the ability to take atomic snapshots of the system and restore them in place, which the filesystem just treats as a power interruption. Secondly, these snaphots take up little room as they only store the differences from the main file system, this is also great for efficient incremental backups.

What this means with Nextcloud and in particular Nextcloud-snap, with its one directory approach, it that you can take a BTRFS snapshot, back the snapshot up (more on that later!) and if anything goes wrong, restore the Nextcloud instance as it was at the time of the backup without requiring any additional re-configuration or complicated database exporting etc which can be complicated with a standard Nextcloud installation.

To configure nextcloud-snap to use this simplified backup/restore structure, you have to install your operating system with a BTRFS filesystem. With Armbian and Debian, this is an option when installing and consists of setting your system to use the whole disk as a partition and to set that partitions format as BTRFS rather than the default of Ext4.

The concept of BTRFS differs from traditional guidance with Ext4 and there is no need to create other partitions for /home, /var etc. subvolumes take the place of partitions with BTRFS and as I have learned have major advantages.

Once you have your OS using BTRFS, you will notice, little or no difference initially, however, here is the good part.

Create a BTRFS subvolume

If you already have installed snap or Nextcloud, you need to move your snap directory, create a subvolume and move the original snap directory back as follows.

You will need to do this as root or using sudo

If you already have installed snap or Nextcloud, you need to move your snap directory, create a subvolume and move the original snap directory back as follows.

$ sudo mv /var/snap /var/tempsnap
$ sudo btrfs subvolume create /var/snap
$ sudo mv /var/tempsnap/* /var/snap/

Make a snapshot

Once you have a working BTRFS subvolume which you can test with:

$ sudo btrfs subvolume list /

which should list the directory /var/snap as a valid subvolume, making a snapshot is as easy as:

$ sudo btrfs subvolume snapshot -r /var/snap /var/snap/BACKUP

This creates a read-only snapshot which is an exact copy of the folder at the time the snapshot was made, with BTRFS the significant advantage is that this snapshot takes up very little additional space as only the changes between the snapshot and the original files are stored. The unchanged files are similar to symlinks to the original files with only the changed parts of the files stored.

This snapshot can be backup up with traditional backup methods i.e. rsync, cp, rdiff-backup, s3cmd or similar, however, there are specific backup applications available for BTRFS that take advantage of the efficient incremental backup available with BTRFS and this is covered in my next post.