diff --git a/functions/src/backup-db.ts b/functions/src/backup-db.ts new file mode 100644 index 00000000..db2df27d --- /dev/null +++ b/functions/src/backup-db.ts @@ -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') + }) + }) diff --git a/functions/src/index.ts b/functions/src/index.ts index 4dfd1e57..f191a771 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -14,3 +14,4 @@ export * from './create-fold' export * from './unsubscribe' export * from './update-contract-metrics' export * from './update-user-metrics' +export * from './backup-db'