1

I've set up a backup-server that creates daily BTRFS-snapshots of / in /backup/.

btrfs subvolume snapshot -r / /backup/$(date +%s)  # simplified example

I found this to be problematic because the snapshots get bloated by temporary volumes in /var/lib/docker. Now I'd like to create snapshots of /home only. But this does not work:

# btrfs subvolume snapshot -r /home /backup/$(date +%s)
ERROR: Not a Btrfs subvolume: Invalid argument

So I understand I need to make /home a subvolume. I'm unsure whether this means /home must be mounted separately. I've looked at other questions like How to make a btrfs snapshot? but they already have /home set up for this. How can I migrate this? What are the steps I need to take?

My current devices:

# blkid
/dev/sda1: UUID="39520d46-2e30-4b51-b093-2a3671eb8f52" TYPE="swap" PARTUUID="c9b515af-26c9-4809-aee1-680ca5ee7153"
/dev/sda2: UUID="3a994a01-9d92-4e67-8440-284ce24465f8" UUID_SUB="cbb73892-5f50-40f2-b411-37307bcbefc4" TYPE="btrfs" PARTUUID="462e3ffe-2c99-4481-89aa-b3f13f3985dc"
/dev/sdb: UUID="3a994a01-9d92-4e67-8440-284ce24465f8" UUID_SUB="b78ffe6c-c078-4cc5-b4c7-dd3874251394" TYPE="btrfs"
/dev/sda3: PARTUUID="63c9d4dd-0a85-40d6-9e10-3eaffcba4b49"

The / volume is mirrored on /dev/sda2 and /dev/sdb.

sba
  • 121
  • 5
  • Which one is your `/` partition? is it `/dev/sda2` or `/dev/sdb`? It is weird that they have the same UUID. – Pilot6 Dec 08 '20 at 12:45
  • Maybe the identical UUID is because `sdb` is used to mirror `sda2`. I don't know enough about BTRFS to say whether this makes sense. – sba Dec 08 '20 at 14:22
  • is this a RAID? It shouldn't make a problem I think. – Pilot6 Dec 08 '20 at 14:24
  • Yeah, I did `btrfs balance start -dconvert=raid1 -mconvert=raid1` on it. – sba Dec 08 '20 at 14:28
  • It is a bit weird that you have a partition on `sda` and don't have paritions on `sdb`. But it works this way. You can use either of `/dev/sda2` or `/dev/sdb` for my answer. – Pilot6 Dec 08 '20 at 14:30

2 Answers2

2

You can't make a snapshot of a directory, but only a subvolume.

But it is quite easy to convert /home directory to a separate snapshot.

Let's say your / is /dev/sda2.

Create a backup of your system first in case you make a mistake!

To convert your /home to a subvolume boot from a LiveUSB and do this:

sudo mount /dev/sda2 /mnt
sudo btrfs sub create /mnt/@
sudo btrfs sub create /mnt/@home
sudo mv /home /mnt/@home
sudo mv / /mnt/@

Now you moved your / to the @ subvolume and /home to @home subvolume.

Now you need to edit /mnt/etc/fstab and add mount options to mount the volumes properly. Add subvol=@ to the line where the / is mounted. And create a similar line for /home like this:

UUID=3a994a01-9d92-4e67-8440-284ce24465f8 /               btrfs subvol=@ 0       0
UUID=3a994a01-9d92-4e67-8440-284ce24465f8 /home           btrfs subvol=@home 0       0

The last thing is to chroot to the system and update grub:

sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys  /mnt/sys
sudo chroot /mnt
sudo update-grub
exit
sudo umount /mnt

If you made everything right and I didn't make a mistake, you should reboot to a working system.

Pilot6
  • 88,764
  • 91
  • 205
  • 313
  • Yes please how do I convert /home to a subvolume? – sba Dec 08 '20 at 12:23
  • Please add output of `sudo blkid` command to your question to make it easier to write. – Pilot6 Dec 08 '20 at 12:25
  • ok added to my question. – sba Dec 08 '20 at 12:43
  • Thanks for the details! I'm curious though, why do I need the `btrfs sub create /mnt/@`? Wouldn't it be enough to just create the extra `@home` subvolume? – sba Dec 08 '20 at 14:39
  • 1
    Well it may work this way. But I am not quite sure grub will take it correctly. Using `@` and `@home` is a standard practice. And if you mount the root of the disk, you'll see the `@home` as a directory. That will confuse things too. – Pilot6 Dec 08 '20 at 14:41
  • I ended up just reinstalling and doing `rmdir /home; btrfs subvol create /home;` because I fumbled something and didn't want to figure out how to recover :-) – sba Dec 08 '20 at 16:32
  • Using `/` in subvolume names is not good. It may confuse some software. If you reinstall the system from scratch @ and @home will be created by the installer. – Pilot6 Dec 08 '20 at 16:35
0
a correction:

  $ sudo mv /home /mnt/@home

should read

  $ sudo mv /home/* /mnt/@home

otherwise you end up with

  $ ls -r/home
  home
  $ ls -r /home/home
  user1 user2 user2 ..

which will prevent GDM login..
jeorge
  • 1
  • 1
  • 2
    This does not provide an answer to the question. Once you have sufficient [reputation](https://askubuntu.com/help/whats-reputation) you will be able to [comment on any post](https://askubuntu.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/1246672) – Pilot6 Aug 31 '22 at 08:52