Skip to content

Commit

Permalink
Handle empty TM disks
Browse files Browse the repository at this point in the history
  • Loading branch information
matteocorti committed Oct 19, 2023
1 parent efccb88 commit c0fb7ef
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2023-10-19 Matteo Corti <[email protected]>

* tmstatus.sh: Handle empty timne machine disks

2023-08-16 Matteo Corti <[email protected]>

* tmstatus.sh: Align today's backups
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* 2023-10-19, Version 1.21.0
* Handling of empty Time Machine disks (no backups yet)
* 2023-10-03, Version 1.20.0
* Corrected the speed parsing on Sonoma
* Align today's backups
Expand Down
3 changes: 1 addition & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* Corrected the speed parsing on Sonoma
* Align today's backups
Handling of empty Time Machine disks (no backups yet)
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.20.0
1.21.0
62 changes: 43 additions & 19 deletions tmstatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#

# shellcheck disable=SC2034
VERSION=1.20.0
VERSION=1.21.0

export LC_ALL=C

Expand Down Expand Up @@ -243,8 +243,20 @@ EOF

elif echo "${LISTBACKUPS}" | grep -q 'No backups found for host.'; then

printf 'Time Machine: no backups found\n'
tm_total=$(df -H "${tm_mount_point}" 2>/dev/null | tail -n 1 | awk '{ print $2 "\t" }' | sed 's/[[:blank:]]//g')
tm_available=$(df -H "${tm_mount_point}" 2>/dev/null | tail -n 1 | awk '{ print $4 "\t" }' | sed 's/[[:blank:]]//g')

tm_total_raw=$(df "${tm_mount_point}" 2>/dev/null | tail -n 1 | awk '{ print $2 "\t" }' | sed 's/[[:blank:]]//g')
tm_available_raw=$(df "${tm_mount_point}" 2>/dev/null | tail -n 1 | awk '{ print $4 "\t" }' | sed 's/[[:blank:]]//g')
tm_percent_available=$(echo "${tm_available_raw} * 100 / ${tm_total_raw}" | bc)

printf '\nVolume (%s) "%s": %s (%s available, %s%%)\n' "${KIND}" "${tm_mount_point}" "${tm_total}" "${tm_available}" "${tm_percent_available}"

# no backups yet
printf ' Oldest:\t-\n'
printf ' Last:\t\t-\n'
printf ' Number:\t0\n'

else

# sometimes df on network volumes throws a 'df: getattrlist failed: Permission denied' error but delivers
Expand All @@ -259,25 +271,37 @@ EOF
printf '\nVolume (%s) "%s": %s (%s available, %s%%)\n' "${KIND}" "${tm_mount_point}" "${tm_total}" "${tm_available}" "${tm_percent_available}"

DATE="$(echo "${LISTBACKUPS}" | head -n 1 | sed 's/.*\///' | sed 's/[.].*//')"
days="$(days_since "${DATE}")"
backup_date=$(echo "${LISTBACKUPS}" | head -n 1 | sed 's/.*\///' | sed 's/[.].*//' | sed 's/-\([^\-]*\)$/\ \1/' | sed 's/\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\1:\2:\3/')
DAYS_AGO="$(format_days_ago "${days}")"
printf ' Oldest:\t%s (%s)\n' "${backup_date}" "${DAYS_AGO}"

LATESTBACKUP="$(tmutil latestbackup -d "${tm_mount_point}")"
if echo "${LATESTBACKUP}" | grep -q '[0-9]'; then
# a date was returned (should implement a better test)
DATE="$(echo "${LATESTBACKUP}" | sed 's/.*\///' | sed 's/[.].*//')"
days=$(days_since "${DATE}")
backup_date=$(echo "${LATESTBACKUP}" | sed 's/.*\///' | sed 's/[.].*//' | sed 's/-\([^\-]*\)$/\ \1/' | sed 's/\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\1:\2:\3/')
DAYS_AGO="$(format_days_ago "${days}")"
printf ' Last:\t\t%s (%s)\n' "${backup_date}" "${DAYS_AGO}"

if echo "${DATE}" | grep -q 'No machine directory' ; then

# no backups yet
printf ' Oldest:\t-\n'
printf ' Last:\t\t-\n'
printf ' Number:\t0\n'

else
printf ' Last:\t\t%s\n' "${LATESTBACKUP}"
fi

days="$(days_since "${DATE}")"
backup_date=$(echo "${LISTBACKUPS}" | head -n 1 | sed 's/.*\///' | sed 's/[.].*//' | sed 's/-\([^\-]*\)$/\ \1/' | sed 's/\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\1:\2:\3/')
DAYS_AGO="$(format_days_ago "${days}")"
printf ' Oldest:\t%s (%s)\n' "${backup_date}" "${DAYS_AGO}"

LATESTBACKUP="$(tmutil latestbackup -d "${tm_mount_point}")"
if echo "${LATESTBACKUP}" | grep -q '[0-9]'; then
# a date was returned (should implement a better test)
DATE="$(echo "${LATESTBACKUP}" | sed 's/.*\///' | sed 's/[.].*//')"
days=$(days_since "${DATE}")
backup_date=$(echo "${LATESTBACKUP}" | sed 's/.*\///' | sed 's/[.].*//' | sed 's/-\([^\-]*\)$/\ \1/' | sed 's/\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\1:\2:\3/')
DAYS_AGO="$(format_days_ago "${days}")"
printf ' Last:\t\t%s (%s)\n' "${backup_date}" "${DAYS_AGO}"
else
printf ' Last:\t\t%s\n' "${LATESTBACKUP}"
fi

number=$(echo "${LISTBACKUPS}" | wc -l | sed 's/\ //g')
printf ' Number:\t%s\n' "${number}"
number=$(echo "${LISTBACKUPS}" | wc -l | sed 's/\ //g')
printf ' Number:\t%s\n' "${number}"

fi

# shellcheck disable=SC2030
FOUND=1
Expand Down

0 comments on commit c0fb7ef

Please sign in to comment.