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 hhss
______________________
#!/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>"