-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbackup-workflow.js
More file actions
481 lines (414 loc) · 11.8 KB
/
backup-workflow.js
File metadata and controls
481 lines (414 loc) · 11.8 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* Backup Workflow Examples for MCP SSH Manager
*
* This file demonstrates various backup and restore workflows
* that can be executed through Claude Code or OpenAI Codex.
*/
// ============================================================================
// EXAMPLE 1: Simple MySQL Backup Before Deployment
// ============================================================================
/*
User: "Backup production database before deployment"
AI executes:
*/
const mysqlBackup = {
tool: 'ssh_backup_create',
params: {
server: 'production',
type: 'mysql',
name: 'pre-deployment',
database: 'myapp_prod',
dbUser: 'backup_user',
dbPassword: process.env.DB_PASSWORD, // From environment
retention: 7 // Keep for 7 days
}
};
// Response:
// {
// "success": true,
// "backup_id": "mysql_pre-deployment_2025-10-01T10-30-45-000Z_abc123de",
// "size_human": "50.00 MB",
// "location": "/var/backups/ssh-manager/mysql_pre-deployment_2025-10-01T10-30-45-000Z_abc123de.gz"
// }
// ============================================================================
// EXAMPLE 2: PostgreSQL Backup with Custom Retention
// ============================================================================
/*
User: "Create PostgreSQL backup and keep it for 30 days"
*/
const postgresBackup = {
tool: 'ssh_backup_create',
params: {
server: 'staging',
type: 'postgresql',
name: 'monthly-backup',
database: 'analytics_db',
dbUser: 'postgres',
dbPassword: process.env.PG_PASSWORD,
retention: 30, // Keep for 30 days
compress: true
}
};
// ============================================================================
// EXAMPLE 3: Files Backup with Exclusions
// ============================================================================
/*
User: "Backup website files excluding cache and logs"
*/
const filesBackup = {
tool: 'ssh_backup_create',
params: {
server: 'web01',
type: 'files',
name: 'website-files',
paths: [
'/var/www/html',
'/etc/nginx',
'/home/deploy/config'
],
exclude: [
'cache/*',
'*.log',
'node_modules/',
'*.tmp'
],
retention: 14
}
};
// ============================================================================
// EXAMPLE 4: MongoDB Backup
// ============================================================================
/*
User: "Backup MongoDB user database"
*/
const mongoBackup = {
tool: 'ssh_backup_create',
params: {
server: 'mongodb01',
type: 'mongodb',
name: 'users-db',
database: 'users',
dbHost: 'localhost',
dbPort: 27017,
dbUser: 'admin',
dbPassword: process.env.MONGO_PASSWORD,
retention: 7
}
};
// ============================================================================
// EXAMPLE 5: List All Backups
// ============================================================================
/*
User: "Show me all MySQL backups on production"
*/
const listBackups = {
tool: 'ssh_backup_list',
params: {
server: 'production',
type: 'mysql' // Optional: filter by type
}
};
// Response:
// {
// "success": true,
// "count": 5,
// "backups": [
// {
// "id": "mysql_pre-deployment_2025-10-01T10-30-45-000Z_abc123de",
// "type": "mysql",
// "created_at": "2025-10-01T10:30:45.000Z",
// "database": "myapp_prod",
// "size_human": "50.00 MB",
// "retention_days": 7
// }
// ]
// }
// ============================================================================
// EXAMPLE 6: Restore from Backup
// ============================================================================
/*
User: "Restore the latest production backup"
*/
// Step 1: List backups to find the ID
const listFirst = {
tool: 'ssh_backup_list',
params: {
server: 'production',
type: 'mysql'
}
};
// Step 2: Restore using the backup ID
const restoreBackup = {
tool: 'ssh_backup_restore',
params: {
server: 'production',
backupId: 'mysql_pre-deployment_2025-10-01T10-30-45-000Z_abc123de',
database: 'myapp_prod', // Can restore to different database
dbUser: 'restore_user',
dbPassword: process.env.DB_PASSWORD
}
};
// ============================================================================
// EXAMPLE 7: Schedule Daily Backups
// ============================================================================
/*
User: "Schedule daily MySQL backup at 2 AM"
*/
const scheduleDaily = {
tool: 'ssh_backup_schedule',
params: {
server: 'production',
schedule: '0 2 * * *', // Daily at 2 AM
type: 'mysql',
name: 'daily-prod',
database: 'myapp_prod',
retention: 7 // Keep last 7 days
}
};
// ============================================================================
// EXAMPLE 8: Schedule Weekly Full Backup
// ============================================================================
/*
User: "Schedule weekly full backup every Sunday at midnight"
*/
const scheduleWeekly = {
tool: 'ssh_backup_schedule',
params: {
server: 'production',
schedule: '0 0 * * 0', // Sunday at midnight
type: 'mysql',
name: 'weekly-full',
database: 'myapp_prod',
retention: 30 // Keep for 4 weeks
}
};
// ============================================================================
// EXAMPLE 9: Multi-Server Backup Strategy
// ============================================================================
/*
User: "Backup all production databases"
*/
// Production MySQL
const prodMysql = {
tool: 'ssh_backup_create',
params: {
server: 'prod-mysql',
type: 'mysql',
name: 'prod-mysql-backup',
database: 'main_db'
}
};
// Production PostgreSQL
const prodPostgres = {
tool: 'ssh_backup_create',
params: {
server: 'prod-postgres',
type: 'postgresql',
name: 'prod-pg-backup',
database: 'analytics'
}
};
// Production MongoDB
const prodMongo = {
tool: 'ssh_backup_create',
params: {
server: 'prod-mongo',
type: 'mongodb',
name: 'prod-mongo-backup',
database: 'sessions'
}
};
// ============================================================================
// EXAMPLE 10: Pre-Deployment Workflow
// ============================================================================
/*
Complete deployment workflow with backup safety net
*/
async function preDeploymentWorkflow() {
// Step 1: Create backup
console.log("Creating pre-deployment backup...");
const backup = await createBackup({
server: 'production',
type: 'mysql',
name: 'pre-deploy',
database: 'myapp_prod'
});
console.log(`Backup created: ${backup.backup_id}`);
// Step 2: Deploy changes
console.log("Deploying new version...");
await deploy({
server: 'production',
branch: 'main'
});
// Step 3: Run health check
console.log("Running health checks...");
const health = await healthCheck({
server: 'production'
});
// Step 4: If deployment fails, restore backup
if (!health.success) {
console.error("Deployment failed! Rolling back...");
await restoreBackup({
server: 'production',
backupId: backup.backup_id
});
console.log("Rollback completed");
} else {
console.log("Deployment successful!");
}
}
// ============================================================================
// EXAMPLE 11: Disaster Recovery Workflow
// ============================================================================
/*
User: "Recover production database from yesterday's backup"
*/
async function disasterRecovery() {
// Step 1: List all backups
const backups = await listBackups({
server: 'production',
type: 'mysql'
});
// Step 2: Find yesterday's backup
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const yesterdayBackup = backups.backups.find(b => {
const backupDate = new Date(b.created_at);
return backupDate.toDateString() === yesterday.toDateString();
});
if (!yesterdayBackup) {
throw new Error("No backup found from yesterday");
}
// Step 3: Restore
console.log(`Restoring backup: ${yesterdayBackup.id}`);
await restoreBackup({
server: 'production',
backupId: yesterdayBackup.id
});
console.log("Recovery completed successfully");
}
// ============================================================================
// EXAMPLE 12: Backup to Remote Storage (S3)
// ============================================================================
/*
User: "Backup database and upload to S3"
*/
async function backupToS3() {
// Step 1: Create local backup
const backup = await createBackup({
server: 'production',
type: 'mysql',
name: 'prod-db',
database: 'myapp_prod'
});
// Step 2: Download backup from server
await downloadFile({
server: 'production',
remotePath: backup.location,
localPath: '/tmp/backup.gz'
});
// Step 3: Upload to S3 (requires AWS CLI on local machine)
await executeLocal(
`aws s3 cp /tmp/backup.gz s3://my-backups/${backup.backup_id}.gz`
);
// Step 4: Clean up local file
await executeLocal('rm /tmp/backup.gz');
console.log(`Backup uploaded to S3: s3://my-backups/${backup.backup_id}.gz`);
}
// ============================================================================
// EXAMPLE 13: Compliance Backup (90-day retention)
// ============================================================================
/*
User: "Create compliance backup with 90-day retention"
*/
const complianceBackup = {
tool: 'ssh_backup_create',
params: {
server: 'production',
type: 'mysql',
name: 'compliance-q4-2025',
database: 'financial_data',
retention: 90, // 90 days for compliance
compress: true
}
};
// Schedule monthly compliance backups
const monthlyCompliance = {
tool: 'ssh_backup_schedule',
params: {
server: 'production',
schedule: '0 3 1 * *', // 1st of each month at 3 AM
type: 'mysql',
name: 'monthly-compliance',
database: 'financial_data',
retention: 365 // Keep for 1 year
}
};
// ============================================================================
// CRON SCHEDULE REFERENCE
// ============================================================================
/*
Common cron schedules:
Daily:
- "0 2 * * *" // Every day at 2 AM
- "0 0 * * *" // Every day at midnight
Hourly:
- "0 * * * *" // Every hour at minute 0
- "0 */6 * * *" // Every 6 hours
Weekly:
- "0 0 * * 0" // Every Sunday at midnight
- "0 3 * * 1" // Every Monday at 3 AM
Monthly:
- "0 0 1 * *" // 1st of month at midnight
- "0 2 15 * *" // 15th of month at 2 AM
Weekdays:
- "0 1 * * 1-5" // Mon-Fri at 1 AM
Custom:
- "*/30 * * * *" // Every 30 minutes
- "0 */4 * * *" // Every 4 hours
- "0 9-17 * * *" // Every hour from 9 AM to 5 PM
*/
// ============================================================================
// NOTES
// ============================================================================
/*
Best Practices:
1. Security:
- Store passwords in environment variables
- Use SSH keys for authentication
- Restrict backup directory permissions (chmod 700)
- Encrypt sensitive backups
2. Testing:
- Test restore procedures monthly
- Verify backup integrity
- Document recovery times
- Keep runbooks updated
3. Monitoring:
- Set up alerts for backup failures
- Monitor disk space
- Track backup sizes and durations
- Review retention policies quarterly
4. Storage:
- Implement 3-2-1 backup rule
- Consider cloud storage (S3, GCS, Azure)
- Monitor backup costs
- Clean up old backups
5. Compliance:
- Follow regulatory retention requirements
- Encrypt backups at rest
- Maintain audit logs
- Test disaster recovery procedures
*/
module.exports = {
mysqlBackup,
postgresBackup,
filesBackup,
mongoBackup,
listBackups,
restoreBackup,
scheduleDaily,
complianceBackup,
preDeploymentWorkflow,
disasterRecovery,
backupToS3
};