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,76 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# Quick validation script to check code quality
|
|
4
|
+
# Can be run manually or in CI/CD
|
|
5
|
+
|
|
6
|
+
echo "🔍 MCP SSH Manager - Code Validation"
|
|
7
|
+
echo "====================================="
|
|
8
|
+
echo ""
|
|
9
|
+
|
|
10
|
+
ERRORS=0
|
|
11
|
+
|
|
12
|
+
# Check JavaScript syntax
|
|
13
|
+
echo "📋 Checking JavaScript syntax..."
|
|
14
|
+
if node --check src/index.js 2>/dev/null; then
|
|
15
|
+
echo " ✅ JavaScript syntax is valid"
|
|
16
|
+
else
|
|
17
|
+
echo " ❌ JavaScript syntax error!"
|
|
18
|
+
ERRORS=$((ERRORS + 1))
|
|
19
|
+
fi
|
|
20
|
+
|
|
21
|
+
# Check SSH Manager syntax
|
|
22
|
+
echo "📋 Checking SSH Manager syntax..."
|
|
23
|
+
if node --check src/ssh-manager.js 2>/dev/null; then
|
|
24
|
+
echo " ✅ SSH Manager syntax is valid"
|
|
25
|
+
else
|
|
26
|
+
echo " ❌ SSH Manager syntax error!"
|
|
27
|
+
ERRORS=$((ERRORS + 1))
|
|
28
|
+
fi
|
|
29
|
+
|
|
30
|
+
# Check for .env in git
|
|
31
|
+
echo "📋 Checking for sensitive files..."
|
|
32
|
+
if git ls-files | grep -q "^\.env$"; then
|
|
33
|
+
echo " ❌ WARNING: .env file is tracked in git!"
|
|
34
|
+
ERRORS=$((ERRORS + 1))
|
|
35
|
+
else
|
|
36
|
+
echo " ✅ No .env file in git"
|
|
37
|
+
fi
|
|
38
|
+
|
|
39
|
+
# Check if dependencies are installed
|
|
40
|
+
echo "📋 Checking dependencies..."
|
|
41
|
+
if [ -d "node_modules" ]; then
|
|
42
|
+
echo " ✅ Node modules installed"
|
|
43
|
+
else
|
|
44
|
+
echo " ⚠️ Node modules not installed (run: npm install)"
|
|
45
|
+
fi
|
|
46
|
+
|
|
47
|
+
# Test server startup (quick test)
|
|
48
|
+
echo "📋 Testing MCP server startup..."
|
|
49
|
+
# The server needs stdin, so we provide empty input and check if it starts
|
|
50
|
+
# Use a timeout alternative for macOS compatibility
|
|
51
|
+
( echo "" | node src/index.js 2>/dev/null 1>/dev/null ) &
|
|
52
|
+
PID=$!
|
|
53
|
+
sleep 2
|
|
54
|
+
if kill -0 $PID 2>/dev/null; then
|
|
55
|
+
kill $PID 2>/dev/null
|
|
56
|
+
echo " ✅ MCP server starts correctly"
|
|
57
|
+
else
|
|
58
|
+
wait $PID
|
|
59
|
+
EXIT_CODE=$?
|
|
60
|
+
if [ $EXIT_CODE -eq 0 ] || [ $EXIT_CODE -eq 143 ]; then
|
|
61
|
+
echo " ✅ MCP server syntax is valid"
|
|
62
|
+
else
|
|
63
|
+
echo " ❌ MCP server failed to start (exit code: $EXIT_CODE)"
|
|
64
|
+
ERRORS=$((ERRORS + 1))
|
|
65
|
+
fi
|
|
66
|
+
fi
|
|
67
|
+
|
|
68
|
+
echo ""
|
|
69
|
+
echo "====================================="
|
|
70
|
+
if [ $ERRORS -eq 0 ]; then
|
|
71
|
+
echo "✅ All checks passed!"
|
|
72
|
+
exit 0
|
|
73
|
+
else
|
|
74
|
+
echo "❌ Found $ERRORS error(s)"
|
|
75
|
+
exit 1
|
|
76
|
+
fi
|
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backup Manager for MCP SSH Manager
|
|
3
|
+
* Handles creation, listing, restoration, and scheduling of backups
|
|
4
|
+
* Supports databases (MySQL, PostgreSQL, MongoDB) and file backups
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import crypto from 'crypto';
|
|
9
|
+
import { logger } from './logger.js';
|
|
10
|
+
|
|
11
|
+
// Backup types
|
|
12
|
+
export const BACKUP_TYPES = {
|
|
13
|
+
MYSQL: 'mysql',
|
|
14
|
+
POSTGRESQL: 'postgresql',
|
|
15
|
+
MONGODB: 'mongodb',
|
|
16
|
+
FILES: 'files',
|
|
17
|
+
FULL: 'full'
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// Default backup directory
|
|
21
|
+
export const DEFAULT_BACKUP_DIR = '/var/backups/ssh-manager';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generate unique backup ID
|
|
25
|
+
*/
|
|
26
|
+
export function generateBackupId(type, name) {
|
|
27
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
28
|
+
const random = crypto.randomBytes(4).toString('hex');
|
|
29
|
+
return `${type}_${name}_${timestamp}_${random}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Get backup metadata file path
|
|
34
|
+
*/
|
|
35
|
+
export function getBackupMetadataPath(backupId, backupDir = DEFAULT_BACKUP_DIR) {
|
|
36
|
+
return path.join(backupDir, `${backupId}.meta.json`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Get backup file path
|
|
41
|
+
*/
|
|
42
|
+
export function getBackupFilePath(backupId, backupDir = DEFAULT_BACKUP_DIR, extension = '.gz') {
|
|
43
|
+
return path.join(backupDir, `${backupId}${extension}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Build MySQL dump command
|
|
48
|
+
*/
|
|
49
|
+
export function buildMySQLDumpCommand(options) {
|
|
50
|
+
const {
|
|
51
|
+
database,
|
|
52
|
+
user,
|
|
53
|
+
password,
|
|
54
|
+
host = 'localhost',
|
|
55
|
+
port = 3306,
|
|
56
|
+
outputFile,
|
|
57
|
+
singleTransaction = true,
|
|
58
|
+
compress = true
|
|
59
|
+
} = options;
|
|
60
|
+
|
|
61
|
+
let command = 'mysqldump';
|
|
62
|
+
|
|
63
|
+
// Connection parameters
|
|
64
|
+
if (user) command += ` -u${user}`;
|
|
65
|
+
if (password) command += ` -p'${password}'`;
|
|
66
|
+
if (host) command += ` -h ${host}`;
|
|
67
|
+
if (port) command += ` -P ${port}`;
|
|
68
|
+
|
|
69
|
+
// Dump options
|
|
70
|
+
if (singleTransaction) command += ' --single-transaction';
|
|
71
|
+
command += ' --routines --triggers';
|
|
72
|
+
|
|
73
|
+
// Database name
|
|
74
|
+
command += ` ${database}`;
|
|
75
|
+
|
|
76
|
+
// Output handling
|
|
77
|
+
if (compress) {
|
|
78
|
+
command += ` | gzip > "${outputFile}"`;
|
|
79
|
+
} else {
|
|
80
|
+
command += ` > "${outputFile}"`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return command;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Build PostgreSQL dump command
|
|
88
|
+
*/
|
|
89
|
+
export function buildPostgreSQLDumpCommand(options) {
|
|
90
|
+
const {
|
|
91
|
+
database,
|
|
92
|
+
user,
|
|
93
|
+
password,
|
|
94
|
+
host = 'localhost',
|
|
95
|
+
port = 5432,
|
|
96
|
+
outputFile,
|
|
97
|
+
compress = true
|
|
98
|
+
} = options;
|
|
99
|
+
|
|
100
|
+
// PostgreSQL uses PGPASSWORD environment variable
|
|
101
|
+
let command = '';
|
|
102
|
+
if (password) {
|
|
103
|
+
command = `PGPASSWORD='${password}' `;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
command += 'pg_dump';
|
|
107
|
+
|
|
108
|
+
// Connection parameters
|
|
109
|
+
if (user) command += ` -U ${user}`;
|
|
110
|
+
if (host) command += ` -h ${host}`;
|
|
111
|
+
if (port) command += ` -p ${port}`;
|
|
112
|
+
|
|
113
|
+
// Dump options
|
|
114
|
+
command += ' --format=custom --clean --if-exists';
|
|
115
|
+
|
|
116
|
+
// Database name
|
|
117
|
+
command += ` ${database}`;
|
|
118
|
+
|
|
119
|
+
// Output handling
|
|
120
|
+
if (compress) {
|
|
121
|
+
command += ` | gzip > "${outputFile}"`;
|
|
122
|
+
} else {
|
|
123
|
+
command += ` > "${outputFile}"`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return command;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Build MongoDB dump command
|
|
131
|
+
*/
|
|
132
|
+
export function buildMongoDBDumpCommand(options) {
|
|
133
|
+
const {
|
|
134
|
+
database,
|
|
135
|
+
user,
|
|
136
|
+
password,
|
|
137
|
+
host = 'localhost',
|
|
138
|
+
port = 27017,
|
|
139
|
+
outputDir,
|
|
140
|
+
compress = true
|
|
141
|
+
} = options;
|
|
142
|
+
|
|
143
|
+
let command = 'mongodump';
|
|
144
|
+
|
|
145
|
+
// Connection parameters
|
|
146
|
+
if (host) command += ` --host ${host}`;
|
|
147
|
+
if (port) command += ` --port ${port}`;
|
|
148
|
+
if (user) command += ` --username ${user}`;
|
|
149
|
+
if (password) command += ` --password '${password}'`;
|
|
150
|
+
|
|
151
|
+
// Database selection
|
|
152
|
+
if (database) command += ` --db ${database}`;
|
|
153
|
+
|
|
154
|
+
// Output directory
|
|
155
|
+
command += ` --out "${outputDir}"`;
|
|
156
|
+
|
|
157
|
+
// Compress the output directory
|
|
158
|
+
if (compress) {
|
|
159
|
+
const archiveName = `${outputDir}.tar.gz`;
|
|
160
|
+
command += ` && tar -czf "${archiveName}" -C "$(dirname ${outputDir})" "$(basename ${outputDir})"`;
|
|
161
|
+
command += ` && rm -rf "${outputDir}"`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return command;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Build files backup command (tar + gzip)
|
|
169
|
+
*/
|
|
170
|
+
export function buildFilesBackupCommand(options) {
|
|
171
|
+
const {
|
|
172
|
+
paths,
|
|
173
|
+
outputFile,
|
|
174
|
+
exclude = [],
|
|
175
|
+
compress = true
|
|
176
|
+
} = options;
|
|
177
|
+
|
|
178
|
+
if (!Array.isArray(paths) || paths.length === 0) {
|
|
179
|
+
throw new Error('paths must be a non-empty array');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let command = 'tar';
|
|
183
|
+
|
|
184
|
+
// Compression flag
|
|
185
|
+
if (compress) {
|
|
186
|
+
command += ' -czf';
|
|
187
|
+
} else {
|
|
188
|
+
command += ' -cf';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Output file
|
|
192
|
+
command += ` "${outputFile}"`;
|
|
193
|
+
|
|
194
|
+
// Exclude patterns
|
|
195
|
+
for (const pattern of exclude) {
|
|
196
|
+
command += ` --exclude="${pattern}"`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Paths to backup
|
|
200
|
+
command += ` ${paths.map(p => `"${p}"`).join(' ')}`;
|
|
201
|
+
|
|
202
|
+
return command;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Build backup restore command based on type
|
|
207
|
+
*/
|
|
208
|
+
export function buildRestoreCommand(backupType, backupFile, options = {}) {
|
|
209
|
+
switch (backupType) {
|
|
210
|
+
case BACKUP_TYPES.MYSQL:
|
|
211
|
+
return buildMySQLRestoreCommand(backupFile, options);
|
|
212
|
+
case BACKUP_TYPES.POSTGRESQL:
|
|
213
|
+
return buildPostgreSQLRestoreCommand(backupFile, options);
|
|
214
|
+
case BACKUP_TYPES.MONGODB:
|
|
215
|
+
return buildMongoDBRestoreCommand(backupFile, options);
|
|
216
|
+
case BACKUP_TYPES.FILES:
|
|
217
|
+
return buildFilesRestoreCommand(backupFile, options);
|
|
218
|
+
default:
|
|
219
|
+
throw new Error(`Unknown backup type: ${backupType}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Build MySQL restore command
|
|
225
|
+
*/
|
|
226
|
+
function buildMySQLRestoreCommand(backupFile, options) {
|
|
227
|
+
const {
|
|
228
|
+
database,
|
|
229
|
+
user,
|
|
230
|
+
password,
|
|
231
|
+
host = 'localhost',
|
|
232
|
+
port = 3306
|
|
233
|
+
} = options;
|
|
234
|
+
|
|
235
|
+
let command = '';
|
|
236
|
+
|
|
237
|
+
// Decompress if needed
|
|
238
|
+
if (backupFile.endsWith('.gz')) {
|
|
239
|
+
command = `gunzip -c "${backupFile}" | `;
|
|
240
|
+
} else {
|
|
241
|
+
command = `cat "${backupFile}" | `;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
command += 'mysql';
|
|
245
|
+
|
|
246
|
+
// Connection parameters
|
|
247
|
+
if (user) command += ` -u${user}`;
|
|
248
|
+
if (password) command += ` -p'${password}'`;
|
|
249
|
+
if (host) command += ` -h ${host}`;
|
|
250
|
+
if (port) command += ` -P ${port}`;
|
|
251
|
+
if (database) command += ` ${database}`;
|
|
252
|
+
|
|
253
|
+
return command;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Build PostgreSQL restore command
|
|
258
|
+
*/
|
|
259
|
+
function buildPostgreSQLRestoreCommand(backupFile, options) {
|
|
260
|
+
const {
|
|
261
|
+
database,
|
|
262
|
+
user,
|
|
263
|
+
password,
|
|
264
|
+
host = 'localhost',
|
|
265
|
+
port = 5432
|
|
266
|
+
} = options;
|
|
267
|
+
|
|
268
|
+
let command = '';
|
|
269
|
+
if (password) {
|
|
270
|
+
command = `PGPASSWORD='${password}' `;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
command += 'pg_restore';
|
|
274
|
+
|
|
275
|
+
// Connection parameters
|
|
276
|
+
if (user) command += ` -U ${user}`;
|
|
277
|
+
if (host) command += ` -h ${host}`;
|
|
278
|
+
if (port) command += ` -p ${port}`;
|
|
279
|
+
if (database) command += ` -d ${database}`;
|
|
280
|
+
|
|
281
|
+
// Restore options
|
|
282
|
+
command += ' --clean --if-exists';
|
|
283
|
+
|
|
284
|
+
// Handle compressed files
|
|
285
|
+
if (backupFile.endsWith('.gz')) {
|
|
286
|
+
command = `gunzip -c "${backupFile}" | ${command}`;
|
|
287
|
+
} else {
|
|
288
|
+
command += ` "${backupFile}"`;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return command;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Build MongoDB restore command
|
|
296
|
+
*/
|
|
297
|
+
function buildMongoDBRestoreCommand(backupFile, options) {
|
|
298
|
+
const {
|
|
299
|
+
database,
|
|
300
|
+
user,
|
|
301
|
+
password,
|
|
302
|
+
host = 'localhost',
|
|
303
|
+
port = 27017,
|
|
304
|
+
drop = true
|
|
305
|
+
} = options;
|
|
306
|
+
|
|
307
|
+
let command = '';
|
|
308
|
+
|
|
309
|
+
// Extract if compressed
|
|
310
|
+
if (backupFile.endsWith('.tar.gz')) {
|
|
311
|
+
const extractDir = backupFile.replace('.tar.gz', '');
|
|
312
|
+
command = `tar -xzf "${backupFile}" -C "$(dirname ${backupFile})" && `;
|
|
313
|
+
command += 'mongorestore';
|
|
314
|
+
|
|
315
|
+
if (drop) command += ' --drop';
|
|
316
|
+
if (host) command += ` --host ${host}`;
|
|
317
|
+
if (port) command += ` --port ${port}`;
|
|
318
|
+
if (user) command += ` --username ${user}`;
|
|
319
|
+
if (password) command += ` --password '${password}'`;
|
|
320
|
+
|
|
321
|
+
command += ` "${extractDir}"`;
|
|
322
|
+
command += ` && rm -rf "${extractDir}"`;
|
|
323
|
+
} else {
|
|
324
|
+
command = 'mongorestore';
|
|
325
|
+
if (drop) command += ' --drop';
|
|
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 += ` "${backupFile}"`;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return command;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Build files restore command
|
|
338
|
+
*/
|
|
339
|
+
function buildFilesRestoreCommand(backupFile, options) {
|
|
340
|
+
const { targetPath = '/' } = options;
|
|
341
|
+
|
|
342
|
+
let command = 'tar';
|
|
343
|
+
|
|
344
|
+
// Auto-detect compression
|
|
345
|
+
if (backupFile.endsWith('.gz') || backupFile.endsWith('.tgz')) {
|
|
346
|
+
command += ' -xzf';
|
|
347
|
+
} else {
|
|
348
|
+
command += ' -xf';
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
command += ` "${backupFile}"`;
|
|
352
|
+
command += ` -C "${targetPath}"`;
|
|
353
|
+
|
|
354
|
+
return command;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Create backup metadata object
|
|
359
|
+
*/
|
|
360
|
+
export function createBackupMetadata(backupId, type, options = {}) {
|
|
361
|
+
return {
|
|
362
|
+
id: backupId,
|
|
363
|
+
type,
|
|
364
|
+
created_at: new Date().toISOString(),
|
|
365
|
+
server: options.server || 'unknown',
|
|
366
|
+
database: options.database || null,
|
|
367
|
+
paths: options.paths || [],
|
|
368
|
+
size: null, // Will be filled after backup
|
|
369
|
+
compressed: options.compress !== false,
|
|
370
|
+
retention: options.retention || 7, // days
|
|
371
|
+
status: 'pending',
|
|
372
|
+
error: null
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Build command to save metadata to remote server
|
|
378
|
+
*/
|
|
379
|
+
export function buildSaveMetadataCommand(metadata, metadataPath) {
|
|
380
|
+
const jsonData = JSON.stringify(metadata, null, 2);
|
|
381
|
+
// Escape single quotes in JSON for shell
|
|
382
|
+
const escapedJson = jsonData.replace(/'/g, "'\\''");
|
|
383
|
+
return `echo '${escapedJson}' > "${metadataPath}"`;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Build command to list backups from remote server
|
|
388
|
+
*/
|
|
389
|
+
export function buildListBackupsCommand(backupDir = DEFAULT_BACKUP_DIR, type = null) {
|
|
390
|
+
let command = `find "${backupDir}" -name "*.meta.json" -type f`;
|
|
391
|
+
|
|
392
|
+
if (type) {
|
|
393
|
+
command += ` | grep "${type}_"`;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Read and parse each metadata file
|
|
397
|
+
command += ` | while read -r file; do cat "$file"; echo "---"; done`;
|
|
398
|
+
|
|
399
|
+
return command;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Parse list backups output
|
|
404
|
+
*/
|
|
405
|
+
export function parseBackupsList(output) {
|
|
406
|
+
if (!output || !output.trim()) {
|
|
407
|
+
return [];
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const backups = [];
|
|
411
|
+
const metadataBlocks = output.split('---').filter(b => b.trim());
|
|
412
|
+
|
|
413
|
+
for (const block of metadataBlocks) {
|
|
414
|
+
try {
|
|
415
|
+
const metadata = JSON.parse(block.trim());
|
|
416
|
+
backups.push(metadata);
|
|
417
|
+
} catch (error) {
|
|
418
|
+
logger.warn('Failed to parse backup metadata', { error: error.message, block });
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Sort by created_at descending
|
|
423
|
+
return backups.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Build cleanup old backups command (based on retention)
|
|
428
|
+
*/
|
|
429
|
+
export function buildCleanupCommand(backupDir = DEFAULT_BACKUP_DIR, retentionDays = 7) {
|
|
430
|
+
// Find backup files older than retention period and delete them
|
|
431
|
+
return `find "${backupDir}" -name "*_*_*" -type f -mtime +${retentionDays} -delete`;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Build cron schedule command
|
|
436
|
+
*/
|
|
437
|
+
export function buildCronScheduleCommand(schedule, backupCommand, cronComment) {
|
|
438
|
+
// Add cron job with comment
|
|
439
|
+
const cronLine = `${schedule} ${backupCommand} # ${cronComment}`;
|
|
440
|
+
return `(crontab -l 2>/dev/null; echo '${cronLine}') | crontab -`;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Parse cron list output
|
|
445
|
+
*/
|
|
446
|
+
export function parseCronJobs(output) {
|
|
447
|
+
if (!output || !output.trim()) {
|
|
448
|
+
return [];
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const jobs = [];
|
|
452
|
+
const lines = output.split('\n');
|
|
453
|
+
|
|
454
|
+
for (const line of lines) {
|
|
455
|
+
if (line.trim() && !line.startsWith('#') && line.includes('ssh-manager-backup')) {
|
|
456
|
+
const parts = line.split('#');
|
|
457
|
+
const schedule = parts[0].trim();
|
|
458
|
+
const comment = parts[1] ? parts[1].trim() : '';
|
|
459
|
+
|
|
460
|
+
jobs.push({
|
|
461
|
+
schedule,
|
|
462
|
+
comment,
|
|
463
|
+
command: schedule.split(/\s+/).slice(5).join(' ')
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return jobs;
|
|
469
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command Aliases System for MCP SSH Manager
|
|
3
|
+
* Provides shortcuts for frequently used commands
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import { fileURLToPath } from 'url';
|
|
9
|
+
import { loadProfile } from './profile-loader.js';
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = path.dirname(__filename);
|
|
13
|
+
|
|
14
|
+
const ALIASES_FILE = path.join(__dirname, '..', '.command-aliases.json');
|
|
15
|
+
|
|
16
|
+
// Get aliases from the active profile
|
|
17
|
+
let profileAliases = {};
|
|
18
|
+
try {
|
|
19
|
+
const profile = loadProfile();
|
|
20
|
+
profileAliases = profile.commandAliases || {};
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error(`Error loading profile aliases: ${error.message}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Load command aliases from file
|
|
27
|
+
*/
|
|
28
|
+
export function loadCommandAliases() {
|
|
29
|
+
try {
|
|
30
|
+
// Start with profile aliases
|
|
31
|
+
let aliases = { ...profileAliases };
|
|
32
|
+
|
|
33
|
+
// Merge with custom aliases from file
|
|
34
|
+
if (fs.existsSync(ALIASES_FILE)) {
|
|
35
|
+
const data = fs.readFileSync(ALIASES_FILE, 'utf8');
|
|
36
|
+
aliases = { ...aliases, ...JSON.parse(data) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return aliases;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error(`Error loading command aliases: ${error.message}`);
|
|
42
|
+
}
|
|
43
|
+
return profileAliases;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Save command aliases to file
|
|
48
|
+
*/
|
|
49
|
+
export function saveCommandAliases(aliases) {
|
|
50
|
+
try {
|
|
51
|
+
// Only save custom aliases (not from profile)
|
|
52
|
+
const customAliases = {};
|
|
53
|
+
for (const [key, value] of Object.entries(aliases)) {
|
|
54
|
+
if (profileAliases[key] !== value) {
|
|
55
|
+
customAliases[key] = value;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fs.writeFileSync(ALIASES_FILE, JSON.stringify(customAliases, null, 2));
|
|
60
|
+
return true;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error(`Error saving command aliases: ${error.message}`);
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Expand command if it's an alias
|
|
69
|
+
*/
|
|
70
|
+
export function expandCommandAlias(command) {
|
|
71
|
+
const aliases = loadCommandAliases();
|
|
72
|
+
|
|
73
|
+
// Check if the entire command is an alias
|
|
74
|
+
if (aliases[command]) {
|
|
75
|
+
return aliases[command];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Check if the command starts with an alias
|
|
79
|
+
const parts = command.split(' ');
|
|
80
|
+
const firstPart = parts[0];
|
|
81
|
+
|
|
82
|
+
if (aliases[firstPart]) {
|
|
83
|
+
parts[0] = aliases[firstPart];
|
|
84
|
+
return parts.join(' ');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return command;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Add or update a command alias
|
|
92
|
+
*/
|
|
93
|
+
export function addCommandAlias(alias, command) {
|
|
94
|
+
const aliases = loadCommandAliases();
|
|
95
|
+
aliases[alias] = command;
|
|
96
|
+
return saveCommandAliases(aliases);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Remove a command alias
|
|
101
|
+
*/
|
|
102
|
+
export function removeCommandAlias(alias) {
|
|
103
|
+
const aliases = loadCommandAliases();
|
|
104
|
+
|
|
105
|
+
// Don't remove profile aliases, just reset them
|
|
106
|
+
if (profileAliases[alias]) {
|
|
107
|
+
aliases[alias] = profileAliases[alias];
|
|
108
|
+
} else {
|
|
109
|
+
delete aliases[alias];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return saveCommandAliases(aliases);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* List all command aliases
|
|
117
|
+
*/
|
|
118
|
+
export function listCommandAliases() {
|
|
119
|
+
const aliases = loadCommandAliases();
|
|
120
|
+
const result = [];
|
|
121
|
+
|
|
122
|
+
for (const [alias, command] of Object.entries(aliases)) {
|
|
123
|
+
result.push({
|
|
124
|
+
alias,
|
|
125
|
+
command,
|
|
126
|
+
isFromProfile: profileAliases[alias] === command,
|
|
127
|
+
isCustom: profileAliases[alias] !== command
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return result.sort((a, b) => a.alias.localeCompare(b.alias));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get suggested aliases based on command
|
|
136
|
+
*/
|
|
137
|
+
export function suggestAliases(command) {
|
|
138
|
+
const suggestions = [];
|
|
139
|
+
const aliases = loadCommandAliases();
|
|
140
|
+
|
|
141
|
+
const commandLower = command.toLowerCase();
|
|
142
|
+
|
|
143
|
+
for (const [alias, aliasCommand] of Object.entries(aliases)) {
|
|
144
|
+
if (aliasCommand.toLowerCase().includes(commandLower) ||
|
|
145
|
+
alias.toLowerCase().includes(commandLower)) {
|
|
146
|
+
suggestions.push({ alias, command: aliasCommand });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return suggestions;
|
|
151
|
+
}
|