-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupDb.ts
29 lines (24 loc) · 947 Bytes
/
backupDb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import * as functions from 'firebase-functions';
import firestore from '@google-cloud/firestore';
import { settings } from '../../utils/settings';
const client = new firestore.v1.FirestoreAdminClient();
export const backupDb = functions
.region(settings.functions.region)
.pubsub.schedule('every 24 hours')
.onRun(async () => {
console.log('backupDb: starting DB backup...');
const projectId = process.env.GCP_PROJECT || (process.env.GCLOUD_PROJECT as string);
const databaseName = client.databasePath(projectId, '(default)');
try {
await client.exportDocuments({
name: databaseName,
outputUriPrefix: settings.backups.bucket,
// collectionIds empty to export all collections
collectionIds: [],
});
} catch (error) {
console.error(error);
throw new Error('backupDb: DB backup operation failed.');
}
console.log('backupDb: DB backup successful.');
});