Skip to content

Instantly share code, notes, and snippets.

@benomatis
Created May 30, 2023 20:08
Show Gist options
  • Save benomatis/3e8e9097b735ed39815d9919a5056c94 to your computer and use it in GitHub Desktop.
Save benomatis/3e8e9097b735ed39815d9919a5056c94 to your computer and use it in GitHub Desktop.
Firebase PubSub function to create a daily backup of all firestore collections, taken from https://firebase.google.com/docs/firestore/solutions/schedule-export
import functions from 'firebase-functions'
import firestore from '@google-cloud/firestore'
// noinspection JSUnresolvedVariable
const client = new firestore.v1.FirestoreAdminClient()
const bucket = 'gs://backup_firestore_daily'
export const scheduledFirestoreExport = functions.pubsub
.schedule('0 0 * * *')
.timeZone('America/Toronto')
.onRun(() => {
const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT
const databaseName = client.databasePath(projectId, '(default)')
return client.exportDocuments({
name: databaseName,
outputUriPrefix: bucket,
collectionIds: []
})
.then(responses => {
const response = responses[0]
console.log(`Operation Name: ${response['name']}`)
return 1
})
.catch(err => {
console.error(err)
throw new Error('Export operation failed')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment