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.,
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:
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.