const fs = require("fs"); const path = require("path"); const dbPath = path.join(__dirname, "../prisma/dev.db"); const backupDir = path.join(__dirname, "../backups"); function backupDatabase() { if (!fs.existsSync(dbPath)) { console.error(`[ERROR] Database file not found at ${dbPath}`); process.exit(1); } if (!fs.existsSync(backupDir)) { fs.mkdirSync(backupDir, { recursive: true }); } const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const backupPath = path.join(backupDir, `dev-backup-${timestamp}.db`); fs.copyFileSync(dbPath, backupPath); console.log(`[SUCCESS] Database backup created: ${backupPath}`); // Test restoration capability into a temp file const testRestorePath = path.join(backupDir, `test-restore-${timestamp}.db`); fs.copyFileSync(backupPath, testRestorePath); const originalSize = fs.statSync(dbPath).size; const restoredSize = fs.statSync(testRestorePath).size; if (originalSize === restoredSize && restoredSize > 0) { console.log(`[SUCCESS] Database restore test verified! Size matches (${originalSize} bytes)`); fs.unlinkSync(testRestorePath); // Clean test restore file return true; } else { console.error(`[FAIL] Restoration test failed! Size mismatch (${originalSize} vs ${restoredSize})`); process.exit(1); } } backupDatabase();