File size: 11,150 Bytes
da819ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const mongoose = require('mongoose');
const fs = require('fs').promises;
const path = require('path');
const { exec } = require('child_process');
const { promisify } = require('util');

const execAsync = promisify(exec);

// Atlas MongoDB connection string
const MONGODB_URI = 'mongodb+srv://nothingyu:wSg3lbO1PkHiRMq9@sandbox.ecysggv.mongodb.net/test?retryWrites=true&w=majority&appName=sandbox';

// Connect to MongoDB Atlas
const connectDB = async () => {
  try {
    await mongoose.connect(MONGODB_URI);
    console.log('βœ… Connected to MongoDB Atlas');
  } catch (error) {
    console.error('❌ MongoDB connection error:', error);
    process.exit(1);
  }
};

// Comprehensive Backup System (Data + Code)
const comprehensiveBackup = {
  // Create comprehensive backup
  async createComprehensiveBackup() {
    try {
      const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
      const backupName = `comprehensive-backup-${timestamp}`;
      
      console.log(`πŸš€ Creating comprehensive backup: ${backupName}`);
      
      // Create backup directory
      const backupDir = path.join(__dirname, 'backups', backupName);
      await fs.mkdir(backupDir, { recursive: true });
      
      // 1. BACKUP DATABASE DATA
      console.log('πŸ“Š Backing up database data...');
      const dbBackup = await this.backupDatabase(backupDir);
      
      // 2. BACKUP CODE FILES
      console.log('πŸ’» Backing up code files...');
      const codeBackup = await this.backupCodeFiles(backupDir);
      
      // 3. BACKUP CONFIGURATION
      console.log('βš™οΈ Backing up configuration...');
      const configBackup = await this.backupConfiguration(backupDir);
      
      // 4. CREATE BACKUP MANIFEST
      console.log('πŸ“‹ Creating backup manifest...');
      const manifest = {
        backupName,
        timestamp: new Date(),
        type: 'comprehensive',
        data: {
          database: dbBackup,
          code: codeBackup,
          configuration: configBackup
        },
        totalSize: await this.calculateBackupSize(backupDir),
        version: '1.0'
      };
      
      await fs.writeFile(path.join(backupDir, 'manifest.json'), JSON.stringify(manifest, null, 2));
      
      // 5. SAVE BACKUP RECORD TO DATABASE
      const backupRecord = {
        backupName,
        timestamp: new Date(),
        type: 'comprehensive',
        location: backupDir,
        size: manifest.totalSize,
        status: 'created'
      };
      
      await mongoose.connection.db.collection('backups').insertOne(backupRecord);
      
      console.log(`βœ… Comprehensive backup created: ${backupName}`);
      console.log(`πŸ“Š Database records: ${dbBackup.totalRecords}`);
      console.log(`πŸ’» Code files: ${codeBackup.fileCount}`);
      console.log(`πŸ’Ύ Total size: ${(manifest.totalSize / 1024 / 1024).toFixed(2)} MB`);
      
      return backupName;
      
    } catch (error) {
      console.error('❌ Error creating comprehensive backup:', error);
      throw error;
    }
  },
  
  // Backup database data
  async backupDatabase(backupDir) {
    const collections = ['subtitles', 'sourcetexts', 'submissions', 'users', 'backups'];
    const dbData = {};
    let totalRecords = 0;
    
    for (const collection of collections) {
      try {
        const data = await mongoose.connection.db.collection(collection).find({}).toArray();
        dbData[collection] = data;
        totalRecords += data.length;
        console.log(`  πŸ“¦ Exported ${data.length} records from ${collection}`);
      } catch (error) {
        console.warn(`  ⚠️ Could not export ${collection}:`, error.message);
      }
    }
    
    const dbBackupPath = path.join(backupDir, 'database.json');
    await fs.writeFile(dbBackupPath, JSON.stringify(dbData, null, 2));
    
    return {
      totalRecords,
      collections: Object.keys(dbData),
      filePath: dbBackupPath
    };
  },
  
  // Backup code files
  async backupCodeFiles(backupDir) {
    const codeDir = path.join(backupDir, 'code');
    await fs.mkdir(codeDir, { recursive: true });
    
    // Define important code directories and files
    const codePaths = [
      // Backend code
      { src: path.join(__dirname), dest: 'backend' },
      // Frontend code (relative to backend)
      { src: path.join(__dirname, '../frontend'), dest: 'frontend' },
      // Root configuration
      { src: path.join(__dirname, '../../'), dest: 'root' }
    ];
    
    let fileCount = 0;
    
    for (const codePath of codePaths) {
      try {
        if (await this.pathExists(codePath.src)) {
          await this.copyDirectory(codePath.src, path.join(codeDir, codePath.dest));
          const count = await this.countFiles(codePath.src);
          fileCount += count;
          console.log(`  πŸ’» Copied ${count} files from ${codePath.dest}`);
        }
      } catch (error) {
        console.warn(`  ⚠️ Could not backup ${codePath.dest}:`, error.message);
      }
    }
    
    return {
      fileCount,
      directories: codePaths.map(p => p.dest),
      location: codeDir
    };
  },
  
  // Backup configuration
  async backupConfiguration(backupDir) {
    const configDir = path.join(backupDir, 'config');
    await fs.mkdir(configDir, { recursive: true });
    
    const configFiles = [
      'package.json',
      'package-lock.json',
      'Dockerfile',
      'docker-compose.yml',
      'nginx.conf',
      '.gitignore'
    ];
    
    let configCount = 0;
    
    for (const configFile of configFiles) {
      try {
        const srcPath = path.join(__dirname, configFile);
        if (await this.pathExists(srcPath)) {
          const destPath = path.join(configDir, configFile);
          await fs.copyFile(srcPath, destPath);
          configCount++;
          console.log(`  βš™οΈ Copied ${configFile}`);
        }
      } catch (error) {
        console.warn(`  ⚠️ Could not copy ${configFile}:`, error.message);
      }
    }
    
    return {
      fileCount: configCount,
      files: configFiles,
      location: configDir
    };
  },
  
  // Helper functions
  async pathExists(path) {
    try {
      await fs.access(path);
      return true;
    } catch {
      return false;
    }
  },
  
  async copyDirectory(src, dest) {
    await fs.mkdir(dest, { recursive: true });
    const entries = await fs.readdir(src, { withFileTypes: true });
    
    for (const entry of entries) {
      const srcPath = path.join(src, entry.name);
      const destPath = path.join(dest, entry.name);
      
      if (entry.isDirectory()) {
        await this.copyDirectory(srcPath, destPath);
      } else {
        await fs.copyFile(srcPath, destPath);
      }
    }
  },
  
  async countFiles(dir) {
    let count = 0;
    const entries = await fs.readdir(dir, { withFileTypes: true });
    
    for (const entry of entries) {
      const fullPath = path.join(dir, entry.name);
      
      if (entry.isDirectory()) {
        count += await this.countFiles(fullPath);
      } else {
        count++;
      }
    }
    
    return count;
  },
  
  async calculateBackupSize(backupDir) {
    const { stdout } = await execAsync(`du -sb "${backupDir}" | cut -f1`);
    return parseInt(stdout.trim());
  },
  
  // List comprehensive backups
  async listComprehensiveBackups() {
    try {
      console.log('πŸ“‹ Available comprehensive backups:');
      
      const backupCollection = mongoose.connection.db.collection('backups');
      const backups = await backupCollection.find({ type: 'comprehensive' }).sort({ timestamp: -1 }).toArray();
      
      if (backups.length === 0) {
        console.log('  No comprehensive backups found');
      } else {
        backups.forEach(backup => {
          const date = new Date(backup.timestamp).toLocaleString();
          const size = (backup.size / 1024 / 1024).toFixed(2);
          console.log(`  πŸ“¦ ${backup.backupName} (${size} MB, ${date})`);
        });
      }
      
    } catch (error) {
      console.error('❌ Error listing backups:', error);
    }
  },
  
  // Restore from comprehensive backup
  async restoreFromBackup(backupName) {
    try {
      console.log(`πŸ”„ Restoring from comprehensive backup: ${backupName}`);
      
      const backupDir = path.join(__dirname, 'backups', backupName);
      const manifestPath = path.join(backupDir, 'manifest.json');
      
      if (!await this.pathExists(manifestPath)) {
        throw new Error('Backup manifest not found');
      }
      
      const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
      console.log('πŸ“‹ Backup manifest:', manifest);
      
      // Restore database
      console.log('πŸ“Š Restoring database...');
      await this.restoreDatabase(backupDir);
      
      // Restore code (optional - user confirmation)
      console.log('πŸ’» Code restoration available');
      console.log('⚠️ Code restoration will overwrite existing files');
      console.log('   Run: node restore-code.js <backup-name> to restore code');
      
      console.log(`βœ… Database restoration completed: ${backupName}`);
      
    } catch (error) {
      console.error('❌ Error restoring from backup:', error);
    }
  },
  
  // Restore database only
  async restoreDatabase(backupDir) {
    const dbBackupPath = path.join(backupDir, 'database.json');
    const dbData = JSON.parse(await fs.readFile(dbBackupPath, 'utf8'));
    
    for (const [collection, data] of Object.entries(dbData)) {
      try {
        // Clear existing data
        await mongoose.connection.db.collection(collection).deleteMany({});
        
        // Insert backup data
        if (data.length > 0) {
          await mongoose.connection.db.collection(collection).insertMany(data);
        }
        
        console.log(`  βœ… Restored ${data.length} records to ${collection}`);
      } catch (error) {
        console.error(`  ❌ Error restoring ${collection}:`, error.message);
      }
    }
  }
};

// Main function
const main = async () => {
  try {
    console.log('πŸš€ Starting comprehensive backup system...');
    
    // Create comprehensive backup
    const backupName = await comprehensiveBackup.createComprehensiveBackup();
    
    // List backups
    await comprehensiveBackup.listComprehensiveBackups();
    
    console.log('\nπŸŽ‰ Comprehensive backup system ready!');
    console.log('\nπŸ“‹ Available functions:');
    console.log('  - createComprehensiveBackup(): Backup data + code');
    console.log('  - listComprehensiveBackups(): List all backups');
    console.log('  - restoreFromBackup(name): Restore database');
    console.log('  - restoreCode(name): Restore code files (separate script)');
    
    console.log('\n⏰ To set up automated comprehensive backups:');
    console.log('  1. Add to crontab: 0 2 * * * cd /path/to/backend && node comprehensive-backup.js');
    console.log('  2. Or run manually: node comprehensive-backup.js');
    
  } catch (error) {
    console.error('❌ Error in comprehensive backup system:', error);
  } finally {
    await mongoose.disconnect();
    console.log('πŸ”Œ Disconnected from MongoDB');
  }
};

// Run the system
connectDB().then(() => {
  main();
});