2

When I tried to create a snapshot:

[root@localhost ~]# btrfs subvolume snapshot /home/admin2/ /.snapshots/s2
ERROR: Not a Btrfs subvolume: Invalid argument

How to create BTRFS snapshot for a directory?

2 Answers 2

2

No simple way is known to me. I would say no, it's not possible directly.

If admin2/ was a subvolume then there would be no problem. I don't think you can transparently and instantly convert a directory to a subvolume though.

There's a workaround:

  1. Create a snapshot of the entire subvolume that holds admin2/.

  2. Remove everything but admin2/* from the snapshot.

  3. Rearrange data in the snapshot:

    • move the content of admin2/* to the top-level directory of the snapshot;
    • remove leftovers (e.g. admin2/* directory, now empty) from the snapshot.
  4. Change ownership and mode of the top-level directory of the snapshot according to the original admin2/ directory.

This way you manually create a subvolume that is what you want: a snapshot of a directory.

If you think you'll want to repeat this process in the future then consider replacing the original directory with the subvolume. Move the directory out of the way (rename or remove it), then move the subvolume into its place. Note the subvolume becomes /home/admin2/ so you will most likely want to create yet another snapshot. But now creating a snapshot is easy, the command you tried should now work.


* Do not confuse admin2/ in the snapshot with the original directory. Where I denote admin2/ with asterisk I mean admin2/ in the snapshot.


Meta note: Frankly I almost duplicated this other answer. The question there is about converting a directory into a subvolume, something you may or may not want to do. Because of this one difference I didn't simply point you to the other answer; I decided to explain the procedure in context of your question. But the difference is mostly in the question. The only difference in the procedure is whether you eventually change few names or not. If you find my answer useful then consider upvoting the linked one.

1

Snapshots can only be taken for subvolumes. There is no direct way to convert a directory into a subvolume.

But if you only want a copy of the directory sharing the same data on disk, you can follow the BTRFS Wiki and use cp --reflink:

cp -ax --reflink=always src/. dest

This will create just a normal copy of your directory but instead of copying all the file data it will be shared (=reflinked) with the original directory. Basically this is the same as a snapshot and if you want to have the copy in a separate subvolume you can just create an empty subvolume and copy the directory into it.

Depending on your data this may be faster than @Kamils answer because deleting all other files may take while for a huge number of files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .