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.
Sigh, seems I posted a bit too soon, with a bit of help of the almighty Zosma on IRC, we found out how this is supposed to work.
Subvolumes @
and @home
are children of the btrfs root, so /
is not actually the root of the file system. The UUID of the btrfs root is supplied in /etc/fstab
twice, providing the mounts for /
and /home
. Mounting the actual root of the file system shows the subvolumes created by the installer:
root@box:~# mkdir /mnt/btrfs
root@box:~# mount UUID=f8963df3-1320-4bc0-a125-62be185b029e /mnt/btrfs
root@box:~# ls /mnt/btrfs
@ @home
So that's where they went! Creating a new subvolume in the exact way the ubuntu installer did is now easy:
root@box:~# btrfs subvolume create /mnt/btrfs/@data
Create subvolume '/mnt/btrfs/@data'
Now, as we've created a subvolume in the btrfs root instead of in the root filesystem for the install, /data/
won't show up by itself. Luckily, it's trivial to copy the trick used by the installer by examining /etc/fstab
and add another mount for /data
:
UUID=f8963df3-1320-4bc0-a125-62be185b029e /data btrfs defaults,subvol=@data 0 2
Last step is to create the mount point: mkdir /data
. After a reboot, we'll have a /data
directory mounted from a separate btrfs subvolume.
… Profit!
Best Answer
It's just a convention. The kernel btrfs wiki's section on subvolumes doesn't mention
@
at all. This discussion on the debian-boot mailing list is informative:This is a convention that OpenSUSE and Debian apparently picked up from Ubuntu, and the idea of using all those subvolumes is attributed by Colin Watson to Roger Leigh. That was in 2010, and in 2011, we already have subvolumes named with
@
, but I can't find online discussions about this during that period. I suppose only Colin can answer this now.