-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.js
43 lines (34 loc) · 1.39 KB
/
dump.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
const { exec } = require('child_process');
const config = require('config');
var query = '{ "_id": { "$gt": ' + dateToId(config.start) + ', "$lte": ' + dateToId(config.end) + ' }}';
var command = "mongodump --db " + config.db + " --query '" + query + "'";
if(config.port) command += ' --port ' + config.port;
if(config.host) command += ' --host ' + config.host;
if(config.username && config.password && config.authenticationDatabase) {
command += ' --username ' + config.username + ' --password ' + config.password + ' --authenticationDatabase ' + config.authenticationDatabase;
}
for(let i=0, x=config.collections.length; i<x; i++) {
let fullCommand = command + ' --collection ' + config.collections[i];
runCommand(fullCommand);
}
function runCommand(command) {
exec(command, (err, stdout, stderr) => {
if (err) {
console.log(err);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
// Convert a date into an ObjectId string
// Accepts both Date object and string input
function dateToId(timestamp) {
// Convert string date to Date object (otherwise assume timestamp is a date)
if (typeof(timestamp) == 'string') {
timestamp = new Date(timestamp);
}
// Convert date object to hex seconds since Unix epoch
let hexSeconds = Math.floor(timestamp / 1000).toString(16);
return 'ObjectId("' + hexSeconds + '0000000000000000")';
}