Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import fs from 'fs-extra'; import * as path from 'path'; import archiver from 'archiver'; import crypto from 'crypto-js'; export interface BackupResult { success: boolean; backupDir: string; recordCount: number; error?: string; } export interface BackupMetadata { customerId: string; service: string; backupType: string; recordCount: number; fileSize: number; checksum: string; createdAt: string; expiresAt?: string; } export class BackupStorage { private baseDir: string; constructor(baseDir = './backup-data') { this.baseDir = baseDir; this.ensureDirectories(); } private async ensureDirectories(): Promise<void> { await fs.ensureDir(this.baseDir); await fs.ensureDir(path.join(this.baseDir, 'customers')); await fs.ensureDir(path.join(this.baseDir, 'temp')); } async createBackupDirectory(customerId: string): Promise<string> { const customerDir = path.join(this.baseDir, 'customers', customerId); const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const backupDir = path.join(customerDir, timestamp); await fs.ensureDir(backupDir); return backupDir; } async saveBackupData( backupDir: string, service: string, filename: string, data: any ): Promise<{ filePath: string; fileSize: number; checksum: string }> { const serviceDir = path.join(backupDir, service); await fs.ensureDir(serviceDir); const filePath = path.join(serviceDir, filename); const jsonData = JSON.stringify(data, null, 2); await fs.writeFile(filePath, jsonData, 'utf8'); const stats = await fs.stat(filePath); const checksum = crypto.SHA256(jsonData).toString(); return { filePath, fileSize: stats.size, checksum }; } async createCompressedBackup( customerId: string, backupDir: string, backupType: string ): Promise<{ archivePath: string; fileSize: number; checksum: string }> { const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const archiveName = `${customerId}-${backupType}-${timestamp}.zip`; const archivePath = path.join(this.baseDir, 'temp', archiveName); return new Promise((resolve, reject) => { const output = fs.createWriteStream(archivePath); const archive = archiver('zip', { zlib: { level: 9 } }); output.on('close', async () => { const stats = await fs.stat(archivePath); const fileBuffer = await fs.readFile(archivePath); const checksum = crypto.SHA256(fileBuffer.toString('hex')).toString(); resolve({ archivePath, fileSize: stats.size, checksum }); }); archive.on('error', (err: any) => { reject(err); }); archive.pipe(output); archive.directory(backupDir, false); archive.finalize(); }); } async loadBackupData(filePath: string): Promise<any> { const data = await fs.readFile(filePath, 'utf8'); return JSON.parse(data); } async restoreFromBackup( customerId: string, backupPath: string, targetDir: string ): Promise<void> { const extractPath = path.join(this.baseDir, 'temp', 'restore', customerId); await fs.ensureDir(extractPath); // Extract the backup await this.extractArchive(backupPath, extractPath); // Copy extracted data to target directory await fs.copy(extractPath, targetDir); // Clean up await fs.remove(extractPath); } private async extractArchive(archivePath: string, extractPath: string): Promise<void> { // This is a simplified extraction - in production you'd use a proper unzip library // For now, we'll assume the backup is already extracted or use a different approach console.log(`Extracting ${archivePath} to ${extractPath}`); } async cleanupOldBackups(customerId: string, retentionDays = 30): Promise<number> { const customerDir = path.join(this.baseDir, 'customers', customerId); Iif (!await fs.pathExists(customerDir)) { return 0; } const cutoffDate = new Date(); cutoffDate.setDate(cutoffDate.getDate() - retentionDays); const backupDirs = await fs.readdir(customerDir); let deletedCount = 0; for (const backupDir of backupDirs) { const backupPath = path.join(customerDir, backupDir); const stats = await fs.stat(backupPath); Iif (stats.isDirectory() && stats.mtime < cutoffDate) { await fs.remove(backupPath); deletedCount++; } } return deletedCount; } async getBackupStats(customerId: string): Promise<{ totalBackups: number; totalSize: number; oldestBackup: string | null; newestBackup: string | null; }> { const customerDir = path.join(this.baseDir, 'customers', customerId); Iif (!await fs.pathExists(customerDir)) { return { totalBackups: 0, totalSize: 0, oldestBackup: null, newestBackup: null }; } const backupDirs = await fs.readdir(customerDir); let totalSize = 0; let oldestDate: Date | null = null; let newestDate: Date | null = null; for (const backupDir of backupDirs) { const backupPath = path.join(customerDir, backupDir); const stats = await fs.stat(backupPath); Iif (stats.isDirectory()) { totalSize += await this.getDirectorySize(backupPath); Iif (!oldestDate || stats.mtime < oldestDate) { oldestDate = stats.mtime; } Iif (!newestDate || stats.mtime > newestDate) { newestDate = stats.mtime; } } } return { totalBackups: backupDirs.length, totalSize, oldestBackup: oldestDate?.toISOString() || null, newestBackup: newestDate?.toISOString() || null }; } private async getDirectorySize(dirPath: string): Promise<number> { let totalSize = 0; const files = await fs.readdir(dirPath); for (const file of files) { const filePath = path.join(dirPath, file); const stats = await fs.stat(filePath); if (stats.isDirectory()) { totalSize += await this.getDirectorySize(filePath); } else { totalSize += stats.size; } } return totalSize; } async validateBackupIntegrity(backupPath: string, expectedChecksum: string): Promise<boolean> { try { const fileBuffer = await fs.readFile(backupPath); const actualChecksum = crypto.SHA256(fileBuffer.toString('hex')).toString(); return actualChecksum === expectedChecksum; } catch (error) { console.error('Error validating backup integrity:', error); return false; } } } |