Jump to content

Last Time Machine backup


JSzinger

Recommended Posts

Josh Levitsky
This post was recognized by Josh Levitsky!

JSzinger was awarded the badge 'Helpful' and 5 points.

This is a custom field which gives the day/time of last full Time Machine backup.

This was originally an extension attribute for another product and I modified it to work as a FileWave custom field.

I'll paste the code here too so you can look without uploading it.

 

Jean

 

#!/bin/bash

 

 

# Check Time Machine Backups

# Written by Chad Nielsen

# Modified for FileWave by Jean Szinger 6/21/21

 

###################################[ VARIABLE DEFINITIONS ]##################################

 

dateBackup="No Time Machine Backups"

macOS=$(sw_vers | awk '/ProductVersion/{print substr($2,1,5)}' | tr -d ".")

pathPlistNew="/Library/Preferences/com.apple.TimeMachine.plist"

pathPlistOld="/private/var/db/.TimeMachine.Results.plist"

 

######################[ SCRIPT BEGINS - DO NOT MODIFY BELOW THIS LINE ]######################

 

 

function main {

# Check the OS to determine where to look.

if [ "$macOS" -ge "109" ]; then

# If Auto Backup is enabled, get the Time Machine last backup date.

checkAutoBackup=$(defaults read "$pathPlistNew" | awk '/AutoBackup/{print $3}' \

| tr -d ";")

if [ "$checkAutoBackup" = "1" ]; then

dateBackup=$(defaults read "$pathPlistNew" Destinations | \

sed -n '/SnapshotDates/,$p' | grep -e '[0-9]' | awk -F '"' '{print $2}' | sort \

| tail -n1 | cut -d" " -f1,2)

if [ "$dateBackup" = "" ]; then

dateBackup="Initial Backup Incomplete"

fi

fi

else

if [ -e "$pathPlistOld" ]; then

dateBackup=$(defaults read "$pathPlistOld" "BACKUP_COMPLETED_DATE")

if [ "$dateBackup" = "" ]; then

dateBackup="Initial Backup Incomplete"

fi

fi

fi

# Report the result to the server.

echo "$dateBackup"

exit 0

}

 

######################################[ FUNCTION CALLS ]#####################################

 

main

 

LastTimeMachineBackup.customfields

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 3 months later...
  • Moderators

If you format the output to match our expected date format, then you can both set the Custom Field as a date field and then use date specific criteria in an inventory query, e.g in the last x days, older than, etc.

To give an example, this is my equivalent of the same thing:

#!/bin/zsh

PlistBuddy=/usr/libexec/PlistBuddy

# New path since macOS 10.10
tm_plist="/Library/Preferences/com.apple.TimeMachine.plist"

# Old path
if [[ -e "/private/var/db/.TimeMachine.Results.plist" ]]
then
	tm_plist="/private/var/db/.TimeMachine.Results.plist"
fi

backup_enabled=$($PlistBuddy -c 'Print AutoBackup' "$tm_plist" 2>/dev/null)

if [[ "$backup_enabled" == "true" ]]
then
	last_date=$($PlistBuddy -c 'Print :Destinations:0:SnapshotDates:' "$tm_plist" 2>/dev/null | awk '{penultimate = last; last = $0} END {print penultimate}')

fi	

if [ ! -z $last_date ]
then
	date -j -f "    %a %b %d %H:%M:%S %Z %Y" "$last_date" "+%Y/%m/%d %H:%M"
fi

 

  • Like 1
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...