#!/bin/bash # Check Time Machine Currency # by Jedda Wignall # http://jedda.me # Additional Code by: # Dan Barrett # http://yesdevnull.net # Modified for GFI RMM usage by: # Devin Campbell # Dayton Backup & IT # http://daytonbackup.com # v1.4 - 17 Nov 2014 # Updated for Yosemite. Old check for OS X version found 10.10 to # be less than 10.9 (mathematically accurate). The method of checking # Time Machine is the same as in Mavericks, only the version checking # has changed. # v1.3 - 14 Dec 2013 # Changed exit codes for GFI usage (errors should be over 1000) # Added /Library/Preferences/com.gfi.MaxRMM.TimeMachine.plist to store last # backup date so that the check doesn't fail whenever the Time Machine # location is disconnected. # v1.2 - 21 Nov 2013 # Updated with support for OS X Mavericks (10.9) # v1.1 - 3 May 2012 # Cleaned up the output to provide a last backed up date. Error checking for non-supplied flags. # v1.0 - 3 May 2012 # Initial release. # This script checks the Time Machine results file on a Mac, and reports if a backup has completed within a number of minutes of the current time. # Very useful if you are monitoring client production systems and want to ensure backups are occurring. # Arguments: # Critical threshold in minutes # Example: # ./check_time_machine_currency.sh 1440 # Supports: # * OS X 10.6 # * OS X 10.7 # * OS X 10.8 # * OS X 10.9 # * OS X 10.10 critMinutes=$1; if [ "$critMinutes" == "" ] then printf "ERROR - You must provide a critical threshold in minutes!\n" exit 1001 fi # Check to see if we're running Mavericks as Time Machine runs a little differently osVersion=`sw_vers -productVersion | grep -E -o "[0-9]+\.[0-9]+"` isMavericks=`echo $osVersion '== 10.9' | bc -l` isYosemite=`echo $osVersion '== 10.10' | bc -l` currentDate=`date +%s` if [ $isMavericks -eq 1 ] || [ $isYosemite -eq 1 ] then # 10.9+ Check if [ $isMavericks -eq 1 ] then printf "Running on Mavericks.\n" elif [ $isYosemite -eq 1 ] then printf "Running on Yosemite.\n" fi lastBackupDateString=`tmutil latestbackup | grep -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}"` if [ "$lastBackupDateString" == "" ] then printf "Looking for last backup in /Library/Preferences/com.gfi.MaxRMM.TimeMachine.\n" lastBackupDateString=`defaults read /Library/Preferences/com.gfi.MaxRMM.TimeMachine LastBackup` if [ "$lastBackupDateString" == "" ] then printf "CRITICAL - Time Machine has not completed a backup on this Mac!\n" exit 1002 fi fi lastBackupDate=`date -j -f "%Y-%m-%d-%H%M%S" $lastBackupDateString "+%s"` `defaults write /Library/Preferences/com.gfi.MaxRMM.TimeMachine LastBackup -string ''$lastBackupDateString''` else # < 10.9 Check printf "Running on pre-Mavericks.\n" lastBackupDateString=`defaults read /private/var/db/.TimeMachine.Results BACKUP_COMPLETED_DATE` if [ "$lastBackupDateString" == "" ] then printf "CRITICAL - Time Machine has not completed a backup on this Mac!\n" exit 1002 fi lastBackupDate=`date -j -f "%Y-%m-%e %H:%M:%S %z" "$lastBackupDateString" "+%s"` fi diff=$(($currentDate - $lastBackupDate)) critSeconds=$(($critMinutes * 60)) if [ "$diff" -gt "$critSeconds" ] then printf "CRITICAL - Time Machine has not backed up since `date -j -f %s $lastBackupDate` (more than $critMinutes minutes)!\n" exit 1003 fi if [ "$lastBackupDate" != "" ] then printf "OK - A Time Machine backup has been taken within the last $critMinutes minutes. (`date -j -f %s $lastBackupDate`)\n" exit 0 else printf "CRITICAL - Could not determine the last backup date for this Mac.\n" exit 1002 fi