@nka212bg/backend-utils 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/misc.js +3 -2
- package/os.js +50 -1
- package/package.json +2 -1
package/misc.js
CHANGED
|
@@ -238,7 +238,8 @@ exports.deleteRecursive = (path, pathTemp) => {
|
|
|
238
238
|
if (pathTemp) this.deleteRecursive(pathTemp);
|
|
239
239
|
};
|
|
240
240
|
|
|
241
|
-
exports.
|
|
241
|
+
exports.copyAny = (source, destination) => {
|
|
242
|
+
if (lstatSync(source).isFile()) return copyFileSync(source, destination);
|
|
242
243
|
if (!existsSync(destination)) mkdirSync(destination, { recursive: true });
|
|
243
244
|
|
|
244
245
|
readdirSync(source).forEach((item) => {
|
|
@@ -246,7 +247,7 @@ exports.copyRecursive = (source, destination) => {
|
|
|
246
247
|
const destinationPath = `${destination}/${item}`;
|
|
247
248
|
|
|
248
249
|
if (lstatSync(sourcePath).isDirectory()) {
|
|
249
|
-
this.
|
|
250
|
+
this.copyAny(sourcePath, destinationPath);
|
|
250
251
|
} else {
|
|
251
252
|
copyFileSync(sourcePath, destinationPath);
|
|
252
253
|
}
|
package/os.js
CHANGED
|
@@ -1,12 +1,61 @@
|
|
|
1
|
+
const { execSync } = require("child_process");
|
|
1
2
|
const si = require("systeminformation");
|
|
2
3
|
const osUtils = require("os-utils");
|
|
3
4
|
const os = require("os");
|
|
4
|
-
const { statfsSync } = require("fs");
|
|
5
|
+
const { statfsSync, readdirSync, existsSync, rmSync, lstatSync, mkdirSync } = require("fs");
|
|
5
6
|
const { shortCount, secToTime } = require("./misc");
|
|
7
|
+
const AdmZip = require("adm-zip");
|
|
6
8
|
|
|
7
9
|
let cpuUsage, temperature;
|
|
8
10
|
const serverStart = Date.now();
|
|
9
11
|
|
|
12
|
+
exports.restartApp = ({ service, password }) => {
|
|
13
|
+
try {
|
|
14
|
+
if (password) password = `echo ${password} | sudo -p ${password} `;
|
|
15
|
+
const execRes = execSync(`${password || ""} systemctl restart ${service}`);
|
|
16
|
+
return String(execRes);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return String(error);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
exports.rebootServer = ({ password } = {}) => {
|
|
23
|
+
try {
|
|
24
|
+
if (password) password = `echo ${password} | sudo -p ${password} `;
|
|
25
|
+
const execRes = execSync(`${password || ""} reboot`);
|
|
26
|
+
return String(execRes);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
return String(error);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
exports.backupSystem = async ({ basePath, archiveName, overrideExisting, items } = {}) => {
|
|
33
|
+
try {
|
|
34
|
+
const startTime = Date.now();
|
|
35
|
+
|
|
36
|
+
basePath = basePath || `${__basedir}/backups`;
|
|
37
|
+
archiveName = archiveName || new Date().toISOString().split("T")[0];
|
|
38
|
+
const fullPath = `${basePath}/${archiveName}`;
|
|
39
|
+
!existsSync(logDir) && mkdirSync(basePath, { recursive: true });
|
|
40
|
+
|
|
41
|
+
const availableBackups = readdirSync(basePath);
|
|
42
|
+
if (availableBackups.length > 2) rmSync(`${basePath}/${availableBackups[0]}`, { recursive: true, force: true });
|
|
43
|
+
|
|
44
|
+
if (!overrideExisting && existsSync(`${fullPath}.zip`)) return console.log("backupSystem info, file awready exists --> ", `${fullPath}.zip`);
|
|
45
|
+
|
|
46
|
+
if (!items) throw "No items";
|
|
47
|
+
if (typeof items == "string") items = [items];
|
|
48
|
+
|
|
49
|
+
const zip = new AdmZip();
|
|
50
|
+
items.forEach((e) => (lstatSync(e).isFile() ? zip.addLocalFile(e) : zip.addLocalFolder(e)));
|
|
51
|
+
zip.writeZip(`${fullPath}.zip`);
|
|
52
|
+
|
|
53
|
+
console.info(`System backup --> time: ${parseInt((Date.now() - startTime) / 1000)}`);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.error("backupSystem error --> ", error);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
10
59
|
exports.osStatus = () => {
|
|
11
60
|
const now = Date.now();
|
|
12
61
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nka212bg/backend-utils",
|
|
3
3
|
"author": "nka212bg",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@maxmind/geoip2-node": "^5.0.0",
|
|
8
8
|
"systeminformation": "^5.21.24",
|
|
9
|
+
"adm-zip": "^0.5.16",
|
|
9
10
|
"nodemailer": "^6.9.14",
|
|
10
11
|
"os-utils": "^0.0.14",
|
|
11
12
|
"stripe": "^14.23.0",
|