Skip to content

Instantly share code, notes, and snippets.

@oPromessa
Created September 20, 2025 19:03
Show Gist options
  • Select an option

  • Save oPromessa/52b5930f63b2c72ea52ee715715a6c77 to your computer and use it in GitHub Desktop.

Select an option

Save oPromessa/52b5930f63b2c72ea52ee715715a6c77 to your computer and use it in GitHub Desktop.
macOS: Terminal: zsh function to show status of Time Machine: attempts to mount SMB Backup destinations and list the backups.
#------------------------------------------------------------------------------
# Report Time Machine status
function tminfo {
function mount_table() {
mount | awk '
{
# Find "on" and "(" positions
for(i=1;i<=NF;i++) {
if($i=="on") on_idx=i
if($i ~ /^\(/) paren_idx=i
}
# Device is field before "on"
device = $(on_idx-1)
# Mount point is everything between "on" and "("
mountpoint = ""
for(j=on_idx+1;j<paren_idx;j++) {
mountpoint = mountpoint $j
if(j < paren_idx-1) mountpoint = mountpoint " "
}
# Filesystem type is first word inside parentheses
fstype = $(paren_idx)
gsub(/^\(/,"",fstype)
gsub(/,.*$/,"",fstype)
print device "\t" mountpoint
}
' | mlr --hi --ho --t2c cat
}
# Function to mount a Time Machine URL and its sparsebundle
function mount_tm_url() {
local url="$1"
if [[ -z "$url" ]]; then
echo "Usage: mount_tm_url <url>"
return 1
fi
echo "Mounting $url ..."
open "$url"
if [[ $? -eq 0 ]]; then
echo "$url mounted successfully."
# Get the ComputerName
local computer_name
computer_name=$(scutil --get ComputerName)
# Find the mount point for this URL
local mount_point
clean_url="${url#smb:}"
mount_point=$(mount_table | grep -v ".timemachine" | grep "$clean_url" | awk -F"," '{print $2}' | head -1)
echo $mount_point
# Try to mount the sparsebundle if it exists
local sparsebundle_path="${mount_point}/${computer_name}.sparsebundle"
if [[ -d "$sparsebundle_path" ]]; then
echo "Mounting sparsebundle: $sparsebundle_path"
open "$sparsebundle_path"
sleep 3
else
echo "No sparsebundle found at $sparsebundle_path"
fi
else
echo "ERROR: Could not mount $url"
fi
}
# Function to mount all Time Machine backup destinations and list their backups
function mount_and_list_tm_backups() {
local destinations url_destinations url
# Get all URLs and Mount Points
url_destinations=("${(@f)$(tmutil destinationinfo | grep "URL" | awk -F': ' '{print $2}')}")
destinations=("${(@f)$(tmutil destinationinfo | grep "Mount Point" | awk -F': ' '{print $2}')}")
echo "---listbackups: start"
# Build a set of mounted URLs by comparing URLs and Mount Points
local mounted_urls=()
for dest in "${(@f)destinations}"; do
# Find the URL corresponding to this mount point
url=$(tmutil destinationinfo | awk -v mp="$dest" '
/^URL/ { url=$0; sub(/^URL[ \t]*:[ \t]*/, "", url) }
/^Mount Point/ {
mnt=$0; sub(/^Mount Point[ \t]*:[ \t]*/, "", mnt)
if (mnt == mp) { print url; exit }
}')
if [[ -n "$url" ]]; then
mounted_urls+=("$url")
fi
done
# Mount URLs that do not have a Mount Point
for url in "${(f@)url_destinations}"; do
local already_mounted=0
for murl in "${(f@)mounted_urls}"; do
if [[ "$url" == "$murl" ]]; then
already_mounted=1
break
fi
done
if [[ $already_mounted -eq 0 ]]; then
echo "------Mounting missing destination URL: $url"
mount_tm_url "$url"
fi
done
# Re-fetch destinations after attempting to mount
destinations=("${(@f)$(tmutil destinationinfo | grep "Mount Point" | awk -F': ' '{print $2}')}")
if [[ ${#destinations[@]} -eq 0 ]]; then
echo "------Still no Time Machine Mount Points found after mounting URLs."
return 1
fi
# For each destination, check if it's mounted and list backups
for dest in "${destinations[@]}"; do
echo "------Processing destination: $dest"
if [[ -d "$dest" && $(mount | grep -F -- "$dest") ]]; then
echo "$dest is a mount point."
else
echo "Attempting to mount $dest..."
open -g "$dest"
sleep 3
fi
if [[ -d "$dest" && $(mount | grep -F -- "$dest") ]]; then
echo "$dest is mounted. Listing backups..."
tmutil listbackups -d "$dest"
else
echo "ERROR: Could not mount $dest"
fi
done
echo "---listbackups: end"
}
echo ---destinationinfo
tmutil destinationinfo
echo ---currentphase
tmutil currentphase
echo ---status
tmutil status
mount_and_list_tm_backups
echo ---listlocalsnapshots
# Adjust as per the NAme of your Local Disk
tmutil listlocalsnapshots /Volumes/Macintosh\ HD
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment