-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.ts
87 lines (76 loc) · 2.23 KB
/
backup.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import * as util from "util";
import * as fs from "fs";
import * as serialize from "serialize-javascript";
import { getDb } from "./helpers";
import { CollectionReference, Query } from "@google-cloud/firestore";
const writeFile = util.promisify(fs.writeFile);
/* =======
* Helpers
* =======
*/
async function storeQueryResult(
name: string,
collection: CollectionReference
): Promise<void> {
let backup: any[] = [];
let curCollectionView: Query = collection;
while (true) {
const snapshot = await curCollectionView.get();
const fetchSize = snapshot.docs.length;
if (fetchSize === 0) break;
console.log(`Fetched ${fetchSize} from ${name}`);
const data = snapshot.docs.map(x =>
serialize({
id: x.id,
data: x.data()
})
);
backup = backup.concat(data);
const last = snapshot.docs[snapshot.docs.length - 1];
curCollectionView = collection.startAfter(last);
}
await writeFile("./backup." + name + ".serializedJs", backup.join("\n"));
console.log(`${backup.length} records written for ${name}.`);
}
async function storeAllQueries(collectionsToBackup: {
[collectionName: string]: CollectionReference;
}): Promise<void> {
await Promise.all(
Object.entries(collectionsToBackup).map(([name, collection]) =>
storeQueryResult(name, collection)
)
);
}
/* ======
* Script
* ======
*/
async function main() {
console.log("Backing up prod Member, Operation, and notificationHistory collections.");
console.log("Arguments: path_to_credentials_file");
const args = process.argv;
if (args.length < 3) {
console.warn(
"Invalid number of arguments. See usage information above. Exiting."
);
return;
}
const prodConfig = require("./firebase.prod.config.json");
const db = getDb(args[2], prodConfig.projectId);
const collectionsToBackup = {
operations: db.collection("operations"),
members: db.collection("members"),
notificationHistory: db.collection("notificationHistory")
};
try {
await storeAllQueries(collectionsToBackup);
console.info("Back up succeeded.");
process.exit(0);
} catch (exception) {
console.error("Back up failed.", exception);
process.exit(1);
}
}
main().then(() => {
process.exit(0);
});