Firestoreの定期的なバックアップ

公開:2020/10/14
更新:2020/10/15
2 min読了の目安(約2000字TECH技術記事

基本的な方法は、Firebaseのドキュメントが参考になります。

エラー

ドキュメントそのままの設定だと以下のようなエラーが出ました。

TypeError: Cannot read property 'toString' of undefined
at PathTemplate.render (/workspace/node_modules/google-gax/build/src/pathTemplate.js:110:37)
at FirestoreAdminClient.databasePath (/workspace/node_modules/@google-cloud/firestore/build/src/v1/firestore_admin_client.js:962:56)
at exports.scheduledFirestoreExport.functions.pubsub.schedule.onRun (/workspace/index.js:11:33)
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:198:28)
at process._tickCallback (internal/process/next_tick.js:68:7) 

調べた所、functions/index.jsのdatabasePathを以下のように変更すると直るようです。

変更前

functions/index.js
const databaseName = client.databasePath(process.env.GCP_PROJECT, '(default)');

変更後

functions/index.js
const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT;
const databaseName = client.databasePath(projectId, '(default)');

package.json

functions/package.json
{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "main": "index.js",
  "dependencies": {
    "@google-cloud/firestore": "^4.4.0",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

参考