未加星标

On-demand backups with the Compose API and Node.js

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Compose API node.js backups On-demand backups with the Compose API and Node.js

You need your latest backup and you need it automatically? We can show you how to create an on-demand backup and retrieve it automatically using the Compose API and Node.js

We recently showed you how to use the API and Node.js to retrieve the automated backups made by Compose deployments every day . The API is pretty straightforward for those cases - find a backup record, download the associated file from the backup record's link. To create and retrieve an on-demand backup is a little more intricate as we'll have to make the backup program wait. Ride along with the code, and we'll show you why.

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:

let startBackup = (deploymentid, options) => { fetch(`${apibase}/deployments/${deploymentid}/backups`, { method: "POST", headers: apiheaders }) .then(function (res) { return res.json(); })

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.

.then(function (recipe) { console.log(`Recipe Id: ${recipe.id} Status: ${recipe.status} `); pollForRecipeComplete(recipe.id) }) .catch(function (err) { console.log(err); }); }

So, the key to checking a recipe's status is the2016-07-get-recipe endpoint. By using GET on this endpoint with the recipe id, we can get a current copy of the recipe.

let pollForRecipeComplete = (recipeid) => { fetch(`${apibase}/recipes/${recipeid}`, { headers: apiheaders }) .then(function (res) { return res.json() })

We now have the current recipe JSON and we can check for whether the recipe has run to completion. At that point the on-demand backup will be ready. We'll know that if the status becomes "complete":

.then(function (recipe) { if (recipe.status == "complete") { process.stdout.write("\n"); return getLatestOndemand(recipe.deployment_id); } else { process.stdout.write('.'); setTimeout(pollForRecipeComplete, 10000, recipeid); } }) };

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:

let getLatestOndemand = (deploymentid) => { fetch(`${apibase}/deployments/${deploymentid}/backups`, { headers: apiheaders }) .then(function (res) { return res.json(); }) .then(function (json) { let backups = json["_embedded"].backups;

Getting the list of backups is essentially the same as the listBackups() function in theprevious 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:

for (let backup of backups) { if(backup.type=="on_demand") { getBackup(backup.deployment_id,backup.id) return true; } } console.log("No on demand backup found") return false; }) .catch(function (err) { console.log(err); }); }

Once we find the backup id, we call the getBackup() function we created in theprevious article, giving it the deployment id and backup id. And we're done...

Well, nearly. To really finish this we need to add a line to the yargs command line definition:

yargs.version("0.0.1") ... .command("start <deploymentid>", "Start on demand backup", {}, (argv) => startBackup(argv.deploymentid)) ... .help() .argv;

Now we can run our command:

$ backupomatplus.js start 55f694344d847d005d000009 Recipe Id: 58a1bf06e1baf600140002cf Status: running .. Going to download rethinkery_2017-02-13_14-13-37_utc_on_demand Done $

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 [email protected] We're happy to hear from you.

attribution Pixabay

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Node.jsGit
分页:12
转载请注明
本文标题:On-demand backups with the Compose API and Node.js
本站链接:http://www.codesec.net/view/533317.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(61)