Create SuperDuper! backup reminders using Growl and cron

Mar 08, '10 07:30:00AM

Contributed by: Tom_H

I use a couple of shell scripts that use Growl to remind me to run SuperDuper backups. The first script (backupcompleted) is set to run after each SuperDuper backup. It writes a timestamp into an invisible file called .lastbackup in my home directory; here's the script:

#!/bin/bash
# This script is run by SuperDuper each time a backup is completed.
date "+%s" > ~/.lastbackup
The second script (lastbackup) reads the .lastbackup file and calculates the time elapsed. It takes one argument: the desired number of hours to wait before showing an alert.

If the elapsed time is greater than the time suppled to the script, it shows a Growl notification. If the elapsed time is greater than twice the time suppled to the script, it also increases the priority of the alert (so you can set a diferent colour for it in Growl preferences). Here's the script:
#!/bin/bash

USAGE="Usage: $0 [integer number of hours]"

if [[ ! "$#" == 1 ]]
then 
  echo $USAGE
  exit 1
fi

let "DISPLAYTHRESHOLD = $1 * 60 * 60" # Convert to seconds
LASTBACKUP=`cat ~/.lastbackup`
NOW=`date "+%s"`
let "ELAPSEDTIME = $NOW - $LASTBACKUP"

if (( $ELAPSEDTIME > $DISPLAYTHRESHOLD ))
then
  
  let "OMFG = $DISPLAYTHRESHOLD * 2"
  
  if (( $ELAPSEDTIME > $OMFG ))
  then
    PRIORITY="2"
  else
    PRIORITY="0"
  fi
  
  NICEDATE=`date -r $LASTBACKUP "+%e %B at %k:%M"` # Format date nicely
  MESSAGE="Your last full backup was completed on $NICEDATE."
  
  /usr/local/bin/growlnotify -s -n "Backup Check" -a SuperDuper\! -t "Last Backup" -m  "$MESSAGE" -p "$PRIORITY"
fi
Finally, to make the script run hourly add this line to your crontab:
0 * * * * /full/path/to/lastbackup 24 &>/dev/null
[robg adds: I haven't tested this one.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100303171421177