Tuesday, December 6, 2016

Scheduling a Time Machine backup, using AppleScripts and Calendar

Scheduling a Time Machine backup, using AppleScripts and Calendar.

Time Machine is a very handy tool but the lack of scheduling can be frustrating. Following these simple steps can help to solve that problem. 


Setup


Create the AppleScript to turn on and off time machine.
Time Machine is the built-in backup feature of your Mac. It backs up changed files every hour, which is great. But there may be situations when you need to turn it off from time to time. Maybe you support a Graphics company who makes changes to lots of files every hour and backing them up every hour is not practical. 

This is one way I found to schedule time machine. To start open Script editor.  

You can do this by:
1. Open Script editor from the utilities folder in Applications. Or by holding the Command button and pressing spacebar. This will bring up the search, type Script Editor then press enter.

2. Next create a new file. File > New. 
3. Now it is time to create the scripts, we will need one for enabling Time Machine and one for disabling Time Machine. Scripts provided Below. Change ‘admin’ to the admin user of your computer. 

1. Script for enabling time machine. 
#enable time machine
set user_name to "admin"
set user_passwd to (do shell script "security find-generic-password -wl 'TimeMachineAccess'")

do shell script "tmutil enable" user name user_name password user_passwd with administrator privileges
2. Script for disabling time machine.
#disable time machine
set user_name to "admin"
set user_passwd to (do shell script "security find-generic-password -wl ‘TimeMachineAccess'")

do shell script "tmutil stopbackup" user name user_name password user_passwd with administrator privileges
do shell script "tmutil disable" user name user_name password user_passwd with administrator privileges

4. To enable or disable Time Machine with a script, it needs to have administrator privileges. This is done securely by setting the password to a keychain entry that we create called TimeMachineAccess. 
5. Now export each script as an application. File > export… select Application as the file format.  
6. Make sure to run each application before setting them up in Calendar, This is to give it permissions to make changes to Time Machine settings and to give the application permissions. Select ‘Always allow’ when prompted. 
Take note of the user name and password it is asking for, that is the user you need as ‘user_name’ in the application. Also the user&pass to setup in Keychain.

*Note* If we put the password into the application it will always be plain human-readable text, this is why I am using a keychain entry. Even after it has been exported into an application. Using ‘show package contents’ option will show script. 

Create KeyChain Entry to allow the script to change Time Machine settings.
The keychain in Mac OS X is Apple's password management system. A keychain can store all your passwords for applications, servers, and websites, etc.. We need to create our own Keychain called ‘TimeMachineAccess’ to store the password for our application.  




Create a new password:
1. Open Keychain Access.
2. Click the + button at the bottom of the window.
3. Create the new user TimeMachineAccess and give it the name of the account and the password. Click Add. 


4. Next step, setup the Calendar and schedule events to run the applications at the start and end of every day.
Back up
Setup Calendar events to schedule Time Machine.
Now that the scripts have been made and the password has been setup in Keychain it is time to schedule the backups. 


1. Make sure that the backup disk is already setup in Time Machine. By plugging in the drive and selecting ‘Use as Backup Disk’, or select drive in Preferences.

2. Open Calendar and create a new event at the time you want Time Machine on and the computer to backup. Make the event repeat every day. Set the alert to open the file 1 minute before the event happens. Below will turn on Time Machine at 6pm.  


3. Create a new event at the time you want Time Machine off following the same steps but selecting TM-OFF application.

*Note* iCal and Calendar are slightly different on how you open a file, the option was ‘run script’ in iCal. 

No comments:

Post a Comment