Skip to content

Instantly share code, notes, and snippets.

@jonesiscoding
Last active June 20, 2024 19:47
Show Gist options
  • Select an option

  • Save jonesiscoding/dfb054ebc51999e247da7b9af8312659 to your computer and use it in GitHub Desktop.

Select an option

Save jonesiscoding/dfb054ebc51999e247da7b9af8312659 to your computer and use it in GitHub Desktop.
Last Time Machine Backup
#!/bin/bash
# @file get-time-machine-latest.sh
# @brief Last Time Machine Backup
# @category Operating system
# @type Date
# @revision
# @uploaded
if [ -f '/var/db/.AppleSetupDone' ]; then
backupDate=$(date -j -r /var/db/.AppleSetupDone +"%Y-%m-%d %H:%M:%S")
else
backupDate="2001-03-24 00:00:00"
fi
isBackupEnabled=$(/usr/bin/defaults read /Library/Preferences/com.apple.TimeMachine AutoBackup 2>&1 | grep -v "does not exist" | grep -v "defaults\[")
if [ "${isBackupEnabled}" -eq "1" ]; then
tryDate=$(tmutil latestbackup | awk -F "/" -F'/' '{print $NF}' | cut -d"." -f1)
if [ -n "${tryDate}" ] && [ "${tryDate}" != "Unable to locate machine directory for host" ] && [ "${tryDate}" != "Failed to mount backup destination, error: Error Domain=com" ]; then
backupDate=$(date -j -f "%Y-%m-%d-%H%M%S" "${tryDate}" +"%Y-%m-%d %H:%M:%S")
elif [ -f "/Library/Preferences/com.apple.TimeMachine.plist" ]; then
lastDate=$(defaults read /Library/Preferences/com.apple.TimeMachine.plist Destinations 2> /dev/null | awk '/SnapshotDates/,/;/' | tail -n 2 | head -n 1 | awk '{$1=$1};1' | sed 's/\"//' | sed 's/\"//')
if [ -n "$lastDate" ]; then
backupDate=$(date -j -f "%Y-%m-%d %H:%M:%S %z" "$lastDate" "+%Y-%m-%d %H:%M:%S")
fi
fi
fi
echo "<result>${backupDate}</result>"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment