Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Last active November 20, 2023 07:52
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tjluoma/8c8a05c217daa2de085c9c07531805b3 to your computer and use it in GitHub Desktop.
Save tjluoma/8c8a05c217daa2de085c9c07531805b3 to your computer and use it in GitHub Desktop.
a variation of 'timemachine-mount-run-unmount.sh' meant to be used with Keyboard Maestro
#!/usr/bin/env zsh -f
# Purpose: Once you set the DEVICE,
# this script will mount your Time Machine drive,
# run Time Machine,
# and then unmount the drive
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2020-04-20
## To find the device, mount the Time Machine drive and then run this command in Terminal:
#
# mount | egrep '^/dev/' | sed -e 's# (.*#)#g' -e 's# on /# (/#g'
#
# and you will see a bunch of entries like this
#
# /dev/disk2s1 (/Volumes/MBA-Clone - Data)
# /dev/disk3s5 (/Volumes/Storage)
# /dev/disk4s6 (/Volumes/MBA-Clone)
#
# You need to set
#
# DEVICE='/dev/disk3s5'
#
# or whatever is correct for your Time Machine drive
DEVICE=''
################################################################################################
if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
else
PATH="$HOME/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin"
fi
zmodload zsh/datetime
TIME=$(strftime "%Y-%m-%d--%H.%M.%S" "$EPOCHSECONDS")
function timestamp { strftime "%Y-%m-%d--%H.%M.%S" "$EPOCHSECONDS" }
LOG="/tmp/km-timemachine-mount-run-unmount.$TIME.log"
STATUS=$(tmutil currentphase)
if [[ "$STATUS" != "BackupNotRunning" ]]
then
echo "Error [`timestamp`]: Time Machine status is '$STATUS'. Should be 'BackupNotRunning'." \
| tee -a "$LOG"
exit 1
fi
# if you have multiple Time Machine destinations, this might not give you the right info
# I'm assuming you only have one
TM_DRIVE_NAME=$(tmutil destinationinfo | egrep '^Name ' | sed 's#^Name *: ##g' | head -1)
MNTPNT="/Volumes/$TM_DRIVE_NAME"
if [[ -d "$MNTPNT" ]]
then
echo "$MNTPNT' is already mounted". >>| "$LOG"
else
if [[ "$DEVICE" == "" ]]
then
echo "[Fatal Error]: the 'DEVICE' variable is not set" | tee -a "$LOG"
exit 1
fi
diskutil mountDisk "$DEVICE"
fi
if [[ ! -d "$MNTPNT" ]]
then
echo "[Fatal Error at `timestamp`]: Failed to mount '$MNTPNT'." | tee -a "$LOG"
exit 1
fi
TM_DRIVE_ID=$(tmutil destinationinfo | egrep '^ID ' | sed 's#^ID *: ##g' | head -1)
echo "Starting backup at `timestamp`"
# `caffeinate -i` is optional but keeps your Mac from sleeping
caffeinate -i tmutil startbackup --block --destination "$ID"
EXIT="$?"
if [[ "$EXIT" == "0" ]]
then
echo "tmutil finished successfully at `timestamp`." >>| "$LOG"
else
echo "tmutil failed (Exit = $EXIT) at `timestamp`." | tee -a "$LOG"
exit 1
fi
while [[ -d "$MNTPNT" ]]
do
# this will try to unmount the drive as long as it is mounted
echo "Trying to unmount '$MNTPNT' at `timestamp`..." >>| "$LOG"
diskutil unmountDisk "$MNTPNT"
sleep 10
done
echo "Finished successfully at `timestamp`" >>| "$LOG"
exit 0
#EOF
@flinge
Copy link

flinge commented May 5, 2022

caffeinate -i tmutil startbackup --block --destination "$ID"
pls update ID to TM_DRIVE_ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment