Level: Intermediate, Version: FM 18 or later

A Simpler Backup Script

Demo file: backup-simplified.zip (requires FM 18 or later)

Like many developers I go back and forth between developing locally and in a hosted environment, and many years ago I posted a couple articles featuring a simple backup script to create on-the-fly backups when working on local files.

Recently a colleague pointed out that this backup routine, which I last wrote about eight years ago, and have given little further thought to, despite including it with almost every demo file on this site since, was due for an update.

A bit of background: the basic idea is to generate a compressed copy with a timestamp prepended to the file name, e.g.,

20201028_154224_yourFile.fmp12
or
2020.10.28 - 15.42.24 ~ yourFile.fmp12

…and save it “downstream” from the current file location, creating the folder structure as necessary, for example:

(current folder)/Backup/Saturday/

The first portion of the script hasn’t changed in any meaningful way, and begins like so:

Note: the term “filespec” signifies not just the file name, but the location as well. In this case the file will be stored two folders “below” whatever folder our solution happens to be in. The $timestamp variable is sliced and zero-padded in this particular manner a) to remove characters that are forbidden on either the Macintosh or Windows platforms, and b) so that if a number of separate backups are later sorted by name, they will sort in chronological order.

Next we make sure we’re safe to proceed… and this too is the same as before.

So, what has changed? Previously it was necessary to do some shell scripting to create the destination folder structure. We used Send Event on the Windows platform…

…and Perform AppleScript on the Macintosh.

Well, times change, and these days if you want to create folders on the user’s local machine, things are easier than they used to be. The following is native to FileMaker and works cross-platform, with help from a couple script steps, Create Data File and Delete File, that were introduced in FM 18.

And once the folders have been created, the remainder of the script is self-explanatory.

Except… steps 21 through 24 turn out to be unnecessary thanks to this change introduced in 17.

In fact a number of steps gained this capability in 17 (according to the FM 17 New Features page):

Okay, so why did I bother with steps 21 – 24 if I was just going to turn around and say they aren’t necessary? Because if you ever need to create empty folders on a user’s hard drive, and you’re in FM 18 or later (and you’ve chosen a destination where the OS will permit the user to create files), this is a great way to accomplish it.

At any rate, here’s the updated script in all its minimalist glory.

Note: this script is completely portable. You can copy it from the demo file, paste it into your solution and immediately start using it… assuming you are working locally and running FileMaker Pro.

A big thank you goes to Bruce Robertson, who gives so much to the FileMake community, and who wrote the Mac-specific code for this demo.

1 thought on “A Simpler Backup Script”

  1. This a lovely simple solution to a more hard-coded script I use for the same purpose. The only modification I would implement is having a “clone only” option. Perhaps a separate button with a script parameter attached. I find I’m always saving during development and am not interested in backing-up what can be large amounts of test data.

Leave a Reply to Simon Clement Cancel reply

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