Introduction to btrfs Snapshots
I’ve been using btrfs for years, and one of its most powerful features is its ability to create snapshots. These are read-only copies of a subvolume at a particular point in time, which can be used for backups, testing, and even recovering from accidental file deletions or modifications. Don’t bother with other file systems if you need this level of flexibility.
Creating Snapshots
To get started with btrfs snapshots, you’ll first need to ensure that your system is using btrfs. Run lsblk -f and look for the btrfs label. If you’re using btrfs, creating a snapshot is straightforward:
btrfs subvolume snapshot /path/to/source /path/to/destination
For example, to create a snapshot of the / subvolume in a new subvolume called @snapshot, you would run:
btrfs subvolume snapshot / /mnt/btrfs/@snapshot
Note that the destination path must be on the same btrfs file system as the source. I’ve seen this go wrong when people forget this, so double-check your paths.
Automatic Pruning of Snapshots
Snapshots can quickly consume a large amount of disk space if not managed properly. This is where people usually get burned - they create a bunch of snapshots and then realize they’re out of disk space. To mitigate this, I usually start with a tool like snapper to automatically prune old snapshots. snapper is a command-line tool that allows you to easily create, manage, and delete btrfs snapshots.
To install snapper on a Debian-based system, run:
sudo apt-get install snapper
Once installed, you can configure snapper to automatically prune snapshots by editing the /etc/snapper/configs/root file. For example, to keep only the last 10 snapshots, add:
NUMBER_LIMIT="10"
You can also configure snapper to prune snapshots based on age, rather than just the number of snapshots. For example, to keep snapshots for up to 30 days, add:
TIMELINE_LIMIT="30"
In practice, this means you can set up snapper to automatically manage your snapshots, so you don’t have to worry about running out of disk space.
Using systemd to Automate Snapshot Creation
To automate the creation of snapshots, you can use systemd to run a script at regular intervals. For example, to create a snapshot every hour, create a new systemd timer file called btrfs-snapshot.timer with the following contents:
[Unit]
Description=Create btrfs snapshot every hour
[Timer]
OnUnitInactiveSec=1h
AccuracySec=1min
[Install]
WantedBy=timers.target
You’ll also need to create a corresponding systemd service file called btrfs-snapshot.service with the following contents:
[Unit]
Description=Create btrfs snapshot
[Service]
Type=oneshot
ExecStart=/usr/bin/btrfs subvolume snapshot / /mnt/btrfs/@snapshot
Once you’ve created these files, you can enable and start the timer using the following commands:
sudo systemctl enable btrfs-snapshot.timer
sudo systemctl start btrfs-snapshot.timer
The real trick is to set up systemd to automate the process, so you can focus on more important things.
Security Considerations
When using btrfs snapshots, it’s essential to consider the security implications. For example, if you’re using snapshots to store sensitive data, you’ll want to ensure that the snapshots are properly secured. One way to do this is to use encryption, such as LUKS to encrypt the underlying disk.
You should also be aware of the potential risks associated with storing multiple snapshots of sensitive data. If an attacker gains access to your system, they may be able to access the snapshots and retrieve sensitive information. To mitigate this risk, you can use a tool like snapper to automatically prune old snapshots, as described earlier.
For more information on btrfs and its features, you can visit the btrfs wiki or the kernel.org website.
Troubleshooting
If you encounter issues with btrfs snapshots, there are several troubleshooting steps you can take. First, check the btrfs file system for errors using the btrfs check command. You can also use the btrfs scrub command to scan the file system for errors and correct them.
If you’re experiencing issues with snapper, you can check the snapper logs for errors. You can also use the snapper list command to view a list of all snapshots, and the snapper delete command to delete a snapshot.
See also
- Taming Disk Space Usage with find and xargs in a Busy /var/log Directory
- Troubleshooting Slow systemd Service Restarts with systemd-analyze
- Taming SSH Config Chaos: Organizing Your SSH Client Settings for Multiple Servers and Identities
- Using SSH to Tunnel Traffic Through a Jump Host for Secure Access to a Remote Network
- Using Third-Party Repositories Without Polluting Your Package Manager