mcp-ssh-manager 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +87 -0
- package/.flake8 +15 -0
- package/.hooks-config.json +131 -0
- package/.pre-commit-config.yaml +105 -0
- package/CHANGELOG.md +137 -0
- package/CLAUDE.md +190 -0
- package/CONTRIBUTING.md +114 -0
- package/DESCRIPTION +1 -0
- package/INSTALLATION.md +180 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +77 -0
- package/README.md +718 -0
- package/cli/README.md +286 -0
- package/cli/build.sh +199 -0
- package/cli/commands/server.sh +284 -0
- package/cli/demo.sh +107 -0
- package/cli/install.sh +164 -0
- package/cli/lib/colors.sh +174 -0
- package/cli/lib/config.sh +284 -0
- package/cli/lib/menu.sh +430 -0
- package/cli/migrate.sh +168 -0
- package/cli/ssh-manager +525 -0
- package/docs/ALIASES_AND_HOOKS.md +285 -0
- package/docs/BACKUP_GUIDE.md +500 -0
- package/docs/DEPLOYMENT_GUIDE.md +241 -0
- package/docs/SEO.md +257 -0
- package/examples/backup-workflow.js +481 -0
- package/examples/claude-code-config.example.json +40 -0
- package/examples/codex-ssh-config.example.toml +71 -0
- package/examples/deploy-workflow.py +183 -0
- package/glama.json +6 -0
- package/package.json +85 -0
- package/profiles/README.md +40 -0
- package/profiles/default.json +52 -0
- package/profiles/docker.json +62 -0
- package/profiles/frappe.json +107 -0
- package/profiles/nodejs.json +63 -0
- package/pyproject.toml +17 -0
- package/scripts/setup-hooks.sh +60 -0
- package/scripts/validate.sh +76 -0
- package/src/backup-manager.js +469 -0
- package/src/command-aliases.js +151 -0
- package/src/config-loader.js +281 -0
- package/src/database-manager.js +555 -0
- package/src/deploy-helper.js +176 -0
- package/src/health-monitor.js +428 -0
- package/src/hooks-system.js +327 -0
- package/src/index.js +4499 -0
- package/src/logger.js +276 -0
- package/src/profile-loader.js +187 -0
- package/src/server-aliases.js +150 -0
- package/src/server-groups.js +409 -0
- package/src/session-manager.js +410 -0
- package/src/ssh-key-manager.js +378 -0
- package/src/ssh-manager.js +435 -0
- package/src/tunnel-manager.js +576 -0
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Manager for MCP SSH Manager
|
|
3
|
+
* Provides database operations for MySQL, PostgreSQL, and MongoDB
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { logger } from './logger.js';
|
|
7
|
+
|
|
8
|
+
// Supported database types
|
|
9
|
+
export const DB_TYPES = {
|
|
10
|
+
MYSQL: 'mysql',
|
|
11
|
+
POSTGRESQL: 'postgresql',
|
|
12
|
+
MONGODB: 'mongodb'
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Default ports
|
|
16
|
+
export const DB_PORTS = {
|
|
17
|
+
mysql: 3306,
|
|
18
|
+
postgresql: 5432,
|
|
19
|
+
mongodb: 27017
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Build MySQL dump command
|
|
24
|
+
*/
|
|
25
|
+
export function buildMySQLDumpCommand(options) {
|
|
26
|
+
const {
|
|
27
|
+
database,
|
|
28
|
+
user,
|
|
29
|
+
password,
|
|
30
|
+
host = 'localhost',
|
|
31
|
+
port = 3306,
|
|
32
|
+
outputFile,
|
|
33
|
+
compress = true,
|
|
34
|
+
tables = null
|
|
35
|
+
} = options;
|
|
36
|
+
|
|
37
|
+
let command = 'mysqldump';
|
|
38
|
+
|
|
39
|
+
if (user) command += ` -u${user}`;
|
|
40
|
+
if (password) command += ` -p'${password}'`;
|
|
41
|
+
if (host) command += ` -h ${host}`;
|
|
42
|
+
if (port) command += ` -P ${port}`;
|
|
43
|
+
|
|
44
|
+
command += ' --single-transaction --routines --triggers';
|
|
45
|
+
command += ` ${database}`;
|
|
46
|
+
|
|
47
|
+
if (tables && Array.isArray(tables)) {
|
|
48
|
+
command += ` ${tables.join(' ')}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (compress) {
|
|
52
|
+
command += ` | gzip > "${outputFile}"`;
|
|
53
|
+
} else {
|
|
54
|
+
command += ` > "${outputFile}"`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return command;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Build PostgreSQL dump command
|
|
62
|
+
*/
|
|
63
|
+
export function buildPostgreSQLDumpCommand(options) {
|
|
64
|
+
const {
|
|
65
|
+
database,
|
|
66
|
+
user,
|
|
67
|
+
password,
|
|
68
|
+
host = 'localhost',
|
|
69
|
+
port = 5432,
|
|
70
|
+
outputFile,
|
|
71
|
+
compress = true,
|
|
72
|
+
tables = null
|
|
73
|
+
} = options;
|
|
74
|
+
|
|
75
|
+
let command = '';
|
|
76
|
+
if (password) {
|
|
77
|
+
command = `PGPASSWORD='${password}' `;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
command += 'pg_dump';
|
|
81
|
+
if (user) command += ` -U ${user}`;
|
|
82
|
+
if (host) command += ` -h ${host}`;
|
|
83
|
+
if (port) command += ` -p ${port}`;
|
|
84
|
+
command += ' --format=custom --clean --if-exists';
|
|
85
|
+
|
|
86
|
+
if (tables && Array.isArray(tables)) {
|
|
87
|
+
for (const table of tables) {
|
|
88
|
+
command += ` -t ${table}`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
command += ` ${database}`;
|
|
93
|
+
|
|
94
|
+
if (compress) {
|
|
95
|
+
command += ` | gzip > "${outputFile}"`;
|
|
96
|
+
} else {
|
|
97
|
+
command += ` > "${outputFile}"`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return command;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Build MongoDB dump command
|
|
105
|
+
*/
|
|
106
|
+
export function buildMongoDBDumpCommand(options) {
|
|
107
|
+
const {
|
|
108
|
+
database,
|
|
109
|
+
user,
|
|
110
|
+
password,
|
|
111
|
+
host = 'localhost',
|
|
112
|
+
port = 27017,
|
|
113
|
+
outputDir,
|
|
114
|
+
compress = true,
|
|
115
|
+
collections = null
|
|
116
|
+
} = options;
|
|
117
|
+
|
|
118
|
+
let command = 'mongodump';
|
|
119
|
+
if (host) command += ` --host ${host}`;
|
|
120
|
+
if (port) command += ` --port ${port}`;
|
|
121
|
+
if (user) command += ` --username ${user}`;
|
|
122
|
+
if (password) command += ` --password '${password}'`;
|
|
123
|
+
if (database) command += ` --db ${database}`;
|
|
124
|
+
|
|
125
|
+
if (collections && Array.isArray(collections)) {
|
|
126
|
+
for (const collection of collections) {
|
|
127
|
+
command += ` --collection ${collection}`;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
command += ` --out "${outputDir}"`;
|
|
132
|
+
|
|
133
|
+
if (compress) {
|
|
134
|
+
command += ` && tar -czf "${outputDir}.tar.gz" -C "$(dirname ${outputDir})" "$(basename ${outputDir})"`;
|
|
135
|
+
command += ` && rm -rf "${outputDir}"`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return command;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Build MySQL import command
|
|
143
|
+
*/
|
|
144
|
+
export function buildMySQLImportCommand(options) {
|
|
145
|
+
const {
|
|
146
|
+
database,
|
|
147
|
+
user,
|
|
148
|
+
password,
|
|
149
|
+
host = 'localhost',
|
|
150
|
+
port = 3306,
|
|
151
|
+
inputFile
|
|
152
|
+
} = options;
|
|
153
|
+
|
|
154
|
+
let command = '';
|
|
155
|
+
|
|
156
|
+
if (inputFile.endsWith('.gz')) {
|
|
157
|
+
command = `gunzip -c "${inputFile}" | `;
|
|
158
|
+
} else {
|
|
159
|
+
command = `cat "${inputFile}" | `;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
command += 'mysql';
|
|
163
|
+
if (user) command += ` -u${user}`;
|
|
164
|
+
if (password) command += ` -p'${password}'`;
|
|
165
|
+
if (host) command += ` -h ${host}`;
|
|
166
|
+
if (port) command += ` -P ${port}`;
|
|
167
|
+
command += ` ${database}`;
|
|
168
|
+
|
|
169
|
+
return command;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Build PostgreSQL import command
|
|
174
|
+
*/
|
|
175
|
+
export function buildPostgreSQLImportCommand(options) {
|
|
176
|
+
const {
|
|
177
|
+
database,
|
|
178
|
+
user,
|
|
179
|
+
password,
|
|
180
|
+
host = 'localhost',
|
|
181
|
+
port = 5432,
|
|
182
|
+
inputFile
|
|
183
|
+
} = options;
|
|
184
|
+
|
|
185
|
+
let command = '';
|
|
186
|
+
if (password) {
|
|
187
|
+
command = `PGPASSWORD='${password}' `;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
command += 'pg_restore';
|
|
191
|
+
if (user) command += ` -U ${user}`;
|
|
192
|
+
if (host) command += ` -h ${host}`;
|
|
193
|
+
if (port) command += ` -p ${port}`;
|
|
194
|
+
command += ' --clean --if-exists';
|
|
195
|
+
command += ` -d ${database}`;
|
|
196
|
+
|
|
197
|
+
if (inputFile.endsWith('.gz')) {
|
|
198
|
+
command = `gunzip -c "${inputFile}" | ${command}`;
|
|
199
|
+
} else {
|
|
200
|
+
command += ` "${inputFile}"`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return command;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Build MongoDB restore command
|
|
208
|
+
*/
|
|
209
|
+
export function buildMongoDBRestoreCommand(options) {
|
|
210
|
+
const {
|
|
211
|
+
database,
|
|
212
|
+
user,
|
|
213
|
+
password,
|
|
214
|
+
host = 'localhost',
|
|
215
|
+
port = 27017,
|
|
216
|
+
inputPath,
|
|
217
|
+
drop = true
|
|
218
|
+
} = options;
|
|
219
|
+
|
|
220
|
+
let command = '';
|
|
221
|
+
|
|
222
|
+
if (inputPath.endsWith('.tar.gz')) {
|
|
223
|
+
const extractDir = inputPath.replace('.tar.gz', '');
|
|
224
|
+
command = `tar -xzf "${inputPath}" -C "$(dirname ${inputPath})" && `;
|
|
225
|
+
command += 'mongorestore';
|
|
226
|
+
if (drop) command += ' --drop';
|
|
227
|
+
if (host) command += ` --host ${host}`;
|
|
228
|
+
if (port) command += ` --port ${port}`;
|
|
229
|
+
if (user) command += ` --username ${user}`;
|
|
230
|
+
if (password) command += ` --password '${password}'`;
|
|
231
|
+
command += ` "${extractDir}"`;
|
|
232
|
+
command += ` && rm -rf "${extractDir}"`;
|
|
233
|
+
} else {
|
|
234
|
+
command = 'mongorestore';
|
|
235
|
+
if (drop) command += ' --drop';
|
|
236
|
+
if (host) command += ` --host ${host}`;
|
|
237
|
+
if (port) command += ` --port ${port}`;
|
|
238
|
+
if (user) command += ` --username ${user}`;
|
|
239
|
+
if (password) command += ` --password '${password}'`;
|
|
240
|
+
command += ` "${inputPath}"`;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return command;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Build MySQL list databases command
|
|
248
|
+
*/
|
|
249
|
+
export function buildMySQLListDatabasesCommand(options) {
|
|
250
|
+
const { user, password, host = 'localhost', port = 3306 } = options;
|
|
251
|
+
|
|
252
|
+
let command = 'mysql';
|
|
253
|
+
if (user) command += ` -u${user}`;
|
|
254
|
+
if (password) command += ` -p'${password}'`;
|
|
255
|
+
if (host) command += ` -h ${host}`;
|
|
256
|
+
if (port) command += ` -P ${port}`;
|
|
257
|
+
command += ` -e "SHOW DATABASES;" | tail -n +2`;
|
|
258
|
+
|
|
259
|
+
return command;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Build MySQL list tables command
|
|
264
|
+
*/
|
|
265
|
+
export function buildMySQLListTablesCommand(options) {
|
|
266
|
+
const { database, user, password, host = 'localhost', port = 3306 } = options;
|
|
267
|
+
|
|
268
|
+
let command = 'mysql';
|
|
269
|
+
if (user) command += ` -u${user}`;
|
|
270
|
+
if (password) command += ` -p'${password}'`;
|
|
271
|
+
if (host) command += ` -h ${host}`;
|
|
272
|
+
if (port) command += ` -P ${port}`;
|
|
273
|
+
command += ` -e "USE ${database}; SHOW TABLES;" | tail -n +2`;
|
|
274
|
+
|
|
275
|
+
return command;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Build PostgreSQL list databases command
|
|
280
|
+
*/
|
|
281
|
+
export function buildPostgreSQLListDatabasesCommand(options) {
|
|
282
|
+
const { user, password, host = 'localhost', port = 5432 } = options;
|
|
283
|
+
|
|
284
|
+
let command = '';
|
|
285
|
+
if (password) {
|
|
286
|
+
command = `PGPASSWORD='${password}' `;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
command += 'psql';
|
|
290
|
+
if (user) command += ` -U ${user}`;
|
|
291
|
+
if (host) command += ` -h ${host}`;
|
|
292
|
+
if (port) command += ` -p ${port}`;
|
|
293
|
+
command += ` -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;" | sed '/^$/d' | sed 's/^[ \\t]*//'`;
|
|
294
|
+
|
|
295
|
+
return command;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Build PostgreSQL list tables command
|
|
300
|
+
*/
|
|
301
|
+
export function buildPostgreSQLListTablesCommand(options) {
|
|
302
|
+
const { database, user, password, host = 'localhost', port = 5432 } = options;
|
|
303
|
+
|
|
304
|
+
let command = '';
|
|
305
|
+
if (password) {
|
|
306
|
+
command = `PGPASSWORD='${password}' `;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
command += 'psql';
|
|
310
|
+
if (user) command += ` -U ${user}`;
|
|
311
|
+
if (host) command += ` -h ${host}`;
|
|
312
|
+
if (port) command += ` -p ${port}`;
|
|
313
|
+
command += ` -d ${database}`;
|
|
314
|
+
command += ` -t -c "SELECT tablename FROM pg_tables WHERE schemaname = 'public';" | sed '/^$/d' | sed 's/^[ \\t]*//'`;
|
|
315
|
+
|
|
316
|
+
return command;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Build MongoDB list databases command
|
|
321
|
+
*/
|
|
322
|
+
export function buildMongoDBListDatabasesCommand(options) {
|
|
323
|
+
const { user, password, host = 'localhost', port = 27017 } = options;
|
|
324
|
+
|
|
325
|
+
let command = 'mongo';
|
|
326
|
+
if (host) command += ` --host ${host}`;
|
|
327
|
+
if (port) command += ` --port ${port}`;
|
|
328
|
+
if (user) command += ` --username ${user}`;
|
|
329
|
+
if (password) command += ` --password '${password}'`;
|
|
330
|
+
command += ` --quiet --eval "db.adminCommand('listDatabases').databases.forEach(function(d){print(d.name)})"`;
|
|
331
|
+
|
|
332
|
+
return command;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Build MongoDB list collections command
|
|
337
|
+
*/
|
|
338
|
+
export function buildMongoDBListCollectionsCommand(options) {
|
|
339
|
+
const { database, user, password, host = 'localhost', port = 27017 } = options;
|
|
340
|
+
|
|
341
|
+
let command = 'mongo';
|
|
342
|
+
if (host) command += ` --host ${host}`;
|
|
343
|
+
if (port) command += ` --port ${port}`;
|
|
344
|
+
if (user) command += ` --username ${user}`;
|
|
345
|
+
if (password) command += ` --password '${password}'`;
|
|
346
|
+
command += ` ${database}`;
|
|
347
|
+
command += ` --quiet --eval "db.getCollectionNames().forEach(function(c){print(c)})"`;
|
|
348
|
+
|
|
349
|
+
return command;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Build MySQL query command (SELECT only)
|
|
354
|
+
*/
|
|
355
|
+
export function buildMySQLQueryCommand(options) {
|
|
356
|
+
const { database, query, user, password, host = 'localhost', port = 3306, format = 'json' } = options;
|
|
357
|
+
|
|
358
|
+
// Validate query is SELECT only
|
|
359
|
+
if (!isSafeQuery(query)) {
|
|
360
|
+
throw new Error('Only SELECT queries are allowed');
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
let command = 'mysql';
|
|
364
|
+
if (user) command += ` -u${user}`;
|
|
365
|
+
if (password) command += ` -p'${password}'`;
|
|
366
|
+
if (host) command += ` -h ${host}`;
|
|
367
|
+
if (port) command += ` -P ${port}`;
|
|
368
|
+
command += ` ${database}`;
|
|
369
|
+
|
|
370
|
+
if (format === 'json') {
|
|
371
|
+
// Use JSON output if MySQL 5.7.8+
|
|
372
|
+
command += ` -e "${query}" --batch --skip-column-names | awk 'BEGIN{print "["} {if(NR>1)print ","; printf "{\\"row\\":%d,\\"data\\":\\"%s\\"}", NR, $0} END{print "]"}'`;
|
|
373
|
+
} else {
|
|
374
|
+
command += ` -e "${query}"`;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return command;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Build PostgreSQL query command (SELECT only)
|
|
382
|
+
*/
|
|
383
|
+
export function buildPostgreSQLQueryCommand(options) {
|
|
384
|
+
const { database, query, user, password, host = 'localhost', port = 5432 } = options;
|
|
385
|
+
|
|
386
|
+
if (!isSafeQuery(query)) {
|
|
387
|
+
throw new Error('Only SELECT queries are allowed');
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
let command = '';
|
|
391
|
+
if (password) {
|
|
392
|
+
command = `PGPASSWORD='${password}' `;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
command += 'psql';
|
|
396
|
+
if (user) command += ` -U ${user}`;
|
|
397
|
+
if (host) command += ` -h ${host}`;
|
|
398
|
+
if (port) command += ` -p ${port}`;
|
|
399
|
+
command += ` -d ${database}`;
|
|
400
|
+
command += ` -c "${query}"`;
|
|
401
|
+
|
|
402
|
+
return command;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Build MongoDB query command
|
|
407
|
+
*/
|
|
408
|
+
export function buildMongoDBQueryCommand(options) {
|
|
409
|
+
const { database, collection, query, user, password, host = 'localhost', port = 27017 } = options;
|
|
410
|
+
|
|
411
|
+
let command = 'mongo';
|
|
412
|
+
if (host) command += ` --host ${host}`;
|
|
413
|
+
if (port) command += ` --port ${port}`;
|
|
414
|
+
if (user) command += ` --username ${user}`;
|
|
415
|
+
if (password) command += ` --password '${password}'`;
|
|
416
|
+
command += ` ${database}`;
|
|
417
|
+
command += ` --quiet --eval "db.${collection}.find(${query || '{}'}).forEach(printjson)"`;
|
|
418
|
+
|
|
419
|
+
return command;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Validate query is safe (SELECT only)
|
|
424
|
+
*/
|
|
425
|
+
export function isSafeQuery(query) {
|
|
426
|
+
const trimmedQuery = query.trim().toLowerCase();
|
|
427
|
+
|
|
428
|
+
// Must start with SELECT
|
|
429
|
+
if (!trimmedQuery.startsWith('select')) {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Block dangerous keywords
|
|
434
|
+
const dangerousKeywords = [
|
|
435
|
+
'insert', 'update', 'delete', 'drop', 'create', 'alter',
|
|
436
|
+
'truncate', 'grant', 'revoke', 'exec', 'execute'
|
|
437
|
+
];
|
|
438
|
+
|
|
439
|
+
for (const keyword of dangerousKeywords) {
|
|
440
|
+
if (trimmedQuery.includes(keyword)) {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Parse database list output
|
|
450
|
+
*/
|
|
451
|
+
export function parseDatabaseList(output, type) {
|
|
452
|
+
const lines = output.trim().split('\n').filter(l => l.trim());
|
|
453
|
+
|
|
454
|
+
// Filter out system databases
|
|
455
|
+
return lines.filter(db => {
|
|
456
|
+
const dbLower = db.toLowerCase();
|
|
457
|
+
if (type === DB_TYPES.MYSQL) {
|
|
458
|
+
return !['information_schema', 'performance_schema', 'mysql', 'sys'].includes(dbLower);
|
|
459
|
+
} else if (type === DB_TYPES.POSTGRESQL) {
|
|
460
|
+
return !['template0', 'template1', 'postgres'].includes(dbLower);
|
|
461
|
+
} else if (type === DB_TYPES.MONGODB) {
|
|
462
|
+
return !['admin', 'config', 'local'].includes(dbLower);
|
|
463
|
+
}
|
|
464
|
+
return true;
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Parse table/collection list output
|
|
470
|
+
*/
|
|
471
|
+
export function parseTableList(output) {
|
|
472
|
+
return output.trim().split('\n').filter(l => l.trim());
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Estimate dump size command
|
|
477
|
+
*/
|
|
478
|
+
export function buildEstimateSizeCommand(type, database, options = {}) {
|
|
479
|
+
const { user, password, host = 'localhost', port } = options;
|
|
480
|
+
|
|
481
|
+
switch (type) {
|
|
482
|
+
case DB_TYPES.MYSQL: {
|
|
483
|
+
let command = 'mysql';
|
|
484
|
+
if (user) command += ` -u${user}`;
|
|
485
|
+
if (password) command += ` -p'${password}'`;
|
|
486
|
+
if (host) command += ` -h ${host}`;
|
|
487
|
+
if (port) command += ` -P ${port}`;
|
|
488
|
+
command += ` -e "SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema='${database}';" | tail -n 1`;
|
|
489
|
+
return command;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
case DB_TYPES.POSTGRESQL: {
|
|
493
|
+
let command = '';
|
|
494
|
+
if (password) {
|
|
495
|
+
command = `PGPASSWORD='${password}' `;
|
|
496
|
+
}
|
|
497
|
+
command += 'psql';
|
|
498
|
+
if (user) command += ` -U ${user}`;
|
|
499
|
+
if (host) command += ` -h ${host}`;
|
|
500
|
+
if (port) command += ` -p ${port}`;
|
|
501
|
+
command += ` -d ${database}`;
|
|
502
|
+
command += ` -t -c "SELECT pg_database_size('${database}');" | sed 's/^[ \\t]*//'`;
|
|
503
|
+
return command;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
case DB_TYPES.MONGODB: {
|
|
507
|
+
let command = 'mongo';
|
|
508
|
+
if (host) command += ` --host ${host}`;
|
|
509
|
+
if (port) command += ` --port ${port}`;
|
|
510
|
+
if (user) command += ` --username ${user}`;
|
|
511
|
+
if (password) command += ` --password '${password}'`;
|
|
512
|
+
command += ` ${database}`;
|
|
513
|
+
command += ` --quiet --eval "db.stats().dataSize"`;
|
|
514
|
+
return command;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
default:
|
|
518
|
+
throw new Error(`Unknown database type: ${type}`);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Parse size output to bytes
|
|
524
|
+
*/
|
|
525
|
+
export function parseSize(output) {
|
|
526
|
+
const size = parseInt(output.trim());
|
|
527
|
+
return isNaN(size) ? 0 : size;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Format bytes to human readable
|
|
532
|
+
*/
|
|
533
|
+
export function formatBytes(bytes) {
|
|
534
|
+
if (bytes === 0) return '0 Bytes';
|
|
535
|
+
const k = 1024;
|
|
536
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
537
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
538
|
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Get database connection info
|
|
543
|
+
*/
|
|
544
|
+
export function getConnectionInfo(type, options) {
|
|
545
|
+
const { host = 'localhost', port, user, database } = options;
|
|
546
|
+
const defaultPort = DB_PORTS[type];
|
|
547
|
+
|
|
548
|
+
return {
|
|
549
|
+
type,
|
|
550
|
+
host,
|
|
551
|
+
port: port || defaultPort,
|
|
552
|
+
user: user || 'default',
|
|
553
|
+
database: database || 'all'
|
|
554
|
+
};
|
|
555
|
+
}
|