Mac Laptop Backup Script

Stick this in your crontab to run every minute, like this:
* * * * * $HOME/bin/backup

Here's the script. Note that you need to have your "dropbox" set up running rsyncd in an appropriate way. And, of course, you should make provisions for the safe keeping of your "backed up" files.

#!/bin/sh

RHOST=dropbox
DOM=local.domain
PFX=/tmp/backup
LOG=$PFX.out
LOCK=$PFX.lock
LAST=$PFX.last
BEFORE=$PFX.cmp

# make sure we're on the right network
/sbin/ping -n -c 1 -o -q $RHOST.$DOM > /dev/null 2>&1 || exit 0

# make sure we didn't just do a backup
SIXAGO=`date -j -v-6H '+%Y%m%d%H%M.%S'`
touch -t $SIXAGO $BEFORE
test -f $LAST && test $LAST -nt $BEFORE && exit 0

# only one backup can run at a time
lockfile -l 86400 -r1 $LOCK 2> /dev/null || exit 0

HOST=`hostname -s | tr 'A-Z' 'a-z'`
RSYNC=/usr/bin/rsync
FLAGS="-v -r -l --copy-unsafe-links -p -o -g -t --delete-during --delete-excluded"

backdir() { # <srcpath> <dstpath> <excludes>
  xSRC=$1/
  xDST=${RHOST}::$2/
  xEXCL=$3
  xCMD="$RSYNC $FLAGS $xEXCL $xSRC $xDST"
  $xCMD >> $LOG 2>&1
  date >> $LOG
}

date > $LOG
backdir $HOME backedup/$USER/${HOST}_home '--exclude=.Trash/ --exclude=Caches/'

touch $LAST

rm -f $LOCK

##### EOF #####