Taming Disk Space Hogs with btrfs Snapshots and Automatic Pruning

Introduction to btrfs Snapshots

I’ve been running Linux for years, and one thing I’ve learned is the importance of managing disk space effectively. btrfs has been a game-changer for me - its snapshotting and automatic pruning features have saved me from more than a few headaches. In this article, I’ll share my experience with using btrfs snapshots to keep my systems running smoothly.

What are btrfs Snapshots?

btrfs snapshots are essentially temporary copies of a subvolume, which can be used to roll back changes or recover data if something goes wrong. I’ve seen this go wrong when I’ve accidentally overwritten important files or filled up my disk with unnecessary data. With btrfs snapshots, you can create a snapshot of your system, make changes, and then delete the snapshot if everything works out. The real trick is to use them consistently and make them a part of your workflow.

Creating btrfs Snapshots

To create a btrfs snapshot, you’ll need to have a btrfs file system installed on your system. Don’t bother with trying to convert an existing file system - it’s usually easier to just start from scratch. Once you have btrfs up and running, you can create a snapshot using the following command:

btrfs subvolume snapshot /path/to/subvolume /path/to/snapshot

This will create a new snapshot of the specified subvolume, which you can then use to roll back changes or recover data. In practice, I usually start with a simple snapshot and then customize it to fit my needs.

Automatic Pruning with btrfs

One of the most powerful features of btrfs is its ability to automatically prune snapshots. This is where people usually get burned - they create a bunch of snapshots and then forget to delete them, which can lead to disk space issues. To enable automatic pruning, you’ll need to use the btrfs subvolume set-default command to set the default subvolume, and then use btrfs subvolume snapshot with the -r option to create a read-only snapshot. You can then use btrfs subvolume delete to delete the snapshot after a certain amount of time.

For example, you can use the following command to create a daily snapshot and keep it for 30 days:

btrfs subvolume snapshot -r /path/to/subvolume /path/to/snapshot

You can then use a cron job to delete the snapshot after 30 days:

0 0 * * * btrfs subvolume delete /path/to/snapshot

This will ensure that your disk space is kept under control, and you’ll always have a recent snapshot available in case something goes wrong.

Security Considerations

When using btrfs snapshots, security is a top concern. Since snapshots are read-only, they can’t be modified by an attacker, which makes them a great way to protect your data. However, if an attacker gains access to your system, they may be able to delete or modify the snapshots, which could lead to data loss. To mitigate this risk, make sure to set up proper permissions and access controls on your btrfs file system, and consider using encryption to protect your data.

Real-World Example

I use btrfs snapshots to manage my homelab servers, which run a variety of services like Nextcloud, GitLab, and Plex. I create a daily snapshot of each server, which allows me to roll back changes in case something goes wrong. I also use automatic pruning to keep my disk space under control, which ensures that I don’t run out of space due to old snapshots.

Troubleshooting Tips

If you’re having trouble with btrfs snapshots, here are a few troubleshooting tips to keep in mind:

  • Make sure you have enough disk space available to create a snapshot. If you’re running low on disk space, you may need to delete some files or resize your partition.
  • Use the btrfs subvolume list command to list all subvolumes on your system, which can help you identify the source of the problem.
  • Check the btrfs logs for any error messages, which can provide valuable insight into what’s going wrong.

For more information on btrfs, I recommend checking out the official btrfs documentation on kernel.org. You can also find more information on using btrfs with systemd.


See also