Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gunantosteven/36c96295b2799f866a0a0e241ed492fb to your computer and use it in GitHub Desktop.
Save gunantosteven/36c96295b2799f866a0a0e241ed492fb to your computer and use it in GitHub Desktop.
Firebase Cloud Backup Function
const functions = require("firebase-functions");
const firestore = require("@google-cloud/firestore");
const client = new firestore.v1.FirestoreAdminClient();
// Replace BUCKET_NAME
const bucket = "gs://firestore_backups_ivadmin_app";
exports.scheduledFirestoreExport = functions.pubsub
.schedule("every 24 hours")
.onRun((context) => {
const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT;
const databaseName =
client.databasePath(projectId, "(default)");
return client.exportDocuments({
name: databaseName,
outputUriPrefix: bucket,
// Leave collectionIds empty to export all collections
// or set to a list of collection IDs to export,
// collectionIds: ['users', 'posts']
collectionIds: [],
})
.then((responses) => {
const response = responses[0];
console.log(`Operation Name: ${response["name"]}`);
return null;
})
.catch((err) => {
console.error(err);
throw new Error("Export operation failed");
});
});
// [END fs_schedule_export]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment