Search code examples
firebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-storagegoogle-cloud-pubsub

Schedule firebase auth:export to bucket using pubsub


I'm trying to schedule a firebase auth:export into a bucket using pubsub. My purpose is to have a backup of auth (the output of firebase auth:export is perfectly fine for my purposes) every day.

This is the pubsub I tried:

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();
        });
    });

but I'm getting following error:

/bin/sh: 1: firebase: not found

I'm assuming this is because I cannot run command line scripts in whatever environment the pubsub is run.

Any other ways to get backup of firebase auth using Google Cloud API’s or firebase would be welcome.


Solution

  • I'm assuming this is because I cannot run command line scripts in whatever environment the pubsub is run.

    Indeed you cannot execute command line scripts (neither Firebase CLI commands nor gsutil ones) in a Cloud Function, which is the "environment" where your code is ran (here Pub/Sub is the mechanism that triggers the Cloud Function).


    On the other hand, since "the Firebase CLI can also be used programmatically as a standard Node module", as explained here, you can execute some commands of the CLI via a Cloud Function.

    Note that the word "some" above is in bold, because, as explained in this same Github page:

    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.

    This is exactly the case with the auth:export command which "exports the active project's user accounts to a JSON or CSV file".

    So, unfortunately, it is not possible to automate the execution of this command via a Cloud Function.


    Any other ways to get backup of firebase auth using Google Cloud API’s or firebase would be welcome.

    One way is to use the Admin SDK: you can 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) or from a server you own running Node.js, Java, Python, etc.