-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.js
110 lines (105 loc) · 3.1 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const {Command, flags} = require('@oclif/command');
const spawn = require('child_process').spawn;
class RestoreCommand extends Command {
async restoreCollection() {
const {flags} = this.parse(RestoreCommand);
const mongoOptions = [
'--host', flags.host,
'--port', flags.port,
'--db', flags.db,
'--collection', flags.collection,
'--file', flags.archive,
'--jsonArray', '--jsonArray',
];
if (flags.username) {
mongoOptions.push(
'--username', flags.username,
'--password', flags.password,
'--authenticationDatabase', flags.authenticationDatabase
);
}
if (flags.ssl) {
mongoOptions.push('--ssl', flags.ssl)
}
this.log('Starting mongorestore of ' + flags.archive + ' in ' + flags.db + '.' + flags.collection);
const mongorestore = spawn('mongoimport', mongoOptions);
return new Promise((resolve, reject) => {
let stoutContent = '', stdoutChunks = [];
let stderrContent = '', stderrChunks = [];
mongorestore.stdout.on('data', (data) => {
stdoutChunks = stdoutChunks.concat(data);
});
mongorestore.stdout.on('end', () => {
stoutContent = Buffer.concat(stdoutChunks).toString();
});
mongorestore.stderr.on('data', (data) => {
stderrChunks = stderrChunks.concat(data);
});
mongorestore.stderr.on('end', () => {
stderrContent = Buffer.concat(stderrChunks).toString();
});
mongorestore.on('exit', (code) => {
if (code === 0) {
resolve(stoutContent);
} else {
reject(stderrContent);
}
});
});
}
async run() {
let res;
try {
res = await this.restoreCollection();
this.log(res);
} catch (e) {
this.error(e);
}
}
}
RestoreCommand.description = 'Restores the specified MongoDB archive'
RestoreCommand.usage = `restore [OPTIONS]`;
RestoreCommand.examples = [`mongodb-utils restore --host localhost --port 27017 --db local --collection employees --archive backups/archive.json`];
RestoreCommand.flags = {
host: flags.string({
description: 'Mongo host',
required: true,
}),
port: flags.string({
description: 'Mongo port',
required: true,
}),
db: flags.string({
description: 'Mongo database name',
required: true,
}),
collection: flags.string({
description: 'Mongo collection name',
required: true,
}),
username: flags.string({
description: 'Mongo user',
required: false,
dependsOn: ['password', 'authenticationDatabase'],
}),
password: flags.string({
description: 'Mongo user password',
required: false,
dependsOn: ['username', 'authenticationDatabase'],
}),
ssl: flags.boolean({
description: 'Use SSL for Mongo connection',
required: false,
default: false,
}),
authenticationDatabase: flags.string({
description: 'Authentication database where the specified username exists',
required: false,
dependsOn: ['username', 'password'],
}),
archive: flags.string({
description: 'Archive file to restore',
required: true,
})
}
module.exports = RestoreCommand