【问题标题】:Node.js - GCP - Cloud Functions - VM disk snapshot across all regions and VMsNode.js - GCP - Cloud Functions - 所有区域和虚拟机的虚拟机磁盘快照
【发布时间】:2018-12-19 23:31:48
【问题描述】:

我想使用 GCP serverless - Cloud Functions 来备份所有区域的虚拟机磁盘,

基本上 - 我想备份所有带有时间戳快照名称的 VM 磁盘,例如 snapshot-vm_name-timestamp

任何超过 14 天的备份,请将其删除。所以这是自助服务功能。

文档很烂,没有多大帮助。这就是我能走多远

exports.run_process = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';

  console.log(message);

  const Compute = require(`@google-cloud/compute`);
  const compute = new Compute();
  const zone = compute.zone('us-east1-b');

  zone.getDisks(function(err, disks) {
    // `disks` is an array of `Disk` objects.
    disks.map(disk => {
      console.log(disk);
    });
  });

  res.status(200).send(message);
};

非常感谢任何帮助或建议。

参考链接:https://cloud.google.com/nodejs/docs/reference/compute/0.10.x/Zone

【问题讨论】:

标签: node.js google-cloud-platform google-cloud-functions


【解决方案1】:

您的代码中有错误。您在哪里使用 const Compute = require(@google-cloud/compute); 语句。而不是“ ` ”,您必须使用“ ' ”。另外,请记住使用添加依赖项所需的库。你的package.json 应该是这样的:

{   
    "name": "sample-http",   
    "version": "0.0.1",   
    "dependencies": {
    "google-cloud": "^0.58.2",
    "@google-cloud/compute": "^0.10.0"   
    } 
 }

继续前进,接下来的步骤应该类似于以下内容:

  1. 使用Snapshot Documentation 中的代码加载所有可用的快照。使用与获取磁盘相同的逻辑

  2. 获取快照的creationTimeStamp 并计算天数差异

  3. 创建一个变量,以 “DD-MM-YYYY” 格式获取当前日期 var datetime = new Date().toLocaleDateString(); 以将其用作新快照的后缀

  4. 使用此方法new_snapshot_name = new_snapshot_name.replace(/\//g, '-'); 将所有/ 替换为- 以匹配正则表达式'(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)'

  5. 使用getDisk(...)方法列出所有磁盘并创建它们的快照

我做了一点编码,它对我有用。该函数执行您在问题中描述的所有内容。你可以找到我的code example in GitHub

【讨论】:

  • 出色的工作安德烈。 github中的代码按预期工作。和很棒的解释。真的很感动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 2016-04-16
相关资源
最近更新 更多