Last active
September 1, 2023 17:14
-
-
Save JaredEzz/d074107cb4ae8990314702c5b3ee7e3b to your computer and use it in GitHub Desktop.
firestore-backup v2 cloud function
This file contains 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('@google-cloud/functions-framework'); | |
const firestore = require('@google-cloud/firestore'); | |
const client = new firestore.v1.FirestoreAdminClient(); | |
const bucket = 'gs://[bucket-name]' | |
// Register a CloudEvent callback with the Functions Framework that will | |
// be executed when the Pub/Sub trigger topic receives a message. | |
functions.cloudEvent('firestore-backup', cloudEvent => { | |
const databaseName = client.databasePath( | |
"[project-id]", | |
'(default)' | |
); | |
return client | |
.exportDocuments({ | |
name: databaseName, | |
outputUriPrefix: bucket, | |
// Leave collectionIds empty to export all collections | |
// or define a list of collection IDs: | |
// collectionIds: ['users', 'posts'] | |
collectionIds: [], | |
}) | |
.then(responses => { | |
const response = responses[0]; | |
console.log(`Operation Name: ${response['name']}`); | |
return response; | |
}) | |
.catch(err => { | |
console.error(err); | |
}); | |
}); |
This file contains 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
{ | |
"dependencies": { | |
"@google-cloud/functions-framework": "^3.0.0", | |
"@google-cloud/firestore": "^1.3.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment