14

Last Time Machine Backup

Posted in System by mosius 677 days ago

I know there are a lot of geeklets out there to show you the time machine log, but it's a bit more info than I want. I try to keep what's on the desktop to the absolute minimum.

This one just displays the last time a backup was completed successfully (DAY H:M:S). It's easily customizable if you can read shell programming and fairly well commented.

#! /bin/sh

# Your computer's timezone offset
OFFSET=`date "+%z" | cut -c 1-3`

# All the following is in ZULU (GMT) time
YEAR=`grep -m 1 "" /private/var/db/.TimeMachine.Results.plist | cut -c 8-11`
MONTH=`grep -m 1 "" /private/var/db/.TimeMachine.Results.plist | cut -c 13-14`
DAY=`grep -m 1 "" /private/var/db/.TimeMachine.Results.plist | cut -c 16-17`
ZULU_HOUR=`grep -m 1 "" /private/var/db/.TimeMachine.Results.plist | cut -c 19-20`
MINUTE=`grep -m 1 "" /private/var/db/.TimeMachine.Results.plist | cut -c 22-23`
SECOND=`grep -m 1 "" /private/var/db/.TimeMachine.Results.plist | cut -c 25-26`

# Corrects for your computer's timezone
HOUR=`expr $ZULU_HOUR + $OFFSET`

# If the TZ shift rolled you back a day you have to adjust the DAY
# If the Day rolls to 0, I didn't bother with the roll back because of the huge issues
# of which month you roll back on and what not. The proper solution would be to do proper
# date math, but I haven't figured out how to do that in shell unless you're using the
# current system time. But here, I'm using a generated time.
if [ $HOUR -lt 0 ] ; then 
    HOUR=`expr '24' + $HOUR`
    DAY=`expr $DAY - '1'`
fi

# Just for formatting. It adds the preceding zero to the number if it's a single digit.
if [ $HOUR -lt 10 ] ; then
    HOUR=`echo 0$HOUR`
fi
#if [ $DAY -lt 10 ] ; then
#   DAY=`echo 0$DAY`
#fi

echo "$DAY $HOUR:$MINUTE"

Comments

User Avatar
dereks 669 days ago
To get it to work on Mac OS X 10.6 I had to modify it like so:

# All the following is in ZULU (GMT) time

YEAR=`grep "" /private/var/db/.TimeMachine.Results.plist | cut -c 8-11 | head -n 1`

MONTH=`grep "" /private/var/db/.TimeMachine.Results.plist | cut -c 13-14 | head -n 1`

DAY=`grep "" /private/var/db/.TimeMachine.Results.plist | cut -c 16-17 | head -n 1`

ZULU_HOUR=`grep "" /private/var/db/.TimeMachine.Results.plist | cut -c 19-20 | head -n 1`

MINUTE=`grep "" /private/var/db/.TimeMachine.Results.plist | cut -c 22-23 | head -n 1`

SECOND=`grep "" /private/var/db/.TimeMachine.Results.plist | cut -c 25-26 | head -n 1`

The "-m 1" is not valid in Mac OSX, so to get around that you use the head -n 1 command.

Also had to make it search in the key, because there are a lot of different keys in that fire
User Avatar
keisans 603 days ago
I'm having major issues getting this to work. The date line in the .plist file isn't until the 6th line, so I'm just getting random characters from the XML heading. I admit I'm not very good with regular expressions, could someone enlighten me as to how to make this work?
User Avatar
PhillyMJS 559 days ago
This seems overly complicated to me. I've got a simple one-liner which pulls the timestamp out and displays it as-is, in YYYY-MM-DD HH:MM:SS format:

printf "Last TM Backup: " && defaults read /private/var/db/.TimeMachine.Results BACKUP_COMPLETED_DATE | awk '{print $1 " " $2}'

Works in Leopard and Snow Leopard, and returns the local time.

Log in to comment or register here.