This guide explains how to protect your Firestore data from accidental deletion (like the nuclear reset incident) using the implemented backup system.
Navigate to: http://localhost:3000/security-audit
You'll see a 🛡️ Backup & Restore section with three options:
- 📦 Create Backup - Download complete database backup
- 📤 Restore Backup - Upload and restore from backup file
- 🧪 Test Profiles - Restore fake profiles for testing
# Web Interface (Recommended)
1. Go to http://localhost:3000/security-audit
2. Click "📥 Download Backup"
3. Save the JSON file somewhere safe
# Direct API call
curl -X POST http://localhost:3000/api/admin/backup-firestore \
-H "Cookie: your-session-cookie" \
--output backup-$(date +%Y-%m-%d).json- ✅ One-click backup from security audit page
- ✅ Automatic download as JSON file
- ✅ Complete data including metadata
- ✅ Admin-only access for security
When to use: Before any risky operations, weekly backups
- ✅ Daily automatic backups at 2 AM UTC
- ✅ Google Cloud Storage integration
- ✅ Enterprise-grade reliability
- ✅ Scheduled via Cloud Scheduler
When to use: Production environments, critical data
- ✅ Custom backup logic and notifications
- ✅ Manual trigger endpoints
- ✅ Restore functionality included
- ✅ Integration with monitoring systems
When to use: Complex workflows, custom requirements
{
"_metadata": {
"collections": 8,
"totalDocuments": 1245,
"timestamp": "2024-01-15T10:30:00.000Z",
"adminUser": "ajumashukurov@gmail.com"
},
"profiles": [
{ "id": "user@example.com", "data": {...} }
],
"matches": [...],
"swipes": [...],
"messages": [...],
"chats": [...],
"likes": [...],
"users": [...],
"notifications": [...]
}Only these emails can create/restore backups:
const ADMIN_EMAILS = [
"ajumashukurov@gmail.com", // Current user
"admin@devmolink.com", // Add more admins here
];const COLLECTIONS_TO_BACKUP = [
"profiles", // User profiles
"matches", // User matches
"swipes", // Swipe history
"messages", // Chat messages
"chats", // Chat metadata
"likes", // Like data
"users", // User accounts
"notifications", // Notifications
];Step 1: Stop the damage
# Immediately stop your app if nuclear reset is running
pm2 stop devmolink
# or
pkill -f "npm run dev"Step 2: Restore from backup
- Go to
http://localhost:3000/security-audit - Click 📤 Restore Backup
- Select your most recent backup file
- Confirm restoration
Step 3: Verify data
# Check Firebase console or run:
curl http://localhost:3000/api/admin/backup-firestore- Use 🧪 Restore Test Data for fake profiles
- Ask users to re-register
- Implement automated backups immediately
- Development: Before risky changes
- Staging: Daily
- Production: Multiple times daily + before deployments
- Local: For immediate recovery
- Cloud Storage: For disaster recovery
- Version Control: For code-related backups
- Multiple Locations: For critical data
# Test backup creation
curl -X POST http://localhost:3000/api/admin/backup-firestore
# Test restore (with test data)
# Use the web interface with a small backup file# Setup script (requires Google Cloud CLI)
cd scripts
chmod +x setup-firestore-backup.sh
./setup-firestore-backup.sh# Deploy backup functions
cd functions/backup-firestore
npm install
firebase deploy --only functions// Add to your monitoring system
fetch("/api/admin/backup-firestore").then((r) =>
r.ok ? "✅ Backup healthy" : "❌ Backup failed"
);- ✅ Admin-only backup/restore endpoints
- ✅ Session-based authentication
- ✅ Logging of all backup operations
- ✅ IP restrictions (optional, configure in middleware)
- ✅ Encrypted backup files (JSON format)
- ✅ No sensitive data in URLs or logs
- ✅ Audit trail of who performed operations
- ✅ Rate limiting on backup endpoints
- ✅ File size limits for uploads
- ✅ Format validation for restore files
- ✅ Error handling for corrupted backups
- ✅ Cleanup of temporary files
Error: "Failed to create backup"
Solutions:
1. Check Firebase permissions
2. Verify admin email in ADMIN_EMAILS array
3. Check Firestore security rules
4. Ensure collections exist
Error: "Invalid backup file format"
Solutions:
1. Verify JSON file structure
2. Check _metadata object exists
3. Ensure file isn't corrupted
4. Try with smaller backup file first
Error: "Admin access required"
Solutions:
1. Add your email to ADMIN_EMAILS
2. Ensure you're logged in
3. Check session hasn't expired
4. Verify authentication working
# Check backup system status
curl http://localhost:3000/api/admin/backup-firestoreMonitor collection growth to estimate backup times:
// Returns document counts per collection
fetch("/api/admin/backup-firestore")
.then((r) => r.json())
.then((data) => console.log("Collections:", data.collections));| Action | URL | Method |
|---|---|---|
| Create Backup | /api/admin/backup-firestore |
POST |
| Restore Backup | /api/admin/restore-firestore |
POST |
| Restore Test Data | /api/admin/restore-profiles |
POST |
| Health Check | /api/admin/backup-firestore |
GET |
| Web Interface | /security-audit |
Browser |
Remember: Always test your restore process with non-production data first! 🧪