如何使用 cURL 从 Google Cloud Engine 备份 firebase 数据库?
How to take backup of a firebase database from Google Cloud Engine using cURL?
我在 Google 计算引擎上使用 cURL 备份我的 firebase 数据库。上周备份成功。两天前,我更改了我的数据库的安全性,只允许经过身份验证的用户读取或写入。
像这样
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
现在我的备份开始失败了。那么,如何在计算引擎中添加或授予用户以成功备份我的 firebase 数据库?谢谢
因为我启用了数据库的身份验证,所以我无法在不对服务器进行身份验证的情况下进行备份。
为了克服,我用了Firebase Admin SDK with a service account. After that I am able to access the Firebase database using the APIs
正如您在其他回答中提到的,您现在使用的是 Firebase Admin SDK,因此我将提供一个非常简单的应用程序来满足您的需求(下载您的数据库并将其保存为 JSON 文件)。
对于此脚本,我将假设您尝试执行的操作类似于:
curl https://YOUR-PROJECT.firebaseio.com/.json?format=export -o backup-20170606.json
如果有人需要它们,可以找到 Firebase Admin SDK 的设置说明 here。
剧本
var fs = require('fs'); // Node.js Internal Module
var path = require('path'); // Node.js Internal Module
var moment = require('moment'); // External module
var admin = require("firebase-admin");
admin.initializeApp(/* Service account details go here */);
// Change these as desired
var outFileName = 'backup_' + moment().format('YYYYMMDD_HH-mm-ss');
var outFileDir = '/tmp';
var outFilePath = path.join(outFileDir, outFileName);
var rootRef = admin.database().ref(); // Ref to database root
rootRef.once("value", function(snapshot){
var outText = JSON.stringify(snapshot.val());
fs.writeFile(outFilePath, outText, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved as '" + outFilePath + "'!");
});
});
注意: moment.js
不是明确要求的,它只是我经常使用的一个非常有用的模块。
需要考虑的事情
- 您应该分段下载数据库(参见
orderByKey()
)并将每个条目保存到缓冲区中,然后在下载完成后保存到磁盘。当前代码将与大型数据库斗争。
- 下载速度会很慢,具体取决于数据库大小。考虑将其并行化。
备选方案
您还可以使用 Firebase Tools 来 access/download/manipulate 您的数据库。
我在 Google 计算引擎上使用 cURL 备份我的 firebase 数据库。上周备份成功。两天前,我更改了我的数据库的安全性,只允许经过身份验证的用户读取或写入。 像这样
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
现在我的备份开始失败了。那么,如何在计算引擎中添加或授予用户以成功备份我的 firebase 数据库?谢谢
因为我启用了数据库的身份验证,所以我无法在不对服务器进行身份验证的情况下进行备份。
为了克服,我用了Firebase Admin SDK with a service account. After that I am able to access the Firebase database using the APIs
正如您在其他回答中提到的,您现在使用的是 Firebase Admin SDK,因此我将提供一个非常简单的应用程序来满足您的需求(下载您的数据库并将其保存为 JSON 文件)。
对于此脚本,我将假设您尝试执行的操作类似于:
curl https://YOUR-PROJECT.firebaseio.com/.json?format=export -o backup-20170606.json
如果有人需要它们,可以找到 Firebase Admin SDK 的设置说明 here。
剧本
var fs = require('fs'); // Node.js Internal Module
var path = require('path'); // Node.js Internal Module
var moment = require('moment'); // External module
var admin = require("firebase-admin");
admin.initializeApp(/* Service account details go here */);
// Change these as desired
var outFileName = 'backup_' + moment().format('YYYYMMDD_HH-mm-ss');
var outFileDir = '/tmp';
var outFilePath = path.join(outFileDir, outFileName);
var rootRef = admin.database().ref(); // Ref to database root
rootRef.once("value", function(snapshot){
var outText = JSON.stringify(snapshot.val());
fs.writeFile(outFilePath, outText, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved as '" + outFilePath + "'!");
});
});
注意: moment.js
不是明确要求的,它只是我经常使用的一个非常有用的模块。
需要考虑的事情
- 您应该分段下载数据库(参见
orderByKey()
)并将每个条目保存到缓冲区中,然后在下载完成后保存到磁盘。当前代码将与大型数据库斗争。 - 下载速度会很慢,具体取决于数据库大小。考虑将其并行化。
备选方案
您还可以使用 Firebase Tools 来 access/download/manipulate 您的数据库。