Hi Automators!
I use a portable SSD exclusively for manual Time Machine backups and want it to eject automatically once the backup finishes. I’ve written the following AppleScript to:
- Check if the drive is mounted.
- Start a Time Machine backup (if not already running).
- Unmount/eject the SSD after the backup is complete.
I’d appreciate your feedback to ensure the script is correct and reliable. Are there any edge cases I might have missed or improvements you’d suggest? Is there a utility that achieves something simlar, is there more reliable way to do this?
The main issue that the script solves for me is forgetting to unmount/ejecting that portable ssd after the backup finishes.
I want to do backups manually through this apple script only, For my case, I don’t want automatic/schedule based backups.
Here’s the script:
-- Define the Time Machine volume name
set timeMachineVolume to "PortableSSD"
-- Initialize a global variable to store log messages
global statusMessages
set statusMessages to {}
-- Function to log messages and collect them for Shortcuts
on logMessage(messageText)
copy messageText to the end of statusMessages -- Add message to the list
log messageText -- Log to Script Editor for debugging
end logMessage
-- Function to check if the drive is mounted
on isDriveMounted(volumeName)
try
set result to do shell script "mount | grep '/Volumes/" & volumeName & "'"
logMessage("Drive is mounted: " & result)
return true
on error
logMessage("Drive " & volumeName & " is not mounted.")
return false
end try
end isDriveMounted
-- Function to check if Time Machine backup is running
on isBackupRunning()
try
set status to do shell script "tmutil status | awk -F '= ' '/Running/{gsub(/;/,\"\",$2); print $2}'"
logMessage("Time Machine running status: " & status)
return status is "1"
on error
logMessage("Failed to check Time Machine status.")
return false
end try
end isBackupRunning
-- Function to start Time Machine backup
on startBackup(volumeName)
if not isDriveMounted(volumeName) then
logMessage("Drive " & volumeName & " is not mounted. Backup cannot proceed.")
error "Drive not mounted."
end if
logMessage("Checking Time Machine status...")
if isBackupRunning() then
logMessage("Backup is already in progress.")
else
logMessage("Starting Time Machine backup...")
try
set result to do shell script "tmutil startbackup --auto --block"
logMessage("Backup completed successfully.")
on error
logMessage("Failed to complete backup. Please check your Time Machine configuration.")
error "Backup failed."
end try
end if
end startBackup
-- Function to eject the Time Machine volume
on ejectVolume(volumeName)
logMessage("Preparing to eject Time Machine volume...")
try
set unmountResult to do shell script "diskutil unmount '/Volumes/" & volumeName & "'"
logMessage("Volume unmounted successfully: " & unmountResult)
set ejectResult to do shell script "diskutil eject '/Volumes/" & volumeName & "'"
logMessage("Volume ejected successfully.")
on error errMsg
logMessage("Error during ejection: " & errMsg)
end try
end ejectVolume
-- Main script execution
try
logMessage("Script started.")
startBackup(timeMachineVolume)
ejectVolume(timeMachineVolume)
logMessage("Script completed successfully.")
on error errMsg
logMessage("Script failed with error: " & errMsg)
end try
-- Return the collected status messages to Shortcuts
return statusMessages
What do you think? Is this script reliable for my use case? Are there better approaches to achieve the same goal?
Thanks in advance for your help!