Skip to content

Instantly share code, notes, and snippets.

@JaredEzz
Last active September 1, 2023 17:14
Show Gist options
  • Save JaredEzz/d074107cb4ae8990314702c5b3ee7e3b to your computer and use it in GitHub Desktop.
Save JaredEzz/d074107cb4ae8990314702c5b3ee7e3b to your computer and use it in GitHub Desktop.
firestore-backup v2 cloud function
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);
});
});
{
"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