1
0

More than 3 years have passed since last update.

FirestoreのバックアップをCloud Functionsで作成する

Posted at

Firestoreは何かあったときのために自分でバックアップとっておく必要があるっぽい。
FirestoreのフルバックアップをGCSに保存する用のCloud Functions(Python)を作成する。

ソース

ランタイム環境変数として以下2つを設定

  • projectid:プロジェクトIDをセット
  • gcs_bucket:「gs://バケット名」の形でセット
main.py
from google.cloud import firestore_admin_v1
import os

def firestore_backup(request):

  projectid=os.environ['projectid']
  gcs_bucket=os.environ['gcs_bucket']
  request_body={"name":"projects/{}/databases/(default)".format(projectid),"output_uri_prefix":gcs_bucket}

  firestore = firestore_admin_v1.FirestoreAdminClient()
  export_return = firestore.export_documents(request=request_body)

  return 'OK'
requirements.txt
# Function dependencies, for example:
# package>=version
google-cloud-firestore == 2.0.2 

あとはCloud Schedulerから起動設定してあげれば良い

実行すると、以下のような感じでバックアップフォルダが作成され、そこの下にExportファイル、メタデータが作成される
image.png

注意点

バックアップ保存先のバケットのリージョン

Firestoreのデータベースのリージョンと同じリージョンのバケットにしか保存できないっぽい

参考

Google Cloud Client Libraries for google-cloud-firestore
googleapis/python-firestore
GCPドキュメント

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0