Tuesday 14 May 2013

Backup btrfs with an external HDD



Ever since my old HDD started dying and filling up with bad sectors I have been thinking about a solid backup solution for my system. 

I have;
  • 1 internal HDD (btrfs)
  • 1 external HDD (btrfs)
  • Arch Linux

The Problem


How can we fully backup our system?

Discussion


There are 2 types of backup.

One type is where the data is protected from failure of the primary medium. This is where the data is typically backed up to another storage medium like an external drive. Examples include the disk dying or being stolen.

The other type is protection against accidental deletion and corruption of data. Think Apple 'timemachine' or a snapshot of your data at a particular time.

They are different. Let's explain the 'timemachine' concept with one example; you may have a corrupted file and your backup program also backed up the corrupted file, so you cannot recover the file from your normal backup. Instead, if you have a snapshot of how your data looked at a certain time you could restore the file from an earlier time where maybe it wasn't corrupted.

BTRFS


Introducing btrfs. A relatively unstable and heavily in-development File System.

One feature over other File Systems (FS) is that this FS allows for snapshots-in-time, effectively letting you restore files as they were when you made the snapshot. The great thing about it is that it doesn't actually take up much space because it just records the changes made to files.

The Solution


Let's create snapshots and backup our data in full. This should give us a full backup solution covering both types of backup. We are protected if data is corrupted or destroyed and we are also protected in case of unintentional modifications or deletions.


We can create a script of which there are 3 components;

  • a local snapshot
  • an rsync backup
  • a remote snapshot

fstab


# backup drive - external USB-3
UUID=5ab08618-1e9d-4faf-a719-bafd19875f77  /mnt/buffalo  btrfs  defaults,noauto  0  0


The local snapshot

A snapshot on our system so we can restore files that were accidentally deleted or corrupt or whatever...

Rsync backup

Copy over all the files on our disk to the external backup drive in case the main drive fails or otherwise blows up.

Remote snapshot

Lets put snapshots on the backup drive as well.. this way if our main drive fails we will also have snapshots still when we recover the data.

It's important to note that remote snapshots only work if the remote backup is on a sub-volume. Because a snapshot can only be taken on a sub-volume.

The Code


The following script can be used as a backup solution which includes snapshots and redundancy.

#!/bin/sh

echo "Starting Backups"

DATE=`date +%F`

echo "$DATE Creating local snapshot.."

# btrfs subvolume snapshot [-r]  [/]
if [ ! -d /snapshots/$DATE ]
then 
 btrfs subvolume snapshot -r / /snapshots/$DATE
 echo "$DATE Done."
else
 echo "$DATE /snapshots/$DATE already exists."
fi

echo "$DATE Creating backup snapshot.."
if [ ! -d /snapshots/$DATE ]
then 
 btrfs subvolume snapshot -r /mnt/buffalo/arch_btrfs/ /mnt/buffalo/arch_btrfs/snapshots/$DATE
 echo "$DATE Done."
else
 echo "$DATE /mnt/buffalo/arch_btrfs/snapshots/$DATE already exists."
fi


echo "$DATE Backing up to external HDD.."

# Backup files on ext4 external disk
rsync -aAXv / /mnt/buffalo/arch_btrfs/ --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/snapshots,/home/*/.gvfs} &> /var/log/rsync.log

echo "$DATE Done. See /var/log/rsync.log for more info."