-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (62 loc) · 1.25 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require("dotenv").config();
const minimist = require("minimist");
const fs = require("fs-extra");
const dayjs = require("dayjs");
const cron = require("node-cron");
const execa = require("execa");
var args = minimist(process.argv.slice(2), {
string: "type",
string: "username",
string: "password",
string: "dbname",
alias: {
user: "username",
pass: "password",
db: "dbname",
},
});
const database = {
username: args.username,
password: args.password,
database: args.dbname,
};
async function mysql() {
try {
const { stdout: mysqldump } = execa("mysqldump", [
"-u",
`${database.username}`,
`-p${database.password}`,
`${database.database}`,
]);
mysqldump.pipe(
fs.createWriteStream(
`backup-${database.database}-${dayjs(
new Date(),
"DD-MM-YYYY HH:mm:ss"
)}.sql`
)
);
console.log("Backup Completed");
} catch (e) {
console.log("Error");
}
}
async function getDb() {
switch (args.type) {
case "mysql":
mysql();
break;
default:
break;
}
}
async function backupInit() {
cron.schedule("* * * * *", () => {
getDb().catch((e) => {
console.log(e);
});
});
}
backupInit().catch((e) => {
console.log(e);
});