-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-db.js
53 lines (45 loc) · 1.45 KB
/
backup-db.js
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
const BashShell = require("./src/lib/BashShell.js");
const fs = require("fs");
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const mainServer = new BashShell("BackupDB");
let busy = false;
mainServer.handler = function(event) {
if (event.data) {
console.log(event.data);
if (event.data.includes("exported")) {
busy = false;
}
}
};
async function exportCollection(collection) {
const db = "vxsacademy";
const outPath = `./db-backup/${db}.${collection}.json`;
// use mongoexport to get data
busy = true;
mainServer.send(`mongoexport --collection=${collection} --db=${db} --out=${outPath}`);
while (busy) {
await wait(10);
}
// format data
let writtenData = fs.readFileSync(outPath).toString().split("\n");
for (let i = 0; i < writtenData.length; i++) {
if (writtenData[i].length > 0) {
let doc = JSON.parse(writtenData[i]);
delete doc._id;
writtenData[i] = JSON.stringify(doc);
}
}
let newData = writtenData.join(",\n");
newData = newData.slice(0, newData.length - 2);
// write formatted data
fs.writeFileSync(outPath, `[\n${newData}\n]`);
}
async function main() {
await exportCollection("discussions");
await exportCollection("programs");
await exportCollection("salts");
await exportCollection("users");
mainServer.kill();
console.log("Export Complete");
}
main();