Utilizing your ActionScript knowledge to Script Windows (and backing up your sites while you're at it!)

As part of reviving the What Is Flash Wiki, I restarted the scheduled automatic daily backups of the database from the web server to my local backup machine and thought it would be nice to share with you how I achieve this as it involves JScript for scripting Windows and JScript is a close cousin of ActionScript (they are both based on the latest ECMAScript specs.)

Update: If you're looking to do the same thing on OS X, Mike Chambers has a great post on Shell Scripting on OS X with ECMA / JavaScript

On a hugely dynamic site such as a Wiki or forum, it is very important to have constant backups of the database to protect against data loss. On a busy site with lots of updates, losing even a day of data is not good but it's worse if that becomes two or three days or a week! The way the What Is Flash server is set up, you can hit a certain URL (with proper authorization) to download backups of the site or of the various databases that it uses. What is needed is a script file that hits these URLs and does so on a daily basis, archiving the backups on the local machine. Enter the Scheduled Tasks scheduler and Windows Scripting.

If you're on Windows, there's very little you cannot automate using the Scheduled Tasks scheduler and Windows Scripting Host WSH).

Here are three of the best references I could find on them: To download the backups, I've written a script in JSCript that creates a directory with the current date as its name (in the form YYYY-MM-DD). It then goes and gets the latest backups for that day from the site and saves them locally in the newly-created directory. In order to access the site, I use Lynx, a powerful text-only browser famous in the Linux/Unix world. You cannot use IE for this since IE doesn't support the type of command line arguments that Lynx does. (You can also use CURL and a Cron job for this and it may more sense for you to do so if you're on a Unix-based system.)

The following script illustrates how it works (you need to have the Lynx.bat file -- see the Lynx installation instructions to know what I'm talking about -- in the current directory and substitute your own URL in the script here.

If you save the script as some_script.js, you can run it from the command line using wscript some_script.js. If you now add this to the Scheduled Tasks (Start->Programs->Accesories->System Tools->Scheduled Tasks and then double-click to launch the Add Scheduled Task Wizard) you can have it execute automatically at a specified time interval (eg. every day.)

Scripting Windows using JScript is a piece of cake if you know ActionScript and can give you complete control over your system. Coupled with the Windows Task Scheduler, you can automate things too.

Comments