College Time Machine Automount

From Software Wiki
Jump to navigation Jump to search

College Time Machine Automount (version 1.00)

Released May 30, 2022
By Robert P. Allred, PhD

Release Announcement]

This AppleScript is written to allow a remote user to automatically connect to a file share on a home network and complete a Time Machine backup.

My daughter will be going out of state for college and is not very good about backups. I wanted to make sure that in additional to the external harddrive I got for her MBA, there was an automated and scheduled way to connect to the home network and do a Time Machine backup here as well. This script solves that problem.

Terms of Use


College Time Machine Automount by Robert P. Allred, PhD is licensed under a Creative Commons Attribution 4.0 International License


You are free to:

Share — copy and redistribute the material in any medium or format
Adapt — remix, transform, and build upon the material for any purpose, even commercially.

This license is acceptable for Free Cultural Works.

The licensor cannot revoke these freedoms as long as you follow the license terms.


Important Links

Current Project Page: https://doctorallred.com/software/index.php?title=College_Time_Machine_Automount

System Requirements

This script has been tested with macOS Catalina (macOS 10.15)and macOS Monteray (macOS 12.4) and requires OpenVPN Connect.

Installation

  • Requires macOS
  • OpenVPN or OpenVPN AS running on a server on the home network. The installation of OpenVPN is beyond the scope of this page. But it is important that static routes are set such that the server on the home network can see the client (remote) machine. Any VPN tunnel product could be used, but the script would need to be updated to include the CLI commands for that appliaction.
  • You must have OpenVPN Connect installed on the remote computer and set up for autologin to the home network.

You will need the following information and should update the script with these values:

  • Home_SSID - the name of the wifi on the home network.
  • IP_address - the static IP address of the Time Machine server on the home network.
  • Server_User - the macOS user name to login to the Time Machine server
  • Server_Volume - the name of the server share/mounted volume name where the Time Machine backups are stored.
  • Destination_ID - the unique Time Machine ID. See below for instructions on finding it.

While the remote machine is on the home network, mount the Time Machine file share using its IP address in the form of smb://Server_User@IP_address/Server_Volume. Once you have mounted this volume, go to System Preferences and add a new Time Machine destination on the server. Set your options. Let the first backup complete.

Open the Terminal app on the remote machine and type tmutil destinationinfo. In the resulting output, make note of the Destination_ID.

Update the script with the variables above. Export as an application and save it in /Applications. In Calendar, create a recurring event with a reminder to open a file. Select the file you just saved in /Applications (some people may find it better to use a cron job to schedule this--cron should be used with caution and is beyond the scope of this article).

When the remote machine is on the home network, it will mount the file share and run Time Machine and then continue running hourly backups until the file share is manually disconnected. When remote, the script will run on the schedule sent, check to see that it is not at home, connect to the home network through the VPN tunnel, mount the file share, complete a Time Machine backup, unmount the remote file share, disconnect the VPN, and then display a success dialog.

Current Version

Version 1.00 (May 30, 2022)

(*
VERSION 1.00
May 30, 2022
By Robert P. Allred, PhD, <https://doctorallred.com>

//TERMS OF USE
College Time Machine Automount by Robert P. Allred, PhD is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
To view a copy of this license, visit <http://creativecommons.org/licenses/by-nc/4.0/>.

// IMPORTANT LINKS:
Current Project Page: <https://doctorallred.com/software/index.php?title=College_Time_Machine_Automount>

// REQUIREMENTS:
Version 1.00 has been tested with macOS Catalina (10.15) and macOS Monteray (12.4)

// INSTALLATION:  
Save the script, add to cron and/or calendar event. See https://doctorallred.com/software/index.php/College_Time_Machine_Automount

// CHANGELOG:

	Version 1.00 (May 30, 2022): Initial release.
*)

