From c0fb7efad6fbe575a45d806a292c72a04bb6798f Mon Sep 17 00:00:00 2001 From: Matteo Corti Date: Thu, 19 Oct 2023 13:02:38 +0200 Subject: [PATCH] Handle empty TM disks --- ChangeLog | 4 ++++ NEWS.md | 2 ++ RELEASE_NOTES.md | 3 +-- VERSION | 2 +- tmstatus.sh | 62 +++++++++++++++++++++++++++++++++--------------- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index bf707a4..4addbd1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2023-10-19 Matteo Corti + + * tmstatus.sh: Handle empty timne machine disks + 2023-08-16 Matteo Corti * tmstatus.sh: Align today's backups diff --git a/NEWS.md b/NEWS.md index 3843654..9c00207 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8d6ab29..2ec0cd3 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,2 +1 @@ - * Corrected the speed parsing on Sonoma - * Align today's backups +Handling of empty Time Machine disks (no backups yet) diff --git a/VERSION b/VERSION index 3989355..3500250 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.20.0 +1.21.0 diff --git a/tmstatus.sh b/tmstatus.sh index 83cdcd2..0aaf84c 100755 --- a/tmstatus.sh +++ b/tmstatus.sh @@ -12,7 +12,7 @@ # # shellcheck disable=SC2034 -VERSION=1.20.0 +VERSION=1.21.0 export LC_ALL=C @@ -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 @@ -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