How to make a btrfs snapshot?

What is the correct command to create a snapshot of my home partition on Ubuntu 12.04?

Given the fact that home is on a btrfs partition and it is mounted as /home, the correct command is:

sudo btrfs subvolume snapshot /home/ /home/<any_name_you_choose_for_the_snapshot>

Here are my results:

Before running the command today:

$ btrfs subvolume list /home  
ID 256 top level 5 path @home  
ID 257 top level 5 path @home/@snapshot_20120521_1936

The command:

$ sudo btrfs subvolume snapshot /home/ /home/@snapshot_20120611_1307  
Create a snapshot of '/home/' in '/home/@snapshot_20120611_1307'

After running the command today:

$ btrfs subvolume list /home  
ID 256 top level 5 path @home  
ID 257 top level 5 path @home/@snapshot_20120521_1936  
ID 258 top level 5 path @home/@snapshot_20120611_1307

The only issue with this method is that snapshots show up when you ls (list) /home.

The Ubuntu community Wiki has been updated with a solution for this. Here it is:

How to work with snaphots in Ubuntu's layout

In order to work with snapshots of / or /home in the Ubuntu layout it is very convenient to mount the btrfs filesystem at a separate location, and work from the top of the btrfs tree, rather than from the mounted subvolumes. <-- that is the solution. Unfortunately, it is not convenient.

sudo mount /dev/sdX# /mnt

To create a snapshot use the same syntax I used above:

sudo btrfs subvolume snapshot /mnt/@ /mnt/@_snapshot

This will create a snapshot of the @ subvolume named @_snapshot located also in the top of the btrfs tree. Since it is in the top of the tree, it will not show up when listing files in a mounted volume.

To roll back to a snapshot, you simply need to change its name to the name that ubuntu mounts and reboot. Here's how to change the name:

sudo mv /mnt/@ /mnt/@_badroot
sudo mv /mnt/@_snapshot /mnt/@

To delete a snapshot use:

sudo btrfs subvolume delete /mnt/@_badroot

btrfs snapshots are subvolumes in themselves, and self-contained, deleting the old @ subvolume like this is fine, provided we have a replacement.

NOTE: The btrfs-tools command set-default will break Ubuntu's layout.


Ok first things first:

  • The name of the subvolume you are looking at is @home as shown by btrfs subvolume list /home. It's mounted in /home. @home is the name of the subvolume also it has the ID 256 so most likely only your /home is formated as btrfs.

  • Now to create a snapshot of @home you have to issue: sudo btrfs subvolume snapshot /home/ /home/@home_snapshot_20120421

  • The subvolume can have any name. The @ is Ubuntu's convention at install time. (If you use apt-btrfs-snapshot the root subvolume has to be named @ btw.) The community wiki strongly recommends keeping @ as root and @home as home since it is mounted that way. This is especially important if you want to roll back: You should NOT use btrfs subvolume set-default (not syntax highlited by purpose).

  • For btrfs device scan you have to use sudo.

  • To delete a subvolume you can use sudo btrfs subvolume delete @home_snapshot_20120421

All btrfs commands can be abbreviated as long as they are unambiguous, so for example sudo btrfs device scan can be truncated to sudo btrfs d s, sudo btrfs dev sc or anything in between.

I hope that answered your questions. There are some tools out there to take regular backups, btrfs-snapshot-rotation is one example. Use those with caution since none of them are really mature or gained enough traction, i.e. they may very well contain bugs.


Install the apt-btrfs-snapshot package, and use its subcommands list, snapshot, delete and set-default.

This is a script that wraps the lower level btrfs commands. It works by first mounting the btrfs root filesystem somewhere. By that I mean the real root, as opposed to the /@ subvolume that is normally mounted as /. In other words, you need to run mount /dev/sda1 /mnt so you can view the subvolumes that will be listed as /mnt/@, /mnt/@home, and any others you have created. From there you can btrfs subvolume snapshot /mnt/@home @snapshot-today, which will create a snapshot of /@home named /@snapshot-today. Compare this with running btrfs subvolume snapshot /home /home/@snapshot_today, which instead creates the snapshot as a child of /@home, hence it shows up when you ls /home and its real name is /@home/@snapshot-today.

Rolling back a snapshot is just a simple rename operation: mv /mnt/@home @backup ; mv /mnt/@snapshot-today @home. The next time you boot, when it goes to mount /@home in /home, it will find the snapshot.

It's a bit tricky to wrap your head around because you have to keep in mind the difference between how btrfs sees things vs how the kernel sees things, which is influenced by the mount options ( the subvol= argument specifically ).

Tags:

Backup

Btrfs