Btrfs Snapshot – Restore Single Files from a Btrfs Snapshot Without Full Copy

btrfssnapshot

Background

I maintain a series of hourly snapshots on my btrfs filesystem using btrbk. The subvolume and snapshots are stored like this (names shortened to make it easier to follow):

/@vol               (subvolume)
/btrbk/@vol.00      (snapshots of /@vol, one per hour)
/btrbk/@vol.01      ...
/home/user/vol      (mountpoint for /@vol)

Now I deleted a single file in /home/user/vol which I should not have deleted. No problem, I can easily restore from the latest snapshot:

# cd /home/user/vol
# cp -a --reflink=always /btrbk/@vol.01/all_ipv4_numbers.txt ./
cp: failed to clone './all_ipv4_numbers.txt' from '/btrbk/@vol.01/all_ipv4_numbers.txt': Invalid cross-device link

Seems I can't "reflink" the file. But I don't want to end up with two copies of the file. Ideally I want it to look like nothing happened between the most recent snapshot and the next snapshot that will be taken in the future.

(Yes, I also have actual backups, but restoring from snapshots are more convenient whenever available!)

Question

How do I restore a single file from a btrfs snapshot while keeping all the nice references and magic that makes btrfs know that it is the same data in all the snapshots?

Best Answer

You can only use cp --reflink when the source and target are both on the same mount point. For this purpose, mounting the same filesystem to multiple directories doesn't count. Mount the "root" volume of the btrfs filesystem somewhere, then do cp --reflink between the snapshot subvolume and @vol that are under that directory.

Related Question