tell application "Finder"
	-- Set server variables
	set Home_SSID to "Family_Wifi" -- please note that this would be only the FIRST word of your SSID
	set IP_address to "192.168.24.11" -- static IP address of fileserver on the home network
	set Server_User to "usernamemcusername" -- server user name
	set Server_Volume to "Time_Machine_Backups" -- name of the share/volume
	set Server_Mounted to true -- assume that the volume is mounted
	set Destination_ID to "15E6A149-55C2-4BAA-87C5-2412344812AB" --to get the destination ID use command line "tmutuil destinationinfo"
	
	try
		-- Check to see if the volume is already mounted
		
		set mountedVolumes to do shell script "ls /Volumes/"
		
		if (mountedVolumes contains Server_Volume) is false then
			
			set Server_Mounted to false
			
		end if
		
		-- SSID DETECTION
		
		set SSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2}'"
		
		-- IF HOME NETWORK IS NOT DETECTED
		
		if SSID is not Home_SSID then -- NOTE: the script above is returning only the first word in the SSID, so for me it works with both my main and guest wifi networks.
			
			-- Connect to OpenVPN tunnel to home network
			-- This assumes you have OpenVPN Connect installed and a working profile with auto-login
			display notification "You are not at home. Will now attempt to connect to VPN" with title "Remote TimeMachine Script" sound name "Glass"
			do shell script "'/Applications/OpenVPN Connect/OpenVPN Connect.app/contents/MacOS/OpenVPN Connect' --connect --minimize"
		end if
		
		-- CONNECT TO FILE SERVER IF NEEDED
		
		if Server_Mounted is false then
			
			-- Check IP address is reachable
			(*
				Derived from a work at <https://forums.macrumors.com/threads/applescript-check-if-connected-to-a-specific-network-then-run-else-fail-silently.1640860/>
			*)
			
			set IP_Valid to true
			
			try
				do shell script ("ping -c 2 " & IP_address)
			on error
				set IP_Valid to false
			end try
			
			if IP_Valid is true then -- The file server is reachable
				
				-- MOUNT REMOTE SHARE
				display notification "Will now attempt to connect mount the remote volume." with title "Remote TimeMachine Script" sound name "Glass"
				mount volume "smb://" & Server_User & "@" & IP_address & "/" & Server_Volume -- mount the correct file share using the right username this could all be built
				-- you can include the password in plaintext, but that's a bad idea.
				delay 5 --need enough time for volume to mount before trying to copy file.
				
				-- SET RESULT VAR TO "WIN"
				
				set endresult to "win" -- depreciated
				display notification "Time Machine Server was successfully mounted." with title "Remote TimeMachine Script" sound name "Glass"
				
			else -- The file server is NOT reachable
				
				-- SET RESULT VAR TO "FAIL"
				
				set endresult to "fail" -- depreciated
				display notification "Time Machine Server was not reachable." with title "Remote TimeMachine Script" sound name "Funk"
				
				-- END
				
			end if
		end if
		-- START TIME MACHINE BACKUP
		(*
 		From: https://stackoverflow.com/questions/20164891/applescript-to-trigger-time-machine-in-mac-os-x-10-9-mavericks
	*)
		
		-- Would love to have countdown notification
		
		do shell script "tmutil startbackup -d " & Destination_ID & " -b"
		
		(*
			the -b flag blocks until TM finishes at which point the TimeMachine volume is unmounted
			e.g., "-b&&umount '/Volumes/TimeMachine Backups"
		    	https://apple.stackexchange.com/questions/256209/command-line-unmount-a-smb-network-drive
		   
		 	to get the destination ID to use with the -d flag, use "tmutuil destinationinfo"
		    	https://ss64.com/osx/tmutil.html
		*)
		
		-- WRAP UP
		
		-- Disconnect from VPN if still connected and dismount remote volume
		-- If Home Network Is Not Detected
		
		if SSID is not Home_SSID then -- NOTE: the script above is returning only the first word in the SSID, so works with both main and guest
			
			do shell script "umount '/Volumes/" & Server_Volume & "'"
			do shell script "'/Applications/OpenVPN Connect/OpenVPN Connect.app/contents/MacOS/OpenVPN Connect' --quit"
			
		end if
		
		display alert "TimeMachine backup to Time Machine Server successfully completed."
		
		-- ELSE
		
	end try
end tell


[Download here as Zip archive]

Change requests

  • Countdown or progress dialog during Time Machine backup.

Previous versions

Full Change Log

  • 1.00 (May 30, 2022): Initial release of script