diff --git a/backup/erpbackup.js b/backup/erpbackup.js new file mode 100644 index 0000000..4708343 --- /dev/null +++ b/backup/erpbackup.js @@ -0,0 +1,43 @@ +import { spawn } from "child_process"; +// import { error } from "console"; +import path from "path"; +// import { exit } from "process"; +import cron from "node-cron"; + +const databasename = "test"; +// in future it will be replaced by the actual databasename + +const archivedPath = path.join(path.resolve(), "..", `${databasename}.gzip`); + +// console.log(archivedPath); + +// erpBackup(); + +function erpBackup() { + const childProcess1 = spawn("mongodump", [ + `--db=${databasename}`, + `--archive=${archivedPath}`, + "--gzip", + ]); + childProcess1.stdout.on("data", (data) => { + console.log("stdout:\n", Buffer.from(data).toString()); + }); + childProcess1.stderr.on("data", (data) => { + console.log("stderr:\n", Buffer.from(data).toString()); + }); + childProcess1.on("error", (error) => { + console.log(error); + }); + childProcess1.on("exit", (code, signal) => { + if (code) { + console.log("process ends with a code:", code); + } else if (signal) { + console.log("process ends with a signal:", signal); + } else { + console.log("process ends successfully"); + } + }); +} +// backup after every 24 hour +// cron.schedule("0 0 * * *", () => erpBackup()); +cron.schedule("*/10 * * * * *", () => erpBackup()); diff --git a/backup/erprestore.js b/backup/erprestore.js new file mode 100644 index 0000000..467d887 --- /dev/null +++ b/backup/erprestore.js @@ -0,0 +1,41 @@ +import { spawn } from "child_process"; +// import { error } from "console"; +import path from "path"; +// import { exit } from "process"; + +const databasename = "test"; +// in future it will be replaced by the actual databasename + +const archivedPath = path.join(path.resolve(), "..", `${databasename}.gzip`); + +// console.log(archivedPath); + +// erpBackup(); + +function erpRestore() { + const childProcess1 = spawn("mongorestore", [ + `--db=${databasename}`, + `--archive=${archivedPath}`, + "--gzip", + ]); + childProcess1.stdout.on("data", (data) => { + console.log("stdout:\n", Buffer.from(data).toString()); + }); + childProcess1.stderr.on("data", (data) => { + console.log("stderr:\n", Buffer.from(data).toString()); + }); + childProcess1.on("error", (error) => { + console.log(error); + }); + childProcess1.on("exit", (code, signal) => { + if (code) { + console.log("process ends with a code:", code); + } else if (signal) { + console.log("process ends with a signal:", signal); + } else { + console.log("Database Restored successfully"); + } + }); +} + +erpRestore(); diff --git a/package-lock.json b/package-lock.json index 0a8ea29..89d50b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "jsonwebtoken": "^9.0.0", "mongoose": "^6.9.0", "morgan": "~1.9.1", + "node-cron": "^3.0.2", "nodemailer": "^6.9.1", "supertest": "^6.3.3", "winston": "^3.8.2", @@ -7943,6 +7944,17 @@ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-5.1.0.tgz", "integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA==" }, + "node_modules/node-cron": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", + "integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/node-fetch": { "version": "2.6.12", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz", @@ -9979,7 +9991,6 @@ "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "optional": true, "bin": { "uuid": "dist/bin/uuid" } diff --git a/package.json b/package.json index 50a0e9f..0c6a050 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,9 @@ "testWin": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest", "testWin:watch": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --watch", "testWin:openHandles": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --detectOpenHandles", - "eslint": "eslint" + "eslint": "eslint", + "backup": "node ./backup/erpbackup", + "restore":"node ./backup/erprestore" }, "dependencies": { "apidoc": "^1.1.0", @@ -39,6 +41,7 @@ "jsonwebtoken": "^9.0.0", "mongoose": "^6.9.0", "morgan": "~1.9.1", + "node-cron": "^3.0.2", "nodemailer": "^6.9.1", "supertest": "^6.3.3", "winston": "^3.8.2",