Back up Firestore tables to cloud bucket
This commit is contained in:
parent
793a2c6d6c
commit
7534847e8d
39
functions/src/backup-db.ts
Normal file
39
functions/src/backup-db.ts
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
// Export script from https://firebase.google.com/docs/firestore/solutions/schedule-export
|
||||||
|
// To import the data into dev Firestore: https://firebase.google.com/docs/firestore/manage-data/move-data
|
||||||
|
|
||||||
|
import * as functions from 'firebase-functions'
|
||||||
|
import * as firestore from '@google-cloud/firestore'
|
||||||
|
const client = new firestore.v1.FirestoreAdminClient()
|
||||||
|
|
||||||
|
const bucket = 'gs://manifold-firestore-backup'
|
||||||
|
|
||||||
|
export const backupDb = 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: [
|
||||||
|
'contracts',
|
||||||
|
'folds',
|
||||||
|
'private-users',
|
||||||
|
'stripe-transactions',
|
||||||
|
'users',
|
||||||
|
],
|
||||||
|
})
|
||||||
|
.then((responses) => {
|
||||||
|
const response = responses[0]
|
||||||
|
console.log(`Operation Name: ${response['name']}`)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err)
|
||||||
|
throw new Error('Export operation failed')
|
||||||
|
})
|
||||||
|
})
|
|
@ -14,3 +14,4 @@ export * from './create-fold'
|
||||||
export * from './unsubscribe'
|
export * from './unsubscribe'
|
||||||
export * from './update-contract-metrics'
|
export * from './update-contract-metrics'
|
||||||
export * from './update-user-metrics'
|
export * from './update-user-metrics'
|
||||||
|
export * from './backup-db'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user