Backup/Clone a btrfs partition

Is it possible to backup a btrfs partition verbatim (including all the shared structures which save space by marking snapshot trees as COW) to another one? For instance, I'd like to backup my root partition which has couple of btrfs subvolumes (snapshots of the / itself).

The only way I can think is to use a block copying tool like dd but that is inefficient as my partition is only 20% full and I intend to back it up regularly. I already backup everything incrementally using rsync, so this is not a workaround for another problem. I simply need a bit for bit clone of my btrfs partition without having to use a tool such as dd.

I know ext3, for instance, provides a dump and restore utility. That is the kind of thing I'm looking for.

UPDATE

Here is a update to clarify the fact that I want to be able to access the files on the backup storage the same way I can on the live disk (ie I do not want to store dump files on the backup storage).


partclone is the tool you are looking for.

  • it supports a lot of linux file systems, including BTRFS
  • it even supports some other filesystems (like FAT or exFAT)
  • unlike dd it looks at the file systems allocation map (e.g.: FAT and exFAT's FAT tables, BTRFS extent maps, etc.) and clones only the used blocks.

Depending on your usage pattern:

  • use the -b (long: --dev-to-dev) if both source and backup target black device are visible on the same machine (basically if both disks are plugged into the same computer).
  • or use -c and -r (long: --clone and --restore) to first clone to partclone's own image format on the source machine, and then restore from the image on the backup machine
  • according to partclone manpage, it's possible to pipe images, so it should be possible to copy partitions over the network without saving actual files, if you're running short on space, but have an extremely reliable network.

A completely different way to handle the problem would be using btrfs send and btrfs receive mechanism.

  • This WON'T produce block device copies down to the sector
  • but, by correctly providing references on both ends, it is also able to keep the references between ref-linked (parts of) files.

So btrfs' own mechanism isn't a sector perfect copy (unlike partclone's) but it's an extent perfect copy (much better than rsynv -aSHAX which only handles hardlinked files).


Pipe the output of dd through something like bzip2 or your other favourite compression program that'll read from standard input. I've done this on a 500 GB disk that had a relatively small install and ended up with an 8 GB file.


Not tried yet, but btrfs-image seems to solve this problem:

https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs-image

Update: Do not use this as backup. Thank you for the comment.

I leave this open and will not delete it, so that others can learn from it too.