First up, we need to create our latest backup. For that the 2016-07-post-deployments-backups endpoint comes into play. POSTing at this endpoint with a deployment id in the path will start an on-demand backup:

But this endpoint doesn't wait till it can return a backup id or backup record. It returns immediately with a Compose API recipe. Recipes are how we track the progress of tasks in Compose. When there's work to be done a recipe is found to do that task and it is run. Each recipe has an id and we can query another endpoint with that recipe id to see how it has progressed. That progress is represented by the status field of the recipe.

As an aside, recipes are forever on Compose, once run, they form a part of the history of a deployment. You can even query a deployment for all the recipes ever run against it, including the ones use to create it initially. That way, we know what happened to any deployment. The more you know.

Back to the task at hand, for this we'll print out some of the recipe information first, then call up a new function, pollForRecipeComplete() giving it the recipe id to work with.

If it is complete, we return the result of calling getLatestOndemand() with the deployment_id the recipe was working on. We'll come back to that in a moment. The else side of the if simply writes out a dot and then sets up the pollForRecipeComplete function to be called again in ten seconds.

Ok, it's time to get the latest on-demand backup in getLatestOndemand(). The recipe, in its current incarnation, can't tell us about the backup it made, so we have to download the on-demand backup list so we can search through that for its work:

Getting the list of backups is essentially the same as the listBackups() function in the previous article, using the 2016-07-get-deployment-backups endpoint to retrieve the JSON list. The difference is that we aren't going to list them. Instead, we are going to search for a backup of type "on_demand". There's only one on-demand backup associated with a deployment at any time so this should be the backup we're looking for:

You'll find the code for this in backupomatplus.js in the backupmat repository on Github. Of course, the Compose API is there for you to make the integrations that best suit your application. We'll be taking a wider look at the API in a future article, covering all the API's functionality.

If you have any feedback about this or any other Compose article, drop the Compose Articles team a line at articles@compose.com. We're happy to hear from you.

Dj Walker-Morgan is Compose's resident Content Curator, and has been both a developer and writer since Apples came in II flavors and Commodores had Pets. Love this article? Head over to Dj Walker-Morgan’s author page to keep reading.