Skip to content

Commit

Permalink
Firestore backup (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzulabo authored Jan 4, 2022
1 parent cdd3c12 commit b7912f7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
3 changes: 2 additions & 1 deletion scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const entries: ScriptEntries = [
],
['firebase.start', RunP(['functions.build.watch', 'firebase.serve'])],
['firebase.docs', Cmd('docsify serve docs', 'firebase')],
['firebase.shell', Cmd('firebase functions:shell', 'firebase')],

[
'firebase.build',
Expand Down Expand Up @@ -74,7 +75,7 @@ const entries: ScriptEntries = [

// secrets
['secrets.copy', secrets.copy],
['secrets.pack', secrets.pack],
['secrets.pack', RunS([secrets.pack, Cmd('git commit -a -m "Update"', 'secrets')])],
['secrets.unpack', secrets.unpack],

// dev-proxy
Expand Down
8 changes: 8 additions & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
immediateNotificationWriteHandler,
} from './firestore';
import { httpsRequestHandler } from './https';
import { pubsubBackupFirestore } from './pubsub/backup-firestore';
import { pubsubExternalAnnounceFetch } from './pubsub/external-announce-fetch';
import { pubsubSendNotification } from './pubsub/send-notification';

Expand Down Expand Up @@ -57,4 +58,11 @@ export const pubsub = {
await pubsubExternalAnnounceFetch(msg, context, adminApp);
return 0;
}),
backupFirestore: region.pubsub
.schedule(appEnv.firestoreBackup.schedule)
.timeZone(appEnv.firestoreBackup.timeZone)
.onRun(async context => {
await pubsubBackupFirestore(context, adminApp);
return 0;
}),
};
26 changes: 26 additions & 0 deletions src/functions/pubsub/backup-firestore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import admin from 'firebase-admin';
import { AppEnv, AppError } from '../../shared';
import { EventContext, FirebaseAdminApp } from '../firebase';
import { logger } from '../utils/logger';

const appEnv = new AppEnv().env;

export const pubsubBackupFirestore = async (_context: EventContext, adminApp: FirebaseAdminApp) => {
const projectID = adminApp.options.projectId;
if (!projectID) {
throw new AppError('missing projectID');
}

const prefix = appEnv.firestoreBackup.bucketPrefix();

const client = new admin.firestore.v1.FirestoreAdminClient();
const databaseName = client.databasePath(projectID, '(default)');

logger.info('exportDocuments', { prefix });

return client.exportDocuments({
name: databaseName,
outputUriPrefix: prefix,
collectionIds: [],
});
};
13 changes: 9 additions & 4 deletions src/shared/appenv.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { _appEnv } from '../../secrets/appenv.env';

interface AppEnvironment {
firebaseConfig: {
readonly firebaseConfig: {
readonly apiKey: string;
readonly authDomain: string;
readonly projectId: string;
readonly storageBucket: string;
readonly messagingSenderId: string;
readonly appId: string;
};
readonly firestoreBackup: {
readonly bucketPrefix: () => string;
readonly schedule: string;
readonly timeZone: string;
};
readonly functionsRegion: string;
readonly vapidKey: string;
readonly contact: string;
sites: {
console: string;
client: string;
docs: string;
readonly console: string;
readonly client: string;
readonly docs: string;
};
}

Expand Down

0 comments on commit b7912f7

Please sign in to comment.