代码之家  ›  专栏  ›  技术社区  ›  Tony Merritt

尝试设置自动备份Firestore、云功能时出错

  •  0
  • Tony Merritt  · 技术社区  · 5 年前

    以下是我的教程: Tutorial

    一切似乎都好,它允许我做的一切教程,但当我运行的功能,我得到这个错误。

    textPayload: "TypeError: Cannot read property 'charCodeAt' of undefined
        at peg$parsetemplate (/workspace/node_modules/google-gax/build/src/pathTemplateParser.js:304:17)
        at Object.peg$parse [as parse] (/workspace/node_modules/google-gax/build/src/pathTemplateParser.js:633:18)
        at new PathTemplate (/workspace/node_modules/google-gax/build/src/pathTemplate.js:55:54)
        at segments.forEach.segment (/workspace/node_modules/google-gax/build/src/pathTemplate.js:120:29)
        at Array.forEach (<anonymous>)
        at PathTemplate.render (/workspace/node_modules/google-gax/build/src/pathTemplate.js:114:23)
        at FirestoreAdminClient.databasePath (/workspace/node_modules/@google-cloud/firestore/build/src/v1/firestore_admin_client.js:904:57)
        at exports.scheduledFirestoreExport (/workspace/index.js:13:31)
        at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28)
        at process._tickCallback (internal/process/next_tick.js:68:7)
    
    insertId: "000000-8410c5c7-8304-42b6-b2b6-dd55a54e8cab"
    resource: {2}
    timestamp: "2020-07-11T18:14:35.981Z"
    severity: "ERROR"
    labels: {1}
    logName: "projects/b-b-b-app/logs/cloudfunctions.googleapis.com%2Fcloud-functions"
    trace: "projects/b-b-b-app/traces/d7c07a715d0106225d9963ce2a046489"
    receiveTimestamp: "2020-07-11T18:14:44.813410062Z"
    }
    

    gcloud firestore导出gs://bbbdata备份

    我正在使用firebase站点上的GCP控制台并使用此代码。

    const firestore = require('@google-cloud/firestore');
    const client = new firestore.v1.FirestoreAdminClient();
    
    const bucket = 'gs://bbbdata-backup'
    
    exports.scheduledFirestoreExport = (event, context) => {
    const databaseName = client.databasePath(
    process.env.GCLOUD_PROJECT,
    '(default)'
    );
    
    return client
     .exportDocuments({
      name: databaseName,
      outputUriPrefix: bucket,
      // Leave collectionIds empty to export all collections
      // or define a list of collection IDs:
      // collectionIds: ['users', 'posts']
      collectionIds: [],
    })
    .then(responses => {
      const response = responses[0];
      console.log(`Operation Name: ${response['name']}`);
      return response;
    })
    .catch(err => {
      console.error(err);
    });
    };
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   andresmijares    5 年前

    我去年也遇到过类似的问题,可能你错过了一些许可,我会这样做,希望这对你有用:

    import * as functions from 'firebase-functions'
    import { auth } from 'google-auth-library'
    
    export const generateBackup = async () => {
      const client = await auth.getClient({
        scopes: [
          'https://www.googleapis.com/auth/datastore',
          'https://www.googleapis.com/auth/cloud-platform'
        ]
      })
    
      const path = `YOUR_FOLDER_NAME_FOR_THE_BACKUP`
      const BUCKET_NAME = `YOUR_BUCKET_NAME_HERE`
    
      const projectId = await auth.getProjectId()
      const url = `https://firestore.googleapis.com/v1beta1/projects/${projectId}/databases/(default):exportDocuments`
      const backup_route = `gs://${BUCKET_NAME}/${path}`
    
      return client.request({
        url,
        method: 'POST',
        data: {
            outputUriPrefix: backup_route,
            // collectionsIds: [] // if you want to specify which collections to export, none means all
        }
      })
      .catch(async (e) => {
        return Promise.reject({ message: e.message })
      })
    
    }
    
    

    然后,您可以决定这是这个函数的触发器,并相应地执行它。

    注意:转到项目的IAM部分并找到appengine服务帐户,您将需要添加角色 Cloud Datastore Import Export Admin ,否则将失败。

    你可以多看看 here 非常详细。