本頁面介紹如何安排 Cloud Firestore 數據的導出。要按計劃運行導出,我們建議使用 Cloud Functions 和 Cloud Scheduler。
在你開始之前
在安排託管數據導出之前,您必須完成以下任務:
- 為您的 Google Cloud 項目啟用結算功能。只有啟用了結算功能的 Google Cloud 項目才能使用導出和導入功能。
- 導出操作需要目標 Cloud Storage 存儲桶。在您的 Cloud Firestore 數據庫位置附近的位置創建一個 Cloud Storage 存儲桶。您不能將 Requester Pays 存儲桶用於導出操作。
創建 Cloud Functions 和 Cloud Scheduler 作業
按照以下步驟創建一個啟動 Cloud Firestore 數據導出的 Node.js Cloud Function 和一個調用該函數的 Cloud Scheduler 作業:
火力地堡命令行
安裝 Firebase CLI 。在新目錄中,為 Cloud Functions 初始化 CLI:
firebase init functions --project PROJECT_ID
- 選擇JavaScript作為語言。
- 或者,啟用 ESLint。
- 輸入
y
安裝依賴項。
將
functions/index.js
文件中的代碼替換為以下內容:const functions = require('firebase-functions'); const firestore = require('@google-cloud/firestore'); const client = new firestore.v1.FirestoreAdminClient(); // Replace BUCKET_NAME const bucket = 'gs://BUCKET_NAME'; exports.scheduledFirestoreExport = functions.pubsub .schedule('every 24 hours') .onRun((context) => { const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT; const databaseName = client.databasePath(projectId, '(default)'); return client.exportDocuments({ name: databaseName, outputUriPrefix: bucket, // Leave collectionIds empty to export all collections // or set to a list of collection IDs to export, // collectionIds: ['users', 'posts'] collectionIds: [] }) .then(responses => { const response = responses[0]; console.log(`Operation Name: ${response['name']}`); }) .catch(err => { console.error(err); throw new Error('Export operation failed'); }); });
- 在上面的代碼中,修改以下內容:
- 將
BUCKET_NAME
替換為您的存儲桶名稱。 -
every 24 hours
修改一次以設置導出計劃。使用AppEngine cron.yaml 語法或unix-cron 格式(* * * * *
)。 修改
collectionIds: []
只導出指定的集合組。保持原樣以導出所有集合。
- 將
部署預定函數:
firebase deploy --only functions
GCP 控制台
創建雲函數
在 GCP Console 中打開Cloud Functions頁面:
- 單擊創建函數
- 輸入函數名稱,例如
firestoreExport
- 在Trigger下,選擇Cloud Pub/Sub
- 在主題下,選擇創建新主題。輸入發布/訂閱主題的名稱,例如
initiateFirestoreExport
。記下創建 Cloud Scheduler 作業所需的主題名稱。 - 在Source code下,選擇Inline editor 。在
index.js
下輸入以下代碼:const firestore = require('@google-cloud/firestore'); const client = new firestore.v1.FirestoreAdminClient(); // Replace BUCKET_NAME const bucket = 'gs://BUCKET_NAME' 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); }); };
在上面的代碼中,修改以下內容:- 將
BUCKET_NAME
替換為您的存儲桶名稱。 修改
collectionIds: []
只導出指定的集合組。保持原樣以導出所有集合。
- 將
- 在
package.json
下,添加以下依賴項:{ "dependencies": { "@google-cloud/firestore": "^1.3.0" } }
- 在Function to execute下,輸入
scheduledFirestoreExport
,index.js
中函數的名稱。 - 單擊創建以部署 Cloud Function。
創建 Cloud Scheduler 作業
接下來,創建調用您的 Cloud Functions 的 Cloud Scheduler 作業:
在 GCP Console 中打開Cloud Scheduler頁面:
- 單擊創建作業。
- 輸入作業的名稱,例如
scheduledFirestoreExport
。 - 輸入頻率,例如,
every 24 hours
。 - 選擇一個時區。
- 在Target下,選擇Pub/Sub 。在主題字段中,輸入您在雲函數旁邊定義的發布/訂閱主題的名稱,在上例中為
initiateFirestoreExport
。 - 在負載字段中,輸入
start export
。該作業需要定義有效負載,但上面的 Cloud Function 實際上並未使用該值。 - 單擊創建。
配置訪問權限
接下來,授予 Cloud Function 啟動導出操作和寫入 GCS 存儲桶的權限。
此 Cloud Functions 使用您項目的默認服務帳戶對其導出操作進行身份驗證和授權。當您創建項目時,系統會使用以下名稱為您創建一個默認服務帳戶:
PROJECT_ID@appspot.gserviceaccount.com
此服務帳號需要啟動導出操作和寫入您的 Cloud Storage 存儲桶的權限。要授予這些權限,請將以下 IAM 角色分配給默認服務帳戶:
-
Cloud Datastore Import Export Admin
- 存儲桶的
Owner
或Storage Admin
角色
您可以使用gcloud
和gsutil
命令行工具來分配這些角色。
如果尚未安裝,您可以從 Google Cloud Platform Console 中的Cloud Shell訪問這些工具:
啟動 Cloud Shell
分配Cloud Datastore Import Export Admin角色。替換PROJECT_ID ,並運行以下命令:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member serviceAccount:PROJECT_ID@appspot.gserviceaccount.com \ --role roles/datastore.importExportAdmin
為您的存儲桶分配存儲管理員角色。替換PROJECT_ID和BUCKET_NAME ,並運行以下命令:
gsutil iam ch serviceAccount:PROJECT_ID@appspot.gserviceaccount.com:admin \ gs://BUCKET_NAME
如果您禁用或刪除 App Engine 默認服務帳戶,您的 App Engine 應用將無法訪問您的 Cloud Firestore 數據庫。如果您禁用了 App Engine 服務帳戶,您可以重新啟用它,請參閱啟用服務帳戶。如果您在過去 30 天內刪除了您的 App Engine 服務帳戶,您可以恢復您的服務帳戶,請參閱取消刪除服務帳戶。
測試您的 Cloud Scheduler 作業和 Cloud Function
您可以在 Google Cloud Platform Console 的Cloud Scheduler頁面中測試您的 Cloud Scheduler 作業。
在 GCP Console 中打開Cloud Scheduler頁面。
打開 Cloud Scheduler 頁面在新的 Cloud Scheduler 作業所在的行中,單擊Run now 。
幾秒鐘後,Cloud Scheduler 作業應將結果列更新為成功,並將上次運行更新為當前時間。您可能需要單擊Refresh 。
Cloud Scheduler 頁面僅確認該作業調用了您的 Cloud Function。打開 Cloud Functions 頁面以查看您的函數的日誌。
查看 Cloud Functions 日誌
要查看 Cloud Functions 是否成功啟動了導出操作,請打開函數的日誌:
Firebase 控制台
在 Firebase 控制台中打開Cloud Functions頁面。
GCP 控制台
在 GCP Console 中打開Cloud Functions頁面。
查看導出進度
您可以使用gcloud firestore operations list
命令查看導出操作的進度,請參閱管理導出和導入操作。
導出操作完成後,您可以在 Cloud Storage 存儲桶中查看輸出文件: