Skip to content

Commit

Permalink
Add optional configuration to backup database
Browse files Browse the repository at this point in the history
  • Loading branch information
sweinstein22 committed Jan 25, 2021
1 parent 71fccc3 commit f38d6d2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ Default is `America/Los_Angeles`.
This is the number of history entries Pairist stores per team. The default is `20`. The recommendation engine uses these to generate pairs based on pairing history.

_You probably won't ever need to change this!_

## `PAIRIST_BACKUP_BUCKET_NAME` (optional)

This is the Cloud Storage bucket a backup of your Firestore database will be stored in. If configured, a backup will be taken every 24 hours.

As described in the [Firebase docs about scheduling data exports](https://firebase.google.com/docs/firestore/solutions/schedule-export), this value should be in the format "gs://BUCKET_NAME".
36 changes: 36 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,39 @@ export { saveHistory } from './saveHistory';
export { updateTeamSettings } from './updateTeamSettings';
export { updateUserProfile } from './updateUserProfile';
export { verifyNewUser } from './verifyNewUser';

// [START fs_schedule_export]
const functions = require('firebase-functions');
const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient();

const bucket = functions.config().pairist.backup_bucket_name;

if (bucket) {
exports.scheduledFirestoreExport = functions.pubsub
.schedule('every 24 hours')
.onRun((context: any) => {

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: []
})
.then((responses: any) => {
const response = responses[0];
console.log(`Operation Name: ${response['name']}`);
})
.catch((err: any) => {
console.error(err);
throw new Error('Export operation failed');
});
});
};
// [END fs_schedule_export]
3 changes: 2 additions & 1 deletion scripts/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ firebase functions:config:set \
pairist.allowed_email_domains=${PAIRIST_ALLOWED_EMAIL_DOMAINS} \
pairist.history_cron_schedule=${PAIRIST_HISTORY_CRON_SCHEDULE} \
pairist.history_cron_timezone=${PAIRIST_HISTORY_CRON_TIMEZONE} \
pairist.history_entries_to_keep=${PAIRIST_HISTORY_ENTRIES_TO_KEEP}
pairist.history_entries_to_keep=${PAIRIST_HISTORY_ENTRIES_TO_KEEP} \
pairist.backup_bucket_name=${PAIRIST_BACKUP_BUCKET_NAME}

# Deploy functions, hosting, database
REACT_APP_PAIRIST_VERSION=$(node -p "require('./package').version") \
Expand Down

0 comments on commit f38d6d2

Please sign in to comment.