A (very) simple backup / history script for BTRFS

My /home partition is a BTRFS volume. Since it allows for very easy snapshotting, I decided to use it for providing a very simple history functionality. Note that this is not a REAL backup, because it doesn't protect against disk or machine failures. However it provides easy access to earlier versions of the file system. Here's how it works:

  • Run it on a hourly basis
  • Creates a separate snapshot for every hour the machine is running
  • Creates a snapshot for every day of the month
  • Creates a snapshot for every month of the year

The script is really very simple:



TS_MONTH=`date '+m-%m'`
TS_DAY=`date '+d-%d'`
TS_HOUR=`date '+h-%H'`

backup() {
    #echo "Backing up $VOLUME into $BASEDIR/$TIMESTAMP"
    if [ -d "$BASEDIR/$TIMESTAMP" ]; then btrfs subvolume delete "$BASEDIR/$TIMESTAMP" > /dev/null; fi
    /usr/bin/btrfs subvolume snapshot -r "$VOLUME" "$BASEDIR/$TIMESTAMP" > /dev/null

if [ "$1" = '' -o "$1" = 'm' ]; then backup "$TS_MONTH"; fi
if [ "$1" = '' -o "$1" = 'd' ]; then backup "$TS_DAY"  ; fi
if [ "$1" = '' -o "$1" = 'h' ]; then backup "$TS_HOUR" ; fi