由于我们可以在 Firebase RTDB 控制台下载 json 文件,是否有任何方法可以导出火灾恢复集合/文档数据的 json 文件?
我的主要目标之一是在更新文档之前/之后对数据进行比较。
没有,您需要创建自己的流程,比如查询一个集合并循环遍历所有内容。
截至2018年8月7日,我们确实有一个 有管理的出口制度,它允许您将数据转储到一个 GCS 桶中。虽然这不是 JSON,但它是一种与 Cloud Datastore 使用的格式相同的格式,因此 BigQuery 能够理解它。这意味着你可以然后 导入到 BigQuery 中。
火灾恢复仍然是在其早期的发展,所以请检查 备份文件的任何信息有关的火灾恢复。
我发现这个 npm 包 Node-firestor- 备份非常简单和有用。
请注意,--accountCredentials path/to/credentials/file.json引用的是一个服务帐户密钥 json 文件,您可以按照 https://developers.google.com/identity/protocols/application-default-credentials的说明获得该文件。
--accountCredentials path/to/credentials/file.json
转到 API 控制台凭据页。 从项目下拉列表中,选择您的项目。 在“凭据”页上,选择“创建凭据”下拉列表,然后选择“服务帐户”键。 从“服务帐户”下拉列表中,选择一个现有的服务帐户或创建一个新的服务帐户。 对于 Key type,选择 JSON Key 选项,然后选择 Create。 把 * 。您刚刚下载的 json 文件位于您选择的目录中。这个目录必须是私有的(你不能让任何人访问它) ,但是可以访问你的 web 服务器代码。
我刚刚为火灾恢复编写了一个备份和恢复程序。你可以在我的 GitHub 上试一试。
Https://github.com/dalenguyen/firestore-backup-restore
谢谢,
我已经编写了一个工具,它可以遍历数据库的集合/文档,并将所有内容导出到一个 json 文件中。另外,它还将导入相同的结构(有助于克隆/移动 Firestore 数据库)。由于我有一些同事使用这些代码,所以我认为应该将其作为 NPM 包发布。请随意尝试并给出一些反馈。
Https://www.npmjs.com/package/node-firestore-import-export
如果有人需要使用 巨蟒2或 3的解决方案。
编辑: 请注意,这不备份规则
转到 https://github.com/RobinManoli/python-firebase-admin-firestore-backup频道
首先安装和设置 Firebase 管理员 Python SDK: https://firebase.google.com/docs/admin/setup
然后将其安装到 python 环境中:
pip install firebase-admin
安装火灾恢复模块:
pip install google-cloud-core pip install google-cloud-firestore
(来自 重要错误: 未能为 Python 导入云火灾恢复库)
巨蟒代 a
# -*- coding: UTF-8 -*- import firebase_admin from firebase_admin import credentials, firestore import json cred = credentials.Certificate('xxxxx-adminsdk-xxxxx-xxxxxxx.json') # from firebase project settings default_app = firebase_admin.initialize_app(cred, { 'databaseURL' : 'https://xxxxx.firebaseio.com' }) db = firebase_admin.firestore.client() # add your collections manually collection_names = ['myFirstCollection', 'mySecondCollection'] collections = dict() dict4json = dict() n_documents = 0 for collection in collection_names: collections[collection] = db.collection(collection).get() dict4json[collection] = {} for document in collections[collection]: docdict = document.to_dict() dict4json[collection][document.id] = docdict n_documents += 1 jsonfromdict = json.dumps(dict4json) path_filename = "/mypath/databases/firestore.json" print "Downloaded %d collections, %d documents and now writing %d json characters to %s" % ( len(collection_names), n_documents, len(jsonfromdict), path_filename ) with open(path_filename, 'w') as the_file: the_file.write(jsonfromdict)
npm i firebase-admin
const firebase = require('firebase-admin'); var serviceAccountSource = require("./source.json"); var serviceAccountDestination = require("./destination.json"); const sourceAdmin = firebase.initializeApp({ credential: firebase.credential.cert(serviceAccountSource), databaseURL: "https://**********.firebaseio.com" // replace with source }); const destinationAdmin = firebase.initializeApp({ credential: firebase.credential.cert(serviceAccountDestination), databaseURL: "https://$$$$$.firebaseio.com" }, "destination"); const collections = [ "books", "authors", ...]; // replace with your collections var source = sourceAdmin.firestore(); var destination = destinationAdmin.firestore(); collections.forEach(colName => { source.collection(colName).get().then(function(querySnapshot) { querySnapshot.forEach(function(doc) { destination.collection(colName).doc(doc.id).set({...doc.data()}); }); }); });
谷歌让它变得更加困难,所以社区找到了一个变通方法。如果你已经安装了 npm,你可以这样做:
npm
npx -p node-firestore-import-export firestore-export -a credentials.json -b backup.json
npx -p node-firestore-import-export firestore-import -a credentials.json -b backup.json
来源
打开任何客户端应用程序(反应,角度等)。使用此代码任何地方记录控制台和复制
const products = await db .collection("collectionName") .where("time", ">", new Date("2020-09-01")) .get() const json = JSON.stringify(products.docs.map((doc) => ({ ...doc.data() }))) console.log(json)
对我有用。
我使用 云端功能将 Firestore 的所有数据导出为 JSON 格式,所使用的函数如下:
exports.exportFirestore2Json = functions.https.onRequest((request, response) => { db.collection("data").get().then(function(querySnapshot) { const orders = []; var order = null querySnapshot.forEach(doc => { order = doc.data(); orders.push(order); }); response.send(JSON.stringify(orders)) return true }) .catch(function(error) { console.error("Error adding document: ", error); return false }); })
然后,转到 https://your-project-id.cloudfunctions.net/exportFirestore2Json,你会看到这样的东西
有一个用于导出/导入火灾恢复的 npm
出口项目 Goto-> project tings-> Service account-> Generate new private key-> 另存为 exportedDB.json
要导入的项目 Goto-> project tings-> Service account-> Generate new private key-> 另存为 import dDB.json
从保存文件的文件夹中运行这两个命令
出口: Npx-p node-firestor- import-export-a export dDB.json-b backup.json
进口: Npx-p node-firestor- import-export-a import dDB.json-b backup.json
是的,你可以,你不需要开始在你的消防控制台计费。有一个伟大的 npm 包 https://www.npmjs.com/package/firestore-export-import与此你可以出口和进口火灾恢复收集和文件很容易。只要按照以下步骤:
- 拿你的服务帐户密钥 打开 Firebase 控制台 > 项目设置 > 服务帐户 > 生成新的私钥
用 serviceAccountKey.json 重命名下载的文件
- 现在创建一个新的文件夹和 index.js 文件。
- 把你的 servicekey.json 粘贴到这个文件夹
现在安装这个软件包
npm install firestore-export-import
或者
yarn add firestore-export-import
const { initializeApp} = require('firestore-export-import') const serviceAccount = require('./serviceAccountKey.json') const appName = '[DEFAULT]' initializeApp(serviceAccount, appName) const fs = require('fs'); const { backup } = require('firestore-export-import') //backup('collection name') backup('users').then((data) => { const json = JSON.stringify(data); //where collection.json is your output file name. fs.writeFile('collection.json', json, 'utf8',()=>{ console.log('done'); }) });
执行节点 index.js,您将看到一个新的 Collection.json 文件,其中包含您的集合和文档。如果它看起来有点凌乱漂亮的格式它在线与 Https://codebeautify.org/jsonviewer
这个 index.js 只是一个非常基本的配置,它导出包含所有内容的整个集合,阅读它们的文档,您可以进行查询等等!
const { initializeApp,restore } = require('firestore-export-import') const serviceAccount = require('./serviceAccountKey.json') const appName = '[DEFAULT]' initializeApp(serviceAccount, appName) restore('collection.json', { //where refs is an array of key items refs: ['users'], //autoParseDates to parse dates if documents have timestamps autoParseDates: true, },()=>{ console.log('done'); })
在执行之后,您应该会看到由集合用户填充的 firestore!
把 Json 从本地扔到 FirestoreDB:
从 firestoreDB 下载数据到本地:
要生成 credentials.json,请转到项目设置-> 服务帐户-> 生成私钥。