Skip to content

Commit bb66124

Browse files
author
vikrantsingh22
committed
feat:351-Backup-Restore-Operation-of-database
1 parent 2b96c47 commit bb66124

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

backup/erpbackup.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { spawn } from "child_process";
2+
// import { error } from "console";
3+
import path from "path";
4+
// import { exit } from "process";
5+
import cron from "node-cron";
6+
7+
const databasename = "test";
8+
// in future it will be replaced by the actual databasename
9+
10+
const archivedPath = path.join(path.resolve(), "..", `${databasename}.gzip`);
11+
12+
// console.log(archivedPath);
13+
14+
// erpBackup();
15+
16+
function erpBackup() {
17+
const childProcess1 = spawn("mongodump", [
18+
`--db=${databasename}`,
19+
`--archive=${archivedPath}`,
20+
"--gzip",
21+
]);
22+
childProcess1.stdout.on("data", (data) => {
23+
console.log("stdout:\n", Buffer.from(data).toString());
24+
});
25+
childProcess1.stderr.on("data", (data) => {
26+
console.log("stderr:\n", Buffer.from(data).toString());
27+
});
28+
childProcess1.on("error", (error) => {
29+
console.log(error);
30+
});
31+
childProcess1.on("exit", (code, signal) => {
32+
if (code) {
33+
console.log("process ends with a code:", code);
34+
} else if (signal) {
35+
console.log("process ends with a signal:", signal);
36+
} else {
37+
console.log("process ends successfully");
38+
}
39+
});
40+
}
41+
// backup after every 24 hour
42+
// cron.schedule("0 0 * * *", () => erpBackup());
43+
cron.schedule("*/10 * * * * *", () => erpBackup());

backup/erprestore.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { spawn } from "child_process";
2+
// import { error } from "console";
3+
import path from "path";
4+
// import { exit } from "process";
5+
6+
const databasename = "test";
7+
// in future it will be replaced by the actual databasename
8+
9+
const archivedPath = path.join(path.resolve(), "..", `${databasename}.gzip`);
10+
11+
// console.log(archivedPath);
12+
13+
// erpBackup();
14+
15+
function erpRestore() {
16+
const childProcess1 = spawn("mongorestore", [
17+
`--db=${databasename}`,
18+
`--archive=${archivedPath}`,
19+
"--gzip",
20+
]);
21+
childProcess1.stdout.on("data", (data) => {
22+
console.log("stdout:\n", Buffer.from(data).toString());
23+
});
24+
childProcess1.stderr.on("data", (data) => {
25+
console.log("stderr:\n", Buffer.from(data).toString());
26+
});
27+
childProcess1.on("error", (error) => {
28+
console.log(error);
29+
});
30+
childProcess1.on("exit", (code, signal) => {
31+
if (code) {
32+
console.log("process ends with a code:", code);
33+
} else if (signal) {
34+
console.log("process ends with a signal:", signal);
35+
} else {
36+
console.log("Database Restored successfully");
37+
}
38+
});
39+
}
40+
41+
erpRestore();

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"testWin": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest",
2626
"testWin:watch": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --watch",
2727
"testWin:openHandles": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --detectOpenHandles",
28-
"eslint": "eslint"
28+
"eslint": "eslint",
29+
"backup": "node ./backup/erpbackup",
30+
"restore":"node ./backup/erprestore"
2931
},
3032
"dependencies": {
3133
"apidoc": "^1.1.0",
@@ -39,6 +41,7 @@
3941
"jsonwebtoken": "^9.0.0",
4042
"mongoose": "^6.9.0",
4143
"morgan": "~1.9.1",
44+
"node-cron": "^3.0.2",
4245
"nodemailer": "^6.9.1",
4346
"supertest": "^6.3.3",
4447
"winston": "^3.8.2",

0 commit comments

Comments
 (0)