Backup and Snapshot Btrfs

written 2024-01-30

Managing backups should be hassle-free. This article introduces Btrfs’s capabilities for efficient backup and snapshot management. Let’s dive in!

Firstly, what is Btrfs? It’s a modern filesystem with advanced features like snapshots and volume management. This guide will help you leverage these for efficient data backups.

The Setup

We start by installing btrbk, a tool that simplifies Btrfs operations.

pacman -S btrbk

Systemd timers and services will automate snapshot and backup processes. The timer triggers every 15 minutes, and the service executes the backup script.

# /etc/systemd/system/timemachine.timer
[Unit]
Description=Run timemachine evey 15 minutes

[Timer]
OnCalendar=*:0/15
Persistent=true

[Install]
WantedBy=timers.target
# /etc/systemd/system/timemachine.service
[Unit]
Description=Start btrbk snapshopt
[Service]
Type=oneshot
ExecStart=/etc/btrbk/snapshot_or_full.sh
[Install]
WantedBy=multi-user.target

Crafting the Backup Script

The script creates a snapshot of the root subvolume and initiates the backup, provided the external server is reachable.

# /etc/btrbk/snapshot_or_full.sh

#!/bin/bash

SERVER_IP="YOUR_SERVERS_IP"

ping -c 1 $SERVER_IP &> /dev/null

if [ $? -eq 0 ]; then
    echo 'Server responsive, initiating full backup'
    btrbk -c /etc/btrbk/btrbk-home.conf run
else
    echo 'Server unresponsive, creating snapshots'
    btrbk run
fi

Configuring Btrbk

We have two configurations: one for full backups to an external server and another for local snapshots.

# /etc/btrbk/btrbk.conf: (Local Snapshots)

timestamp_format	long

# Snapshots
snapshot_preserve_min	18h
snapshot_preserve	48h
snapshot_dir		.snapshots

# Incremental Backups
target_preserve_min	no
target_preserve		30d	12w	*m

# SSH
ssh_identity /etc/btrbk/ssh/id_rsa

volume /
  subvolume	data
  subvolume	home
  subvolume	/


```bash
# /etc/btrbk/btrbk-home.conf: (External Backups)
timestamp_format	long

# Snapshots
snapshot_preserve_min	18h
snapshot_preserve	48h
snapshot_dir		.snapshots

# Incremental Backups
target_preserve_min	no
target_preserve		30d	12w	*m

# SSH
ssh_identity /etc/btrbk/ssh/id_rsa

volume /
  subvolume	data
  subvolume	home
    target	ssh://YOUR_SERVERS_IP/storage/backups
  subvolume	/
    target	ssh://YOUR_SERVERS_IP/storage/backups

Restoration Simplified

if we just need to access a single file, we can just visit the .snapshots folder and copy the file from there. If we need to restore the whole system, we can use the following command.

btrfs send /mnt/storage/backups/data.YYYYMMDD | btrfs receive /mnt/
# move the borken subvolume
mv /data /data.broken
# rename the restored subvolume
mv /mnt/data /data
# create read-write subvolume
btrfs subvolume snapshot /mnt/data.YYYYMMDD /data
# clean up
btrfs subvolume delete /data.broken

Closing Thoughts

This approach streamlines Btrfs’s powerful features for reliable backups and snapshots. Feel free to share your insights or questions via email.

Bithive, signing off!

There is no comment system. If you want to contact me about this article, you can do so via e-mail or Mastodon.