Introduction to btrfs Snapshots
As someone who’s run a homelab for years, I’ve learned that managing disk space is key to keeping my servers running smoothly. One of the most effective ways to keep disk usage under control is by using btrfs snapshots. I’ve seen this go wrong when people don’t have a solid backup strategy, and btrfs snapshots have been a lifesaver for me. The real trick is to use them in conjunction with a solid backup plan.
Creating btrfs Snapshots
To create a btrfs snapshot, I use the following command:
btrfs subvolume snapshot /path/to/source /path/to/destination
For example, to create a snapshot of my root file system, I would use:
btrfs subvolume snapshot / /mnt/snapshots/$(date +%Y-%m-%d)
This command creates a snapshot of my root file system and saves it to the /mnt/snapshots directory with a timestamped name. Don’t bother with manually naming snapshots - the timestamp keeps things organized.
Automatic Pruning of Snapshots
To automatically prune old snapshots, I use a combination of btrfs subvolume list and btrfs subvolume delete commands in a script. The script lists all snapshots, filters out the ones older than a specified number of days, and deletes them. Here’s an example script:
#!/bin/bash
# Set the number of days to keep snapshots
KEEP_DAYS=14
# List all snapshots
SNAPSHOTS=$(btrfs subvolume list /mnt/snapshots | grep -oP '(?<=ID \d+ \S+ ).*')
# Delete snapshots older than KEEP_DAYS
for snapshot in $SNAPSHOTS; do
if [ $(date -d "$snapshot" +%s) -lt $(date -d "-$KEEP_DAYS days" +%s) ]; then
btrfs subvolume delete /mnt/snapshots/$snapshot
fi
done
I usually start with a simple script like this and adjust the KEEP_DAYS variable as needed. I schedule this script to run daily using systemd timers. If you’re new to systemd timers, I recommend checking out the systemd.io website for more information.
Security Considerations
When using btrfs snapshots, security is a top concern. This is where people usually get burned - snapshots can contain sensitive data, so it’s crucial to ensure that they are properly secured. In practice, I use btrfs encryption to protect my snapshots. For more information on btrfs encryption, you can refer to the btrfs wiki.
Troubleshooting
If you encounter issues with btrfs snapshots, the btrfs troubleshooting guide is a great resource. It’s saved me a lot of time and hassle over the years.
See also
- Taming tmux: How I Replaced My Desktop's Taskbar with a Custom Terminal Layout
- Taming the initramfs: How I Solved My Linux Boot Hangs with Dracut Debugging
- Taming the Beast: Customizing tmux for a More Productive Terminal Workflow
- Taming Resource-Intensive Background Jobs with nice and ionice
- Taming the Noise in journalctl with Systemd Journal Filters and Priorities