【问题标题】:Firestore automatic backup using cloud functions?Firestore 使用云功能自动备份?
【发布时间】:2019-12-21 23:29:52
【问题描述】:

Firebase 文档建议您部署 App Engine 应用来处理自动 Firestore 导出。

https://firebase.google.com/docs/firestore/solutions/schedule-export

app.js

const axios = require('axios');
const dateformat = require('dateformat');
const express = require('express');
const { google } = require('googleapis');

const app = express();

// Trigger a backup
app.get('/cloud-firestore-export', async (req, res) => {
  const auth = await google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/datastore']
  });

  const accessTokenResponse = await auth.getAccessToken();
  const accessToken = accessTokenResponse.token;

  const headers = {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + accessToken
  };

  const outputUriPrefix = req.param('outputUriPrefix');
  if (!(outputUriPrefix && outputUriPrefix.indexOf('gs://') == 0)) {
    res.status(500).send(`Malformed outputUriPrefix: ${outputUriPrefix}`);
  }

  // Construct a backup path folder based on the timestamp
  const timestamp = dateformat(Date.now(), 'yyyy-mm-dd-HH-MM-ss');
  let path = outputUriPrefix;
  if (path.endsWith('/')) {
    path += timestamp;
  } else {
    path += '/' + timestamp;
  }

  const body = {
    outputUriPrefix: path
  };

  // If specified, mark specific collections for backup
  const collectionParam = req.param('collections');
  if (collectionParam) {
    body.collectionIds = collectionParam.split(',');
  }

  const projectId = process.env.GOOGLE_CLOUD_PROJECT;
  const url = `https://firestore.googleapis.com/v1beta1/projects/${projectId}/databases/(default):exportDocuments`;

  try {
    const response = await axios.post(url, body, { headers: headers });
    res
      .status(200)
      .send(response.data)
      .end();
  } catch (e) {
    if (e.response) {
      console.warn(e.response.data);
    }

    res
      .status(500)
      .send('Could not start backup: ' + e)
      .end();
  }
});

// Index page, just to make it easy to see if the app is working.
app.get('/', (req, res) => {
  res
    .status(200)
    .send('[scheduled-backups]: Hello, world!')
    .end();
});

// Start the server
const PORT = process.env.PORT || 6060;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

package.json

{
  "name": "solution-scheduled-backups",
  "version": "1.0.0",
  "description": "Scheduled Cloud Firestore backups via AppEngine cron",
  "main": "app.js",
  "engines": {
    "node": "8.x.x"
  },
  "scripts": {
    "deploy": "gcloud app deploy --quiet app.yaml cron.yaml",
    "start": "node app.js"
  },
  "author": "Google, Inc.",
  "license": "Apache-2.0",
  "dependencies": {
    "axios": "^0.18.0",
    "dateformat": "^3.0.3",
    "express": "^4.16.4",
    "googleapis": "^38.0.0"
  },
  "devDependencies": {
    "prettier": "^1.16.4"
  }
}

cron.yaml

cron:
- description: "Daily Cloud Firestore Export"
  url: /cloud-firestore-export?outputUriPrefix=gs://BUCKET_NAME[/PATH]&collections=test1,test2
  target: cloud-firestore-admin
  schedule: every 24 hours

问题

但我想知道,是否有可能使用 HTTP 云功能Cloud Scheduler 实现相同的功能?

此 App Engine 代码中是否有我无法通过 HTTP 云函数重现或访问的内容?我的意思是,App Engine 项目在这里真的有必要吗?

注意:不是基于意见的问题,也不是太宽泛。我想知道我是否需要 App Engine 来实现这种行为,以及为什么。

显然,我不需要将其设置为快速服务器。只是一个普通的 HTTP 云函数,调用时会进行导出。

我会在 Cloud Scheduler 中添加类似以下 cron 作业的内容:

【问题讨论】:

  • 您介意分享您编写的云函数的代码吗?我也有同样的需求和挣扎
  • @JeremyBelolo 我已经在你的问题上发布了。

标签: firebase google-app-engine cron google-cloud-firestore firebase-admin


【解决方案1】:

如果您使用 Cloud Scheduler 触发执行备份的功能,则根本不需要 App Engine。

【讨论】:

  • 谢谢,道格!那么,我将使用 Cloud Functions。
猜你喜欢
  • 2020-11-01
  • 1970-01-01
  • 2019-12-22
  • 2020-08-17
  • 2018-05-09
  • 2019-11-12
  • 2022-10-18
  • 2022-12-20
  • 2018-06-23
相关资源
最近更新 更多