使用 pubsub 将 firebase auth:export 安排到存储桶
Schedule firebase auth:export to bucket using pubsub
我正在尝试使用 pubsub 将 firebase auth:export
安排到存储桶中。我的目的是每天备份 auth(firebase auth:export
的输出非常适合我的目的)。
这是我试过的发布订阅:
const functions = require('firebase-functions')
const exec = require("child_process").exec
const datetime = new Date();
const formattedDate = datetime.toISOString().slice(0,10)
const commandString = `firebase auth:export auth_export_${formattedDate}.json --format=JSON && \
gsutil -m cp -r auth_export_${formattedDate}.json gs://backup_firebase_auth_daily && \
rm auth_export_${formattedDate}.json`
exports.scheduledFirebaseAuthExport = functions.pubsub
.schedule('every 24 hours')
.onRun(() => {
return exec(commandString, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
process.exit();
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
process.exit();
return;
}
console.log(stdout);
process.exit();
});
});
但我收到以下错误:
/bin/sh: 1: firebase: not found
我假设这是因为我无法 运行 在 pubsub 的任何环境中使用命令行脚本 运行。
欢迎使用 Google Cloud API 或 firebase 获取 firebase 身份验证备份的任何其他方法。
I'm assuming this is because I cannot run command line scripts in
whatever environment the pubsub is run.
事实上,您不能在 Cloud Function 中执行命令行脚本(Firebase CLI 命令或 gsutil 命令),这是您的代码所在的“环境”运行(此处 Pub/Sub 是mechanism that triggers 云函数)。
另一方面,由于“Firebase CLI 也可以以编程方式用作标准 Node 模块”,如 here 所述,
您可以通过 Cloud Function 执行 CLI 的 一些 命令。
请注意,上面的“一些”一词是粗体的,因为,如同一 Github 页面中所述:
Note: when used in a limited environment like Cloud Functions, not all
firebase-tools
commands will work programmatically because they require
access to a local filesystem.
auth:export
命令就是这种情况,它“将活动项目的用户帐户 导出到 JSON 或 CSV 文件 ”。
因此,不幸的是,无法通过 Cloud Functions 自动执行此命令。
Any other ways to get backup of firebase auth using Google Cloud API’s
or firebase would be welcome.
一种方法是使用 Admin SDK:您可以 retrieve the entire list of users in batches and for example store it in a protected Firestore collection (or any other storage solution). This can be triggered from a Cloud Function (for example a scheduled Cloud Function) 或从您拥有的服务器 运行 Node.js、Java、Python等
firebase-tools
现在提供 auth.export()
功能。 See Repo
在此处查看云函数的示例实现:
Backup firebase users
import * as tools from 'firebase-tools'
// Export users to a temporary file
await tools.auth.export(path, { project: projectId })
我正在尝试使用 pubsub 将 firebase auth:export
安排到存储桶中。我的目的是每天备份 auth(firebase auth:export
的输出非常适合我的目的)。
这是我试过的发布订阅:
const functions = require('firebase-functions')
const exec = require("child_process").exec
const datetime = new Date();
const formattedDate = datetime.toISOString().slice(0,10)
const commandString = `firebase auth:export auth_export_${formattedDate}.json --format=JSON && \
gsutil -m cp -r auth_export_${formattedDate}.json gs://backup_firebase_auth_daily && \
rm auth_export_${formattedDate}.json`
exports.scheduledFirebaseAuthExport = functions.pubsub
.schedule('every 24 hours')
.onRun(() => {
return exec(commandString, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
process.exit();
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
process.exit();
return;
}
console.log(stdout);
process.exit();
});
});
但我收到以下错误:
/bin/sh: 1: firebase: not found
我假设这是因为我无法 运行 在 pubsub 的任何环境中使用命令行脚本 运行。
欢迎使用 Google Cloud API 或 firebase 获取 firebase 身份验证备份的任何其他方法。
I'm assuming this is because I cannot run command line scripts in whatever environment the pubsub is run.
事实上,您不能在 Cloud Function 中执行命令行脚本(Firebase CLI 命令或 gsutil 命令),这是您的代码所在的“环境”运行(此处 Pub/Sub 是mechanism that triggers 云函数)。
另一方面,由于“Firebase CLI 也可以以编程方式用作标准 Node 模块”,如 here 所述, 您可以通过 Cloud Function 执行 CLI 的 一些 命令。
请注意,上面的“一些”一词是粗体的,因为,如同一 Github 页面中所述:
Note: when used in a limited environment like Cloud Functions, not all
firebase-tools
commands will work programmatically because they require access to a local filesystem.
auth:export
命令就是这种情况,它“将活动项目的用户帐户 导出到 JSON 或 CSV 文件 ”。
因此,不幸的是,无法通过 Cloud Functions 自动执行此命令。
Any other ways to get backup of firebase auth using Google Cloud API’s or firebase would be welcome.
一种方法是使用 Admin SDK:您可以 retrieve the entire list of users in batches and for example store it in a protected Firestore collection (or any other storage solution). This can be triggered from a Cloud Function (for example a scheduled Cloud Function) 或从您拥有的服务器 运行 Node.js、Java、Python等
firebase-tools
现在提供 auth.export()
功能。 See Repo
在此处查看云函数的示例实现: Backup firebase users
import * as tools from 'firebase-tools'
// Export users to a temporary file
await tools.auth.export(path, { project: projectId })