Created
January 25, 2022 01:55
-
-
Save xaphod/c356d907e92278538c4664fdd40c4fe1 to your computer and use it in GitHub Desktop.
Scheduled firestore backup
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 { storageBucket, projectId } = require('...'); | |
const client = new firestore.v1.FirestoreAdminClient(); | |
const firestoreBackup = functions | |
.runWith({ memory: '512MB', timeoutSeconds: 540 }) | |
.pubsub | |
.schedule('every 24 hours') | |
.onRun(() => { // param context is available | |
const databaseName = client.databasePath(projectId, '(default)'); | |
const time = new Date().getTime(); | |
return client.exportDocuments({ | |
name: databaseName, | |
outputUriPrefix: `${storageBucket}/backup/firestore/${time}`, | |
// 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]; | |
return console.log(`Operation Name: ${response.name}`); | |
}).catch((err) => { | |
console.error(err); | |
throw new Error('Export operation failed'); | |
}); | |
}); | |
module.exports = { firestoreBackup }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment