Linux – Restore Btrfs snapshot from subdirectory to parent

btrfslinuxsnapshot

I have a Btrfs partition which has a single subvolume at the top level (/root). It has subvol=root option in /etc/fstab.

Every week, I take a readonly snapshot into /root/snapshots/... using:

btrfs subvolume snapshot -r / /snapshots/"$(date --rfc-3339=date)"

(paths don't have /root because it's mounted as subvol=root).

Now let's say something went wrong and I wanted to restore my root subvolume from a snapshot, I boot from a USB disk and mount the partition as /mnt/disk without subvol=root. If I try to run:

btrfs subvolume snapshot /mnt/disk/root/snapshots/2015-05-01 /mnt/disk/root

It creates the new subvolume as /mnt/disk/root/2015-05-01 instead of replacing /mnt/disk/root/. If I try to delete it first by running

btrfs subvolume delete /mnt/disk/root

It gives the error message:

ERROR: cannot delete '/mnt/disk/root' - Directory not empty

Is there a way to do this? Or should I get into the habit of creating snapshots outside the subvolume being snapshotted?

Best Answer

I think this is where you went wrong:

If I try to run:

btrfs subvolume snapshot /mnt/disk/root/snapshots/2015-05-01 /mnt/disk/root

It creates the new subvolume as /mnt/disk/root/2015-05-01 instead of replacing /mnt/disk/root/.

btrfs subvolume snapshot is used to create a snapshot of the first argument, and it place it in the directory given by the second argument. It sounds like you're expecting it to replace /mnt/disk/root instead.

Before you try to overwrite the /mnt/disk/root subvolume, you'll need to move or delete it (for example mv /mnt/disk/root /mnt/disk/root-backup-during-restore). Then do:

btrfs subvolume snapshot /mnt/disk/root/snapshots/2015-05-01 /mnt/disk/

And then:

mv /mnt/disk/2015-05-01 /mnt/disk/root
Related Question