Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- GREEN_FG="\033[32m"
- RED_FG="\033[31m"
- NORMAL="\033[0m"
- TM_RESULTS="/private/var/db/.TimeMachine.Results"
- if [ ! -f "${TM_RESULTS}.plist" ]; then
- echo -e "${RED_FG}No successful backup (is it currently in progress?)${NORMAL}"
- exit
- fi
- # timestamp when backup was completed (in UTC +0000)
- backup_date=$(defaults read ${TM_RESULTS} BACKUP_COMPLETED_DATE 2>/dev/null)
- if [ $? -ne 0 ]; then
- echo -e "${RED_FG}No successful backup${NORMAL}"
- exit
- fi
- # convert time machine timestamp to epoch seconds
- # -j do not try to set the date
- # -f format string
- epoch_tm_completed=$(date -jf "%F %T %z" "$(echo $backup_date)" +%s)
- # convert now to epoch seconds
- epoch_now=$(date +%s)
- function duration_in_words() {
- # difference in seconds
- local seconds_ago=$(($1 - $2))
- # difference in minutes
- local minutes_ago=$((seconds_ago / 60))
- if [ $minutes_ago = 0 ]; then
- echo "less than a minute" && return
- fi
- if [ $minutes_ago = 1 ]; then
- echo "a minute" && return
- fi
- if [ $minutes_ago -lt 45 ]; then
- echo "$minutes_ago minutes" && return
- fi
- if [ $minutes_ago -lt 90 ]; then
- echo "about 1 hour" && return
- fi
- # difference in hours
- local hours_ago=$((minutes_ago / 60))
- if [ $minutes_ago -lt 1440 ]; then
- echo "about $hours_ago hours" && return
- fi
- if [ $minutes_ago -lt 2880 ]; then
- echo "1 day" && return
- fi
- # difference in days
- local days_ago=$((minutes_ago / 1440))
- if [ $minutes_ago -lt 43200 ]; then
- echo "$days_ago days" && return
- fi
- if [ $minutes_ago -lt 86400 ]; then
- echo "about 1 month" && return
- fi
- # difference in months
- local months_ago=$((minutes_ago / 43200))
- if [ $minutes_ago -lt 525960 ]; then
- echo "about $months_ago months" && return
- fi
- if [ $minutes_ago -lt 1051920 ]; then
- echo "about 1 year" && return
- fi
- # difference in years
- local years_ago=$((minutes_ago / 525960))
- echo "over $years_ago years"
- }
- # get 2 lines back and store them in the duration array
- duration=( $(grep 'com.apple.backupd' /var/log/system.log | \
- awk '
- # initialize buffer and flag when start text is found
- /Starting (manual|automatic) backup/ { a=""; f=0 }
- # accumulate in buffer while in-between start and end text
- /Starting (manual|automatic) backup/,/Backup completed successfully/ {
- if (a == "") a = $0
- }
- # store first line in secondary buffer when end text is found (to
- # cover the case where end text is never found after start text)
- !f && /Backup completed successfully/ { b=a; f=1; last=$0 }
- # print portion of correct buffer based on flag for first line
- # print portion of last line
- END { print substr(f ? a : b, 8, 8); print substr(last, 8, 8) }
- ') )
- # convert start of backup to epoch seconds
- epoch_tm_start=$(date -jf "%T" ${duration[0]} +%s)
- # convert end of backup to epoch seconds
- epoch_tm_end=$(date -jf "%T" ${duration[1]} +%s)
- echo -e "Successful backup${GREEN_FG}" \
- $(duration_in_words $epoch_now $epoch_tm_completed) \
- "ago${NORMAL} in${GREEN_FG}" \
- $(duration_in_words $epoch_tm_end $epoch_tm_start) \
- "${NORMAL}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement