Created
March 21, 2021 10:10
-
-
Save gunantosteven/36c96295b2799f866a0a0e241ed492fb to your computer and use it in GitHub Desktop.
Firebase Cloud Backup Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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