7

I have a laptop with an internal drive using Btrfs. There are several subvolumes and snapshots on here. I want a copy of this drive to be made periodically to an external USB disk of approximately the same size. The external drive must be bootable, and near enough identical to the internal one, so that if the main drive fails I can just swap it in.

What is the best way to achieve this?

Below, some methods I have considered:

I considered using Btrfs RAID 1, but this is really designed for a permanently connected drive, so I suspect it would not work well.

When I had the same disk and used Ext4 on both, I managed this setup with Rysnc, which worked well. I expect this would not work though now, because rsync would not understand the snapshots, and would copy everything many times.

Maybe using Btrfs send / receive could be made to work, but it is not so simple, because to send a filesystem, a read only snapshot must first be made, and then the name of this snapshot is used on the external disk. I don't think there is a way to receive the root filesystem, at /

2 Answers 2

4

Maybe using Btrfs send / receive could be made to work, but it is not so simple, because to send a filesystem, a read only snapshot must first be made, and then the name of this snapshot is used on the external disk. I don't think there is a way to receive the root filesystem, at /

This is the best solution. I use snapshots to make fast incremental backups of my servers. You can back up the root subvolume just like any other, but I don't think you can receive at the root. More to the point, you shouldn't do so, because that prevents you from enjoying one of the benefits of snapshotting: incremental backups. Done properly, it will only send the data that has changed and consume only the disk space for the changed data.

This script runs as a cron job and takes a daily snapshot of my root partition, then uses btrfs send to send an incremental copy to my backup partition. The script as written uses pv, but if for some reason you don't want to install it, you can simply remove pv from the middle of the pipes.

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
date=$(date +%Y-%m-%d)

# the path to the partition mount point that we are backing up
source_partition=/

# where backup snapshots will be stored on the local partition
# this is needed for incremental backups
source_snapshot_dir=/snapshots

# where backups will be stored on the backup drive
target_snapshot_dir=/mnt/media/backups/root

if [ ! -d $source_snapshot_dir ]; then
    echo 'Creating initial snapshot...'
    mkdir --parents $source_snapshot_dir $target_snapshot_dir

    # create a read-only snapshot on the local disk
    btrfs subvolume snapshot -r $source_partition $source_snapshot_dir/$date

    # clone the snapshot as a new subvolume on the backup drive
    # you could also pipe this through ssh to back up to a remote machine
    btrfs send $source_snapshot_dir/$date | pv | \
        btrfs receive $target_snapshot_dir
elif [ ! -d $source_snapshot_dir/$date ]; then
    echo 'Creating root volume snapshot...'

    # create a read-only snapshot on the local disk
    btrfs subvolume snapshot -r $source_partition $source_snapshot_dir/$date

    # get the most recent snapshot
    previous=$(ls --directory $source_snapshot_dir/* | tail -n 1)

    # send (and store) only the changes since the last snapshot
    btrfs send -p $previous $source_snapshot_dir/$date | pv | \
        btrfs receive $target_snapshot_dir
fi

echo 'Cleaning up...'

# keep the 3 most recent snapshots on the source partition
ls --directory $source_snapshot_dir/* | \
    head --lines=-3 | \
    xargs --no-run-if-empty --verbose \
    btrfs subvolume delete --commit-after

# keep the 28 most recent snapshots on the backup partition
ls --directory $target_snapshot_dir/* | \
    head --lines=-28 | \
    xargs --no-run-if-empty --verbose \
    btrfs subvolume delete --commit-after

(Note: I've adapted the script somewhat to make it into a general solution, and haven't tested it as written. Please feel free to submit revisions if necessary.)

-1

As I understand it you're looking to create a mirror (or close enough to one) of your laptop hard drive to replace the laptop hard drive in the event of a disk failure. The mirror drive is to be connected via USB and may not always be connected, for instance you take your laptop on a short trip but leave the mirror drive at work or home.

To me this looks like a case for RAID 1. This can be done with either BTRFS or MDADM. In either case when connecting the external drive back to the laptop, you'll need to ensure it is seen as the "slave." Since more recent writes would have likely occurred on the laptop, this should be the case. In btrfs one could then run a scrub operation (btrfs-scrub).

Alternatively you could use fssync, though granted you would be ssh'ing into the localhost. fssync is reported from the man page to work well with btrfs.

Regarding snapshots and using rsync or other such synchronization, you could when creating snapshots, create one on both the external mirror and the internal drive, and just not sync the snapshots assuming read-only snapshots. If not read-only, you would need to sync and would potentially have the issue you stated.

If you have another system, you might consider using DRBD in single-primary mode. Though you may be able to get this to work with both drives on a single system, I've seen reports that indicate this does not work.

Note that RAID, synchronization, and replication is considered by many not a suitable substitute for a backup system.

1
  • "To me this looks like a case for RAID 1." It is not. RAID is for when all drives are always connected except in case of failure. "Note that RAID, synchronization, and replication is considered by many not a suitable substitute for a backup system." This is correct. RAID is not a backup. It protects you against certain types of damage, but don't let its convenience lull you into a false sense of security.
    – Mikkel
    Jul 10, 2016 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .