Advertisement
Guest User

Untitled

a guest
Mar 13th, 2013
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. GREEN_FG="\033[32m"
  4. RED_FG="\033[31m"
  5. NORMAL="\033[0m"
  6.  
  7. TM_RESULTS="/private/var/db/.TimeMachine.Results"
  8.  
  9. if [ ! -f "${TM_RESULTS}.plist" ]; then
  10.     echo -e "${RED_FG}No successful backup (is it currently in progress?)${NORMAL}"
  11.     exit
  12. fi
  13. # timestamp when backup was completed (in UTC +0000)
  14. backup_date=$(defaults read ${TM_RESULTS} BACKUP_COMPLETED_DATE 2>/dev/null)
  15. if [ $? -ne 0 ]; then
  16.     echo -e "${RED_FG}No successful backup${NORMAL}"
  17.     exit
  18. fi
  19. # convert time machine timestamp to epoch seconds
  20. #   -j do not try to set the date
  21. #   -f format string
  22. epoch_tm_completed=$(date -jf "%F %T %z" "$(echo $backup_date)" +%s)
  23. # convert now to epoch seconds
  24. epoch_now=$(date +%s)
  25.  
  26. function duration_in_words() {
  27.     # difference in seconds
  28.     local seconds_ago=$(($1 - $2))
  29.     # difference in minutes
  30.     local minutes_ago=$((seconds_ago / 60))
  31.     if [ $minutes_ago = 0 ]; then
  32.         echo "less than a minute" && return
  33.     fi
  34.     if [ $minutes_ago = 1 ]; then
  35.         echo "a minute" && return
  36.     fi
  37.     if [ $minutes_ago -lt 45 ]; then
  38.         echo "$minutes_ago minutes" && return
  39.     fi
  40.     if [ $minutes_ago -lt 90 ]; then
  41.         echo "about 1 hour" && return
  42.     fi
  43.     # difference in hours
  44.     local hours_ago=$((minutes_ago / 60))
  45.     if [ $minutes_ago -lt 1440 ]; then
  46.         echo "about $hours_ago hours" && return
  47.     fi
  48.     if [ $minutes_ago -lt 2880 ]; then
  49.         echo "1 day" && return
  50.     fi
  51.     # difference in days
  52.     local days_ago=$((minutes_ago / 1440))
  53.     if [ $minutes_ago -lt 43200 ]; then
  54.         echo "$days_ago days" && return
  55.     fi
  56.     if [ $minutes_ago -lt 86400 ]; then
  57.         echo "about 1 month" && return
  58.     fi
  59.     # difference in months
  60.     local months_ago=$((minutes_ago / 43200))
  61.     if [ $minutes_ago -lt 525960 ]; then
  62.         echo "about $months_ago months" && return
  63.     fi
  64.     if [ $minutes_ago -lt 1051920 ]; then
  65.         echo "about 1 year" && return
  66.     fi
  67.     # difference in years
  68.     local years_ago=$((minutes_ago / 525960))
  69.     echo "over $years_ago years"
  70. }
  71.  
  72. # get 2 lines back and store them in the duration array
  73. duration=( $(grep 'com.apple.backupd' /var/log/system.log | \
  74.     awk '
  75.                # initialize buffer and flag when start text is found
  76. /Starting (manual|automatic) backup/   { a=""; f=0 }
  77.                # accumulate in buffer while in-between start and end text
  78. /Starting (manual|automatic) backup/,/Backup completed successfully/   {
  79.    if (a == "") a = $0
  80. }
  81.                # store first line in secondary buffer when end text is found (to
  82.                # cover the case where end text is never found after start text)
  83. !f && /Backup completed successfully/  { b=a; f=1; last=$0 }
  84.                # print portion of correct buffer based on flag for first line
  85.                # print portion of last line
  86. END { print substr(f ? a : b, 8, 8); print substr(last, 8, 8) }
  87. ') )
  88.  
  89. # convert start of backup to epoch seconds
  90. epoch_tm_start=$(date -jf "%T" ${duration[0]} +%s)
  91. # convert end of backup to epoch seconds
  92. epoch_tm_end=$(date -jf "%T" ${duration[1]} +%s)
  93.  
  94. echo -e "Successful backup${GREEN_FG}" \
  95.     $(duration_in_words $epoch_now $epoch_tm_completed) \
  96.     "ago${NORMAL} in${GREEN_FG}" \
  97.     $(duration_in_words $epoch_tm_end $epoch_tm_start) \
  98.     "${NORMAL}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement