forked from tohfos/HelpDesk-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.js
65 lines (54 loc) · 2.29 KB
/
restore.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
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const mongoURI = process.env.DB_URL; // Update with your MongoDB URI
const BACKUP_DIR = path.join(__dirname, 'backups'); // Update with your backup directory
async function restoreLatestBackup() {
try {
// Read all files in the backup directory
const files = fs.readdirSync(BACKUP_DIR);
// Filter out non-backup files
const backupFiles = files.filter((file) => {
// Modify the condition to match your backup file naming pattern
return file.startsWith('backup');
});
// Sort the backup files based on their timestamps
// Example of backup file names:
backupFiles.sort((a, b) => {
const timestampA = Date.parse(a.replace('backup_', '').replace(/-/g, '/').replace(/,/g, '').replace(/_/g, ':'));
const timestampB = Date.parse(b.replace('backup_', '').replace(/-/g, '/').replace(/,/g, '').replace(/_/g, ':'));
return timestampB - timestampA;
});
console.log(backupFiles);
if (backupFiles.length > 0) {
let latestBackup = path.join(BACKUP_DIR, backupFiles[0]);
console.log(latestBackup);
const backupWithHelpDesk = path.join(latestBackup, 'HelpDesk'); // Append 'HelpDesk' to the latest backup path
console.log("latestBackup", latestBackup)
const child = spawn('mongorestore', [
`--uri=${mongoURI}`, // Use --drop to drop the existing database before restoring
`--gzip`,
`${backupWithHelpDesk}`, // Specify the path to the latest backup file
]);
child.stdout.on('data', (data) => {
console.log('stdout:\n', data);
});
child.stderr.on('data', (data) => {
console.log('stderr:\n', Buffer.from(data).toString());
});
child.on('error', (error) => {
console.log('error:\n', error);
});
child.on('exit', (code, signal) => {
if (code) console.log('Process exit with code:', code);
else if (signal) console.log('Process killed with signal:', signal);
else console.log('Restore is successful ✅');
});
} else {
console.log('No backup files found.');
}
} catch (error) {
console.error('Error:', error);
}
}
module.exports = restoreLatestBackup; // Export the function to perform the restore