Quickly Restore a Mistakenly Deleted Home Folder with Btrfs Snapshots

Accidentally running rm -rf ~/Documents or letting a rogue script wipe your entire home directory is a nightmare that can happen to anyone. If your root filesystem sits on Btrfs, you can usually recover almost instantly by leveraging snapshots. This post walks through the steps you need to take—from setting up snapshots before you delete anything to restoring the lost data—and discusses the trade‑offs and pitfalls you should watch out for.


Why Btrfs Snapshots Matter

Btrfs supports copy‑on‑write (COW) and snapshotting out of the box. A snapshot is a read‑only or read‑write view of a subvolume at a particular point in time. Because Btrfs records only the differences between snapshots, creating one is fast and inexpensive in terms of space. If you have a snapshot of your home subvolume, you can roll back to it in seconds, even if you’ve deleted a large portion of the data.


1. Set Up a Snapshot Strategy

Before you can recover from a deletion, you need to have snapshots in place. The simplest approach is to create a daily snapshot of your home subvolume and keep the last few days.

# Assume /home is a Btrfs subvolume
sudo btrfs subvolume snapshot -r /home /home_snapshots/$(date +%Y-%m-%d)
  • -r makes the snapshot read‑only, which protects it from accidental writes.
  • Store snapshots in a dedicated directory (/home_snapshots) that is itself a subvolume on the same Btrfs filesystem to keep them on the same storage pool.

Automate with cron

Add this to /etc/cron.daily/btrfs-snapshots:

#!/bin/bash
SNAPDIR="/home_snapshots"
mkdir -p "$SNAPDIR"
btrfs subvolume snapshot -r /home "$SNAPDIR/$(date +%Y-%m-%d)"

Make it executable:

sudo chmod +x /etc/cron.daily/btrfs-snapshots

Now you have a daily snapshot automatically created.


2. Verify Snapshot Health

Snapshots can become corrupted if the underlying filesystem is damaged. Before you rely on them for recovery, check their integrity:

sudo btrfs filesystem df /home
sudo btrfs filesystem show

If you see any errors, run a full scrub:

sudo btrfs scrub start -Bd /home

Scrubs verify checksums and rebuild corrupted data from redundancy (RAID, mirrors, etc.). A scrub can take a while on large volumes, but it’s worth the peace of mind.


3. Recovering a Deleted Home Folder

Let’s walk through a realistic scenario:

  1. Mistake: The user accidentally deletes ~/Documents.
  2. Snapshot: A snapshot from yesterday exists at /home_snapshots/2026-11-28.
  3. Goal: Restore Documents without affecting other files.

3.1 Find the Snapshot

List snapshots:

ls -l /home_snapshots

You should see directories named by date. Pick the one closest to the deletion time.

3.2 Mount the Snapshot (Optional)

If you want to inspect the snapshot before restoring, mount it:

sudo mkdir /mnt/home_snap
sudo mount -o subvol=/home_snapshots/2026-11-28 /dev/sda1 /mnt/home_snap

Replace /dev/sda1 with the device that hosts your Btrfs filesystem. Now you can browse /mnt/home_snap.

3.3 Restore the Specific Folder

The easiest way is to copy the missing folder from the snapshot back into your home directory:

rsync -aAXv --progress /home_snapshots/2026-11-28/Documents/ ~/Documents/
  • -a preserves permissions, timestamps, and symlinks.
  • -X preserves extended attributes (useful for SELinux or AppArmor contexts).
  • -A preserves ACLs.

If you prefer a simple copy:

cp -a /home_snapshots/2026-11-28/Documents/ ~/Documents/

Both commands will recreate the folder and all its contents.

3.4 Verify the Restore

Check that the files are back:

ls -l ~/Documents

Open a few to ensure integrity. If you see missing files, you might need to restore from an earlier snapshot.


4. Full Rollback (When the Whole Home Is Gone)

If the entire /home subvolume is lost, you can replace it with a snapshot:

