Auto backup on mount

Created: — modified: — tags: bash

Automatically backup files on media mount, using usbmount

Assuming you're using WM, not DE, you probably have installed usbmount package in order to automatically mount USB sticks and HDDs when they are inserted.

It has nice "autorun" feature which we can use to automatically backup onto some removable harddisks when they are inserted. Assuming you're familiar with my rsync backups script (if not, you always can write your own or use plain rsync).

Simply put this script into /etc/usbmount/mount.d/:

#!/bin/sh

ROOT=$UM_MOUNTPOINT/backups.auto
test -d $ROOT || exit 0

export ALLOW_FIRST_RUN=1
export CLEAN_DIRS=$ROOT/*
export IGNORE_23=1

/root/backup-script.sh /home/ $ROOT/`hostname`

sync

Now, if inserted drive has backups.auto dir with a subdir matching your machine's hostname, it will be used to keep incremental backups of your home dirs. Remember to remove ALLOW_FIRST_RUN=1 when it's not needed anymore.

Few notes regarding speed optimisation: