node 操作 mongodb 数据库备份与还原-示例代码
config.js
module.exports = {
port: 80,
database: {
port: 27017,
ip: "127.0.0.1",
name: "xxxxxx-xx",
username: "",
password: "",
libPath: "/xxx/xxx/mongoDB/mongodbserver/bin", //绝对路径
// outPath: "" //绝对路径
},
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
utils.js
const fs = require('fs-extra');
const path = require('path');
// 数据库备份
const { exec } = require('child_process');
const mongodump = (database, callback) => {
let check = "";
database.outPath = database.outPath || path.join(__dirname, '../backup');
fs.ensureDir(database.outPath);
if (database.username && database.password) {
check = `-u ${database.username} -p ${database.password}`
}
let outPath = `${database.outPath}/${datetime.format(new Date(), 'YYYY-MM-DD+HH:mm:ss')}`;
fs.ensureDirSync(outPath);
let pwd = `mongodump -h ${database.ip} --port ${database.port} ${check} -d ${database.name} -o ${outPath}`
if (database.libPath) {
pwd = `cd ${database.libPath} && ./` + pwd
}
exec(pwd, (err, stdout, stderr) => {
console.log(err);
callback(err, stdout, stderr)
})
}
const mongorestore = (database, callback) => {
let check = ""
database.outPath = database.outPath || path.join(__dirname, '../backup');
fs.ensureDir(database.outPath);
if (database.username && database.password) {
check = `-u ${database.username} -p ${database.password}`
}
let pwd = `mongorestore -h ${database.ip} --port ${database.port} ${check} -d ${database.name} --drop ${database.outPath}/${database.pathName}/${database.name}`
if (database.libPath) {
pwd = `cd ${database.libPath} && ./` + pwd
}
exec(pwd, (err, stdout, stderr) => {
console.log(err);
callback(err, stdout, stderr)
})
}
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
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
routes.js
const express = require('express');
const router = express.Router();
const fs = require('fs-extra')
const path = require('path')
const { mongodump, mongorestore } = require('./utils');
const config = require(./config.js');
const datetime = require('silly-datetime');
const schedule = require('node-schedule');
const rule = new schedule.RecurrenceRule();
// rule.second = [0,10,20,30,40,50];//隔十秒
rule.hour = 23; rule.minute = 59; rule.second = 0;//每天23点55分执行
schedule.scheduleJob(rule, function () {
mongodump(config.database, (err) => {
if (err) {
console.log(datetime.format(new Date(), 'YYYY-MM-DD HH:mm:ss') + ":数据库备份失败!");
} else {
console.log(datetime.format(new Date(), 'YYYY-MM-DD HH:mm:ss') + ":数据库备份成功!");
}
})
});
// 备份数据
router.post('/data', (req, res, next) => {
let { mark } = req.body;
if (mark === 'backup') {
mongodump(config.database, (err) => {
if (err) {
res.json({ code: 500, result: false, message: "数据库备份失败!" });
} else {
res.json({ code: 200, result: true, message: "数据库备份成功!" });
}
})
} else if (mark === 'recovery') {
// config.database.pathName = name
mongorestore(config.database, (err) => {
if (err) {
res.json({ code: 500, result: false, message: "数据库还原失败!" });
} else {
res.json({ code: 200, result: true , message: "数据库还原成功!"});
}
})
} else {
res.json({ code: 500, result: false, message: 'mark参数不得为空!' });
}
});
// 读取备份文件夹
router.post('/list', (req, res, next) => {
fs.readdir(path.join(__dirname, `../../backup`), function (err, files) {
if (err) {
res.json({ code: 500, data: [], result: false, message: '操作失败!' });
} else {
res.json({ code: 200, data: files, result: true, message: '操作成功!' });
}
})
});
// 删除备份文件夹
router.post('/remove', (req, res, next) => {
let { name } = req.body;
fs.remove(path.join(__dirname, `../../backup/${name}`))
.then(() => {
res.json({ code: 200, result: true, message: name + '备份文件删除成功!' });
})
.catch(err => {
res.json({ code: 500, result: false, message: name + '备份文件删除失败!' });
})
});
module.exports = router;
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
68
69
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
68
69
上次更新: 2024/01/30, 00:35:17
- 01
- linux 在没有 sudo 权限下安装 Ollama 框架12-23
- 02
- Express 与 vue3 使用 sse 实现消息推送(长连接)12-20