代码之家  ›  专栏  ›  技术社区  ›  cbdeveloper

使用云功能的FireStore自动备份?

  •  0
  • cbdeveloper  · 技术社区  · 7 年前

    FireBase Docs建议您部署 应用程序引擎 用于处理自动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.');
    });
    

    包.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:
    - 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云函数 以及 云调度程序 ?

    这个应用程序引擎代码中有什么我不能用HTTP云函数重现或访问的吗? 我的意思是,应用引擎项目在这里真的有必要吗?

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

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

    我想在 云调度程序 :

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   Doug Stevenson    7 年前

    如果您使用云调度器触发一个执行备份的函数,则根本不需要应用程序引擎。