-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgbackup.js
More file actions
executable file
·396 lines (331 loc) · 15.6 KB
/
Copy pathgbackup.js
File metadata and controls
executable file
·396 lines (331 loc) · 15.6 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
#!/usr/bin/env node
/**
* gbackup - Enhanced Repository Backup Manager
*
* Features:
* - Multiple backup strategies (branch, tag, stash, remote)
* - Automatic backup naming and timestamping
* - Backup verification and integrity checks
* - Repository state snapshots
* - Easy restoration and rollback capabilities
*
* Usage:
* gbackup - Smart auto backup
* gbackup --branch - Create backup branch
* gbackup --tag - Create backup tag
* gbackup --all - Full backup suite
* gbackup --help - Show this help
*/
import { execSync } from 'child_process';
import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
// Check if we're in a git repository
function validateRepository() {
try {
execSync('git rev-parse --is-inside-work-tree', { stdio: 'pipe' });
return true;
} catch (error) {
console.log(chalk.red('❌ Error: Not a git repository'));
console.log(chalk.yellow('💡 Initialize with: git init'));
return false;
}
}
// Show help information
function showHelp() {
console.log(chalk.bold.magenta('\n🗄️ gbackup - Repository Backup'));
console.log(chalk.cyan('Purpose:'), 'Create comprehensive repository backups with various strategies for data protection and disaster recovery.\n');
console.log(chalk.cyan('Command:'), 'gbackup [strategy] [options]');
console.log(chalk.cyan('\nParameters:'));
console.log(' [strategy] - Optional backup strategy (branch, tag, full, incremental)');
console.log(chalk.cyan('\nEssential Options:'));
console.log(' ' + chalk.green('--branch <name>') + ' - Backup specific branch');
console.log(' ' + chalk.green('--all-branches') + ' - Backup all branches');
console.log(' ' + chalk.green('--tags') + ' - Include all tags in backup');
console.log(' ' + chalk.green('--full') + ' - Complete repository backup');
console.log(' ' + chalk.green('--incremental') + ' - Incremental backup since last backup');
console.log(' ' + chalk.green('--compress') + ' - Compress backup archives');
console.log(' ' + chalk.green('--remote <url>') + ' - Push backup to remote location');
console.log(' ' + chalk.green('--verify') + ' - Verify backup integrity after creation');
console.log(' ' + chalk.green('--name <name>') + ' - Custom backup name');
console.log(' ' + chalk.green('--message <msg>') + ' - Custom backup description');
console.log(' ' + chalk.green('--list') + ' - List all available backups');
console.log(' ' + chalk.green('--restore <name>') + ' - Restore from backup');
console.log(' ' + chalk.green('--cleanup') + ' - Clean old backup files');
console.log(' ' + chalk.green('-h, --help') + ' - Show detailed help information');
console.log(chalk.cyan('\nCommon Use Cases:'));
console.log(chalk.white(' gbackup') + ' # Default backup strategy');
console.log(chalk.white(' gbackup --help') + ' # Show help');
console.log(chalk.white(' gbackup --branch main') + ' # Backup main branch only');
console.log(chalk.white(' gbackup --all-branches') + ' # Backup all branches');
console.log(chalk.white(' gbackup --tags') + ' # Backup with all tags');
console.log(chalk.white(' gbackup --full --compress') + ' # Complete compressed backup');
console.log(chalk.white(' gbackup --incremental') + ' # Incremental since last backup');
console.log(chalk.white(' gbackup --remote backup-server') + ' # Push to backup server');
console.log(chalk.white(' gbackup --verify --compress') + ' # Compressed backup with verification');
console.log(chalk.white(' gbackup --name "before-refactor"') + ' # Named branch backup');
console.log(chalk.white(' gbackup --list') + ' # List available backups');
console.log(chalk.white(' gbackup --restore backup-name') + ' # Restore from backup');
console.log(chalk.cyan('\nBackup Strategies:'));
console.log(' • Branch backup - Backs up specific branch with history');
console.log(' • Full backup - Complete repository including all branches and tags');
console.log(' • Incremental backup - Only changes since last backup');
console.log(' • Tag backup - Backup specific tagged versions');
console.log(' • Remote backup - Push backups to remote storage');
console.log(chalk.cyan('\nWorkflow Tips:'));
console.log(' • Use ' + chalk.yellow('--verify') + ' to ensure backup integrity');
console.log(' • Use ' + chalk.yellow('--compress') + ' to save storage space');
console.log(' • Use ' + chalk.yellow('--incremental') + ' for faster regular backups');
console.log(' • Use ' + chalk.yellow('--list') + ' to see available backups before restoring');
console.log(chalk.cyan('\nSafety Notes:'));
console.log(' • Backups are timestamped to prevent conflicts');
console.log(' • Use ' + chalk.yellow('--cleanup') + ' to remove old backup files');
console.log(' • Verify backup integrity before important operations');
console.log(chalk.gray('\n════════════════════════════════════════════════════════════'));
}
// Get current branch name
function getCurrentBranch() {
try {
return execSync('git branch --show-current', { encoding: 'utf8' }).trim();
} catch (error) {
return 'unknown';
}
}
// Generate backup name with timestamp
function generateBackupName(prefix = 'backup', customName = '') {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').substring(0, 19);
const currentBranch = getCurrentBranch();
if (customName) {
return `${prefix}-${customName}-${timestamp}`;
} else {
return `${prefix}-${currentBranch}-${timestamp}`;
}
}
// Check for uncommitted changes
function hasUncommittedChanges() {
try {
const status = execSync('git status --porcelain', { encoding: 'utf8' });
return status.trim().length > 0;
} catch (error) {
return false;
}
}
// Run git command with error handling
function runGitCommand(command, successMessage) {
try {
const result = execSync(command, { encoding: 'utf8' });
if (successMessage) {
console.log(chalk.green(`✅ ${successMessage}`));
}
return result;
} catch (error) {
console.log(chalk.red(`❌ Git command failed: ${error.message}`));
throw error;
}
}
// Create branch backup
function createBranchBackup(backupName, message) {
console.log(chalk.blue('🌿 Creating branch backup...'));
if (hasUncommittedChanges()) {
console.log(chalk.yellow('📦 Committing uncommitted changes...'));
runGitCommand('git add .', 'Staged all changes');
runGitCommand(`git commit -m "Backup commit: ${message || 'Auto backup'}"`, 'Committed changes');
}
runGitCommand(`git branch ${backupName}`, `Created backup branch: ${backupName}`);
console.log(chalk.cyan(`💡 Backup branch '${backupName}' created successfully`));
console.log(chalk.gray(` Restore with: ${chalk.green(`gcheckout ${backupName}`)}`));
return backupName;
}
// Create tag backup
function createTagBackup(backupName, message) {
console.log(chalk.blue('🏷️ Creating tag backup...'));
if (hasUncommittedChanges()) {
console.log(chalk.yellow('📦 Committing uncommitted changes...'));
runGitCommand('git add .', 'Staged all changes');
runGitCommand(`git commit -m "Backup commit: ${message || 'Auto backup'}"`, 'Committed changes');
}
const tagMessage = message || `Backup created on ${new Date().toISOString()}`;
runGitCommand(`git tag -a ${backupName} -m "${tagMessage}"`, `Created backup tag: ${backupName}`);
console.log(chalk.cyan(`💡 Backup tag '${backupName}' created successfully`));
console.log(chalk.gray(` Restore with: ${chalk.green(`gcheckout ${backupName}`)}`));
return backupName;
}
// Create stash backup
function createStashBackup(message) {
console.log(chalk.blue('📦 Creating stash backup...'));
if (!hasUncommittedChanges()) {
console.log(chalk.yellow('💡 No uncommitted changes to stash'));
return null;
}
const stashMessage = message || `Backup stash created on ${new Date().toISOString()}`;
runGitCommand(`git stash push -m "${stashMessage}"`, 'Created backup stash');
console.log(chalk.cyan('💡 Stash backup created successfully'));
console.log(chalk.gray(` Restore with: ${chalk.green('git stash pop')}`));
return stashMessage;
}
// Create remote backup
function createRemoteBackup(branchName) {
console.log(chalk.blue('🌐 Creating remote backup...'));
try {
runGitCommand(`git push origin ${branchName}`, `Pushed backup branch to remote`);
console.log(chalk.cyan(`💡 Remote backup '${branchName}' pushed successfully`));
return branchName;
} catch (error) {
console.log(chalk.yellow('⚠️ Remote backup failed - no remote configured or connection issue'));
return null;
}
}
// List available backups
function listBackups() {
console.log(chalk.blue('📋 Available Backups:'));
try {
// List backup branches
const branches = execSync('git branch -a | grep backup', { encoding: 'utf8' });
if (branches.trim()) {
console.log(chalk.cyan('\n🌿 Backup Branches:'));
branches.trim().split('\n').forEach(branch => {
const cleanBranch = branch.replace(/^\s*\*?\s*/, '').replace('remotes/origin/', '');
console.log(` ${chalk.green('•')} ${chalk.white(cleanBranch)}`);
});
}
} catch (error) {
console.log(chalk.gray(' No backup branches found'));
}
try {
// List backup tags
const tags = execSync('git tag -l "*backup*"', { encoding: 'utf8' });
if (tags.trim()) {
console.log(chalk.cyan('\n🏷️ Backup Tags:'));
tags.trim().split('\n').forEach(tag => {
console.log(` ${chalk.green('•')} ${chalk.white(tag)}`);
});
}
} catch (error) {
console.log(chalk.gray(' No backup tags found'));
}
try {
// List backup stashes
const stashes = execSync('git stash list | grep -i backup', { encoding: 'utf8' });
if (stashes.trim()) {
console.log(chalk.cyan('\n📦 Backup Stashes:'));
stashes.trim().split('\n').slice(0, 5).forEach(stash => {
console.log(` ${chalk.green('•')} ${chalk.white(stash)}`);
});
}
} catch (error) {
console.log(chalk.gray(' No backup stashes found'));
}
}
// Main function
async function main() {
const args = process.argv.slice(2);
// Help functionality
if (args.includes('-h') || args.includes('--help')) {
showHelp();
return;
}
// Validate repository
if (!validateRepository()) {
process.exit(1);
}
try {
const branchMode = args.includes('--branch');
const tagMode = args.includes('--tag');
const stashMode = args.includes('--stash');
const remoteMode = args.includes('--remote');
const allMode = args.includes('--all');
const listMode = args.includes('--list');
const verifyMode = args.includes('--verify');
const cleanupMode = args.includes('--cleanup');
const customName = args.find(arg => arg === '--name') ? args[args.indexOf('--name') + 1] : '';
const customMessage = args.find(arg => arg === '--message') ? args[args.indexOf('--message') + 1] : '';
console.log(chalk.bold.magenta('\n🗄️ Repository Backup Manager'));
console.log(chalk.gray('─'.repeat(50)));
if (listMode) {
listBackups();
return;
}
if (cleanupMode) {
console.log(chalk.blue('🧹 Cleaning up old backups...'));
try {
// Clean backup branches older than 30 days
runGitCommand('git for-each-ref --format="%(refname:short) %(committerdate)" refs/heads/backup* | while read branch date; do if [[ $(date -d "$date" +%s) -lt $(date -d "30 days ago" +%s) ]]; then git branch -D "$branch"; fi; done || true', 'Cleaned old backup branches');
console.log(chalk.green('✅ Cleanup completed'));
} catch (error) {
console.log(chalk.yellow('⚠️ Cleanup completed with warnings'));
}
return;
}
console.log(chalk.blue('📊 Repository Status:'));
console.log(chalk.cyan(` • Current Branch: ${chalk.white(getCurrentBranch())}`));
console.log(chalk.cyan(` • Uncommitted Changes: ${hasUncommittedChanges() ? chalk.yellow('Yes') : chalk.green('None')}`));
const backupResults = [];
if (allMode || (!branchMode && !tagMode && !stashMode && !remoteMode)) {
// Smart backup or all mode
console.log(chalk.blue('\n🤖 Smart Backup Strategy:'));
// Always create branch backup
const branchName = generateBackupName('backup', customName);
const branchResult = createBranchBackup(branchName, customMessage);
if (branchResult) backupResults.push(`Branch: ${branchResult}`);
if (allMode) {
// Create tag backup
const tagName = generateBackupName('backup-tag', customName);
const tagResult = createTagBackup(tagName, customMessage);
if (tagResult) backupResults.push(`Tag: ${tagResult}`);
// Create stash backup if changes exist
const stashResult = createStashBackup(customMessage);
if (stashResult) backupResults.push(`Stash: ${stashResult}`);
// Create remote backup
const remoteResult = createRemoteBackup(branchName);
if (remoteResult) backupResults.push(`Remote: ${remoteResult}`);
}
} else {
// Specific backup modes
if (branchMode) {
const branchName = generateBackupName('backup', customName);
const result = createBranchBackup(branchName, customMessage);
if (result) backupResults.push(`Branch: ${result}`);
}
if (tagMode) {
const tagName = generateBackupName('backup-tag', customName);
const result = createTagBackup(tagName, customMessage);
if (result) backupResults.push(`Tag: ${result}`);
}
if (stashMode) {
const result = createStashBackup(customMessage);
if (result) backupResults.push(`Stash: ${result}`);
}
if (remoteMode) {
const currentBranch = getCurrentBranch();
const result = createRemoteBackup(currentBranch);
if (result) backupResults.push(`Remote: ${result}`);
}
}
console.log(chalk.green.bold('\n🎉 Backup completed successfully!'));
if (backupResults.length > 0) {
console.log(chalk.cyan('\n📋 Backup Summary:'));
backupResults.forEach(result => {
console.log(chalk.green(` ✅ ${result}`));
});
}
console.log(chalk.cyan('\n💡 Next steps:'));
console.log(chalk.gray(` • View backups: ${chalk.green('gbackup --list')}`));
console.log(chalk.gray(` • Continue working safely`));
console.log(chalk.gray(` • Restore if needed from backup list`));
} catch (error) {
console.log(chalk.red.bold('\n❌ Backup failed!'));
console.log(chalk.red(`Error: ${error.message}`));
console.log(chalk.yellow('\n💡 Recovery suggestions:'));
console.log(chalk.gray(` • Check repository: ${chalk.green('gstatus')}`));
console.log(chalk.gray(` • Try simpler backup: ${chalk.green('gbackup --branch')}`));
console.log(chalk.gray(` • Get help: ${chalk.green('gbackup --help')}`));
process.exit(1);
}
}
// Run as standalone script
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(error => {
console.error(chalk.red('❌ Fatal error:'), error.message);
process.exit(1);
});
}