Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save organicaudio/9726affd4f7e37ff172d737a2fb3886a to your computer and use it in GitHub Desktop.

Select an option

Save organicaudio/9726affd4f7e37ff172d737a2fb3886a to your computer and use it in GitHub Desktop.
Time Machine - Mount Remote SparseBundle (AppleScript)
-- VAR:
set nameBackup to "YourHostName Backups"
set pathBackup to "/Volumes/nameBackup/"
set pathVolume to "smb://user@REMOTEHOST._smb._tcp.local/time_machine"
set pathSparseBundle to "/Volumes/time_machine/HOSTNAME.sparsebundle"
-- SCRIPT:
tell application "System Events" to set diskNames to name of every disk
if nameBackup is in diskNames then
-- backup destination not mounted; take no action
display dialog "" & nameBackup & " - disk exists - no action taken" buttons {"OK"} default button 1 with icon 1
else
-- backup destination not mounted; mount remote volume than mount disk image
display dialog "" & nameBackup & " - disk does not exist - mounting..." buttons {"OK"} default button 1 with icon 1
try
-- mount remote volume
mount volume pathVolume
end try
-- mount (timemachine) sparsebundle image
do shell script "hdiutil attach -mountpoint " & quoted form of POSIX path of pathBackup & " " & quoted form of POSIX path of pathSparseBundle
-- validate remote mount & diskimage attach was successful
tell application "System Events" to set diskNames to name of every disk
if nameBackup is in diskNames then
-- previous actions were successful
display dialog "" & nameBackup & " - disk now mounted" buttons {"OK"} default button 1 with icon 1
else
-- failure: something broke or remote volume not available. user debug required
display dialog "" & nameBackup & " - FAILURE - remote vol not available or something broke. please debug and try again" buttons {"OK"} default button 1 with icon 1
end if
end if
@organicaudio
Copy link
Author

organicaudio commented Nov 28, 2021

TL;DR This script simplifies the use of an (encrypted) Sparse Bundle Disk Image as a destination for macOS (Time Machine) & iOS (iMazing) device backups using a Samba share.

LANGUAGE: AppleScript

Logic:

  • Check if a specific volume is attached (by name) for use as a backup destination
  • If match present; script ends having taken no action
  • If no match present = script mounts remote path to a volume path and attaches the sparse bundle disk image located inside to a volume
  • Script verifies actions taken were successful be repeating the initial validation step.
  • Script silent except for failure result (action taken but fails for unknown reason), this produces an alert dialog ensuring user alerted to a problem

Background:

  • Use of remote sparse bundles requires first to mount the remote path path (eg. smb) as normal
  • Once done the sparse bundle is effectively just a disk image located on a file path (irregardless of local / remote).
  • Time Machine & iMazing are not 100% reliable on mounting volume & attaching sparse bundle disk image if not already done so when a backup event is triggered.
  • This script functions as a safety net to ensure that everything is mounted and ready just before the scheduled backup time to ensure success.

Usage:

Localize these variables

nameBackup = name of the mounted sparsebundle disk image (eg. YourHostName Backups)
pathBackup = almost always will be the name above preceeded by /Volumes/
pathVolume = remote path (eg. smb) to mountable share containing SparseBundle file.
(if possible use the Bonjour / Avahi advertised path to smb (that is how Time Machine detects the path without user action)
pathSparseBundle = local mounted path of remote volume and the remaining path to sparsebundle disk image

Create executable Application using Script Editor

See this guide for details https://support.apple.com/en-ca/guide/script-editor/scpedt1072/2.11/mac/12.0

By default macOS Script Editor saves files locally in $HOME/Library/Scripts/

Run from command line:

Execute - Local path:

open -Wa $HOME/Library/Scripts/mount_remote_timemachine_sparsebundle_dest.app

Execute - iCloud path:

open -Wa $HOME/Library/Mobile\ Documents/com~apple~ScriptEditor2/Documents/mount_remote_timemachine_sparsebundle_dest.app

Run as Application:
run from inside gui :)

Run on Calendar Event trigger:
Script can be set to run when a calendar event occurs (think of it as an easy method of cron jobs in macOS) See this guide for a how-to if unfamiliar https://www.idownloadblog.com/2018/10/19/mac-calendar-open-file-alerts/

Changes required for running script scheduled / headless is to remove display dialog entries except final one (that would advise you on failure) since they function as a wait until user responds.

The script would be triggered by the calendar event (say one every 12hours or right before scheduled backup time). Checking if action required, if yes execute mount / attach, validate success, and all the while being silent. Script would only alert user if a failure occurred (disk wasn't mounted, script tried to mount it, but ultimately failed)

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