Copy BTRFS partition to external harddisk BTRFS partition including snapshots

btrfsfilesystems

I have a question about backuping BTRFS partitions.

Assume I have a BTRFS partition on /dev/sda1 and an external harddrive with BTRFS on /dev/sdb1.

I already found out I can make an initial backup by issuing:

btrfs replace start /dev/sda1 /dev/sdb1

Afterwards I change 2 things:

  • I create a new regular file on SDA1
  • I create a new BTRFS snapshot on SDA1

Now I want to bring my external harddrive SDB1 ('backup') aligned with SDA1. So both the files as the BTRFS specific stuff (snapshots).

How do I do this?

So I am looking for the rsync equivalent which also syncs the BTFS features (snapshots):

rsync -avr --delete <mount point sda1> <mount point sdb1>

Thanks

Best Answer

The replace command doesn't make a backup of sda1, it replaces sda1 with sdb1 in the filesystem, but since it's a one device filesystem and btrfs doesn't bother wiping the data from sda1 when it replaces it they end up being indentical copies of the filesystem. However you do NOT want to do this as both will have the same UUID, and currently it's not safe to mount two btrfs filesystems with the same UUID as it can cause MASSIVE DATA CORRUPTION (see btrfswiki's Gotchas page). If you want to use btrfs's incremental backup feature you should format you're backup drive /dev/sdb1 to a new btrfs filesystem. Then you should make a read-only snapshot of watever subvolume(s) you want to backup on your filesystem by using

btrfs su sn -r @subvolume-name @subvolume-name-RO

on each subvolume. Then you should mount the blank btrfs filesystem and run

btrfs send /path/to/@subvolume-name-RO | btrfs rec /path/to/backup-directory/

This will be the first send and btrfs will have to transfer all of the data this time. Next time you want to send a backup to this drive you can use incremental sends to only send what data has changed since the previous backup you sent. It will also use Copy On Write so you'll save a lot of space as well. Just make sure you keep the latest snapshot on both filesystems. When it's done you can rename the sent snapshot to whatever you want.

Now if you want to send another snapshot just rename the orignal one and take a new snapshot with something like

mv @subvolume-name-RO @subvolume-name-RO-old
btrfs su sn -r @subvolume-name @subvolume-name-RO

Then you can use send to send the latest snapshot using

btrfs send -p @subvolume-name-RO-old @subvolume-name-RO | btrfs rec /path/to/backup-directory/

and if the previous snapshot still exists on your backup drive it will send your new snapshot by only having to copy whatever changes you've made since the previous one.

Related Question