-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore-db.js
More file actions
42 lines (36 loc) · 1.15 KB
/
restore-db.js
File metadata and controls
42 lines (36 loc) · 1.15 KB
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
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
const possiblePaths = [
'C:\\Program Files\\MongoDB\\Server\\6.0\\bin',
'C:\\Program Files\\MongoDB\\Server\\5.0\\bin',
'C:\\Program Files\\MongoDB\\Server\\4.4\\bin',
'C:\\Program Files\\MongoDB\\Tools\\100\\bin',
'C:\\Program Files\\MongoDB\\Tools\\4.4\\bin'
];
let mongoRestorePath = null;
for (const binPath of possiblePaths) {
if (fs.existsSync(path.join(binPath, 'mongorestore.exe'))) {
mongoRestorePath = path.join(binPath, 'mongorestore.exe');
break;
}
}
if (!mongoRestorePath) {
console.error('mongorestore.exe not found. Please install MongoDB or provide the correct path.');
process.exit(1);
}
const backupPath = path.join(__dirname, 'backup');
const command = `"${mongoRestorePath}" "${backupPath}" --drop`;
console.log(`Running command: ${command}`);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Stdout: ${stdout}`);
console.log('Database restored successfully!');
});