# 1. Rename the damaged subvolume
sudo mv /home /home.broken

# 2. Create a new subvolume to host the restored data
sudo btrfs subvolume create /home

# 3. Copy the snapshot over
sudo rsync -aAXv /home_snapshots/2026-11-28/ /home/

# 4. Clean up
sudo rm -rf /home.broken

This approach keeps the snapshot intact for future use.


5. Trade‑offs and Caveats

Aspect Benefit Drawback
Speed Snapshots are instant to create; restoration is just a copy. If you need to restore a large directory, copying can still take time.
Space Snapshots share data blocks; only changes consume space. Over time, many snapshots can accumulate and consume significant space if not pruned.
Read‑only protection Read‑only snapshots prevent accidental writes. You cannot modify a snapshot; you must copy data out.
Data consistency Snapshots capture a consistent view of the filesystem. If you delete files after a snapshot, they’re gone until the next snapshot.
Redundancy Btrfs can use RAID or mirrors for redundancy. Requires additional disks; misconfiguration can lead to data loss.

Pruning Old Snapshots

Keep your snapshot directory tidy:

# Keep only the last 7 days
find /home_snapshots -maxdepth 1 -type d -mtime +7 -exec rm -rf {} \;

Run this nightly to prevent disk bloat.


6. Common Mistakes and How to Avoid Them

Mistake Fix
Deleting the snapshot directory Snapshots are stored in a subvolume; avoid deleting /home_snapshots.
Mounting the wrong subvolume Verify the subvolume ID with btrfs subvolume list /home.
Using cp -r without -a You’ll lose permissions and ACLs.
Assuming snapshots are instant Large snapshots can take a few seconds to create if many changes exist.
Not checking for corruption Run btrfs scrub regularly, especially after power failures.

7. Security Considerations

  • Snapshot Permissions: By default, snapshots inherit the parent subvolume’s permissions. If your home directory is world‑readable, snapshots will be too. Restrict access with chmod 700 /home_snapshots.
  • Audit Trail: Keep a log of snapshot creation times. This helps identify when a deletion occurred.
  • Encryption: If you encrypt your home directory with LUKS, snapshots will still be encrypted. However, if you use Btrfs’s native encryption, snapshots will preserve the encrypted state. Just remember that restoring a snapshot does not decrypt the data; you still need the key.

8. Real‑World Example: A Home Server

I set up a small home media server on a 1 TB SSD using Btrfs. I created a daily snapshot of /srv/media and a weekly snapshot of /home. One night, a misconfigured backup script deleted /home/user/Downloads. Because I had a snapshot from the previous day, I simply ran:

rsync -aAXv /home_snapshots/2026-11-27/Downloads/ /home/user/Downloads/

The folder reappeared in under a minute, and the media server continued running without interruption. This quick recovery saved me from a full system reinstall.


9. When Snapshots Aren’t Enough

  • Very Recent Deletions: If you delete files and immediately try to recover, the snapshot may not exist yet. In that case, consider using extundelete or testdisk on non‑Btrfs filesystems.
  • Corrupted Snapshots: If a snapshot is corrupted, you’ll need to restore from a backup or a previous snapshot.
  • Hardware Failure: Snapshots don’t protect against disk failure. Pair Btrfs snapshots with RAID or external backups for full protection.

10. Quick Reference Commands

# Create a read‑only snapshot
sudo btrfs subvolume snapshot -r /home /home_snapshots/$(date +%Y-%m-%d)

# List snapshots
ls -l /home_snapshots

# Mount a snapshot for inspection
sudo mount -o subvol=/home_snapshots/2026-11-28 /dev/sda1 /mnt/home_snap

# Restore a folder
rsync -aAXv /home_snapshots/2026-11-28/Documents/ ~/Documents/

# Full rollback
sudo mv /home /home.broken
sudo btrfs subvolume create /home
sudo rsync -aAXv /home_snapshots/2026-11-28/ /home/
sudo rm -rf /home.broken

See also