Automator + Cron & email confirmation for backups

I used to use Calendar Events to get my machine to perform selective backups of desktop folders to an external drive connected to my laptop every work day. I could also backup to a network server.

You see, I have a network server that gets backed up a few times a day at work… but the Adobe CC applications don’t like saving files to this server… I get application crashes often. Perhaps it’s Yosemite in combination with CC and the network server, but this relegated me to needing to work on files locally. But not having those files backup would be a big problem.

The Calendar Events worked well but suddenly they stopped working. I think Yosemite might have had something to do with that. If you google “Calendar events not firing” you’ll likely find a slew of forum posts related to this problem. My previous workflow was thus:

  1. Wake up from the screensaver.
  2. Enter my password to gain access.
  3. Copy the contents of several directories to the external drive, overwriting files when present.
  4. Email myself with a confirmation that the procedure succeeded.
  5. Go back to the screensaver, thus locking the machine back down.

Since this didn’t work as a Calendar Event any more, I decided to just turn to Unix’s Cron – a time-based job scheduler. I am not a huge fan of using a Terminal when I don’t need to, even though I am a developer and most like to run in that environment. Since there are other options, I decided to choose one.

LaunchD Task Scheduler. You can find it in OS X’s App Store. It costs $3.99 but the simple convenience that it offers made it an easy decision for me. You can create/edit/delete tasks. Check it out.

So essentially I was able to keep my original Automator worrkflow, convert it to an Automator application & then created a new cron job for it using LaunchD Task Scheduler. Mine kicks off at 5PM every day. It works a treat and I really love getting that 5PM email. There may be ways to make the .app a little better, but for a blunt force take everything and copy it without relying on TimeMachine, etc. it’s been awesome.

Below is some of the Automator-related bits. The flow is as follows:

  1. Run AppleScript
  2. Get Specified Finder Items
  3. Copy Finder Items
  4. Create New Outlook Mail Message
  5. Pause
  6. Send Outgoing Mail Messages
  7. Run AppleScript

1. Run AppleScript

on run {input, parameters}
	
	tell application "System Events"
		tell process "Finder"
			keystroke space
		end tell
	end tell
	delay 1
	tell application "System Events"
		keystroke "mypassword" & return
	end tell
	delay 2
	
	return input
end run

Here the running screensaver is stopped by the space keystroke and the account password is entered to get into the running system account.

2. Get Specified Finder Items

I provided this Automator bit a few directory locations that I wanted to back up. Basically a few lines of Name/Path data. Adding these is easy – use the Add… button on the Automator section panel.

getSpec3. Copy Finder Items

Here I provided a folder for my mounted eternal hard drive. This is where you want the specified Finder items to be copied to. I selected Replacing existing files. That’s up to you. It takes a little longer to perform the File I/O operation but for me I thought it was a little cleaner and less error-prone. Right or wrongly.

4. Create New Outlook Mail Message

Make sure in your Options for this panel that you select Ignore this action’s input. Otherwise you’re going to be attaching all those Finder items to your email message. I can’t see why you’d want to do that, but if you do, then don’t select that option. I am using Outlook version 15.13.1 (Office 2016) for this. It’s been far more reliable than Office 2011 was.

Make sure the correct From is selected (or just use Default), enter the email you’d like to send the email to, and a message. I basically say this in my message, “Today’s backup has successfully completed. Have a wonderful evening.”

5. Pause

I’m not sure if this panel is actually needed. I have a pause of 2 seconds at this point. When I was using Office 2011 for Mac if I didn’t have a pause sometimes the email would fail. Perhaps it was composing programmatically and didn’t get everything in before the next step. I kept it in since it shouldn’t be a big deal to have a pause since I won’t be sitting in front of the machine when the app launches.

6. Send Outgoing Outlook Mail Messages

Using the same account used to compose (for me). Here the email is actually fired. Within a short time from this step’s firing the email should hit the appropriate InBox. Yay!

7. Run AppleScript

Here is where things are buttoned up and the system is sent back to it’s screensaver which has the password option turned on in it’s settings.

on run {input, parameters}
	delay 5
	tell application "System Events" to start current screen saver
	return input
end run

Another pause just to ensure that the email was sent – I’m pretty sure this part isn’t needed, but again… since no one is sitting in front of the machine, why not? The script tells OS X to start up the current screen saver again – not waiting to time out.


And that’s about it. You can set up multiple cron jobs and Automator applications to do many things at different times. Even when drives mount, etc. My use is simple, but works well for me. Perhaps you too.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.