24

What are the commands to use in btrfs in case I want to:

  1. create a (read-only) snapshot of a volume;
  2. (do some experiments in the volume);
  3. and then rollback the volume to the snapshot?
imz -- Ivan Zakharyaschev
  • 15,113
  • 15
  • 61
  • 123
  • 1
    [Here are the commands for this scenario in LVM](http://unix.stackexchange.com/questions/18913/how-to-create-a-read-only-snapshot-in-lvm-and-then-rollback-to-it-after-some-wor/18914#18914). – imz -- Ivan Zakharyaschev Aug 22 '11 at 23:54

2 Answers2

21
# take a read-only snapshot:
btrfs sub snap -r fs snapshot

... do things on fs

# rolling back:
btrfs sub del fs # at which point you'll lose those things you've done
                 # if you want to preserve them, just rename fs instead

btrfs sub snap snapshot fs # reinstate snapshot as a read+write fs
btrfs sub del snapshot # delete the non-longer needed read-only snapshot
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • 3
    I like how you `snapshot` the snapshot over the filesystem, instead of attempting to move it. It's elegant in analogy to copying the snapshot, rather than attempting to use and alter it. You might mention that this sequence will end up deleting everything done in the "... do things" step. – Bladt Jun 24 '15 at 15:02
  • Here is what happened when I tried to restore the snapshot. I have a btrfs filesystem mounted at /mnt/sdc1, and I created a read-only snapshot named 20170917. `btrfs sub del .` > Delete subvolume (no-commit): '/mnt/sdc1' > ERROR: cannot delete '/mnt/sdc1': Invalid argument or btrfs sub snap 20170917/ . Create a snapshot of '20170917/' in './20170917' ERROR: cannot snapshot '20170917/': File exists – zkilnbqi Sep 17 '17 at 19:59
  • Sorry about the poor formatting in the above comment. Markdown for comments is braindead. – zkilnbqi Sep 17 '17 at 20:06
  • 1
    @Harold, sound like you can't delete the root volume that way. There may be ways to do it, but I have to admit it's been 5 years since I last used `btrfs` as I gave up on it as it had too many stability, performance and reliability issues. It may be more usable now, but the end of the story is that I'm not so familiar with it anymore (not to mentioned that it has probably evolved a lot as well since). – Stéphane Chazelas Sep 17 '17 at 20:13
  • 1
    Thanks. It seems to be the best choice on Linux right now for a modern filesystem, if I can figure out how to use it. – zkilnbqi Sep 17 '17 at 20:15
  • It should be noted that nowadays there is no need to create a snapshot of a snapshot to change its read-only state. You can just use `btrfs property set "$path_to_snapshot" ro true/false` to set it to either true or false which is probably faster. – Zorf Jan 13 '19 at 18:07
  • I don't get the "rename" part. Mind to create an example? – Ikem Krueger Apr 15 '22 at 22:40
  • Xbian says this upon executing first command: ERROR: Could not statfs: No such file or directory When i use / instead of fs, then it says "Create a readonly snapshot of '/' in './snapshot'" yet second command fails with "ERROR: Could not destroy subvolume/snapshot: Invalid argument" when used "/" or ends with "..no such file" when used "fs" (in second command). – 16851556 Aug 02 '23 at 06:52
5

Try from the other side: create R/W snapshot, use it as new main one, do "some experiments" on old one, then just drop contents of old.

Article "BTRFS Fun" can be helpful.

poige
  • 6,195
  • 2
  • 30
  • 57