Time Machine get last backup date extension attribute

agrotropic
New Contributor

Hello Community,

if anyone needs to show the last time machine backup date as an extension attribute, I wrote the following code which is working fine with OSX 12.3 and higher.

It also works when more then one backup volume is configured, it shows the most recent backup date among all backup volumes.

The response is in the format yyyy-mm-dd hh:mm:ss

______________________

#!/bin/sh

# time machine can have several backup destinations. We loop through all of them and get the most recent backup

# we use plutil, because PlistBuddy automatically makes date-format conversions that we don't want

# plutil option raw is available only with MacOS 12.3 onwards

# we check the correctness of the plist with PlistBuddy, because plutil throws an error and aborts the code when the plist is not well formated or the entry doe not exist

# scripted by valentin burgisser

success=false

entry="$(/usr/libexec/PlistBuddy -c "Print Destinations" /Library/Preferences/com.apple.TimeMachine.plist)"

if [[ ${entry} != "" ]]

then

destinationscount=$(plutil -extract Destinations raw /Library/Preferences/com.apple.TimeMachine.plist)

#loop through the destinations

for (( destination=0; destination <= $(($destinationscount-1)); ++destination ))

do

# get the number ob backups for each destination (size of array)

entry="$(/usr/libexec/PlistBuddy -c "Print Destinations:$destination:SnapshotDates" /Library/Preferences/com.apple.TimeMachine.plist)"

if [[ ${entry} != "" ]]

then

declare -i sizeofarray=$(plutil -extract Destinations.$destination.SnapshotDates raw /Library/Preferences/com.apple.TimeMachine.plist)

# get the last backup

backupdate=$(plutil -extract Destinations.$destination.SnapshotDates.$(($sizeofarray-1)) raw /Library/Preferences/com.apple.TimeMachine.plist)

if [ "$destination" -eq "0" ] #first destination

then

maxdate=$backupdate

success=true

else #all other destinations

if [ "$backupdate" \> "$maxdate" ]

then 

maxdate=$backupdate

fi

fi

fi

done

fi

 

if ($success)

then

backupday=$(echo $maxdate| cut -d'T' -f 1)

backuptime=$(echo $maxdate| cut -d'T' -f 2)

backuptime=$(echo $backuptime| cut -d'Z' -f 1)

response=($backupday" "$backuptime)

else

response="Backup not correctly configured"

fi

 

echo "<result>$response</result>"

3 REPLIES 3

GoingUndergroud
New Contributor III

hi, thanks for posting this it works as advertised.

i wonder if plutil can be pushed out to macs with macos < 12.3?

also the date/time value retrieved seems to be displayed in gmt in jamf Pro. is there a way to get it to retrive in local time?

many thanks.

Franky779
New Contributor III

How is this script executed?