5.0 KiB
5.0 KiB
Database Backup System - Complete Setup
✅ What's Been Implemented
1. Backend API Endpoints (Admin Only)
POST /admin/backup- Trigger manual backupGET /admin/backups- List all available backupsPOST /admin/restore?filename=<name>- Restore from backup
2. Frontend Admin Panel
- New "ניהול" (Management) tab in navigation (visible to admin users only)
- 🛡️ Admin button in top bar
- Full backup management interface:
- Create new backups instantly
- View all backups with dates and sizes
- Restore from any backup with confirmation
3. Automated Weekly Backups
- Batch script:
run_backup.bat - Full setup guide:
WEEKLY_BACKUP_SETUP.md - Configured for Windows Task Scheduler
🚀 How to Use
Manual Backup (Admin User)
- Login with admin account
- Click 🛡️ "ניהול" button in top bar (or use the ניהול tab)
- Click "צור גיבוי חדש" (Create New Backup)
- Backup is created, compressed, and uploaded to R2
- See confirmation toast: "גיבוי נוצר בהצלחה! 📦"
Restore from Backup (Admin User)
- Go to Admin Panel (🛡️ ניהול)
- View all available backups in the table
- Click "שחזר" (Restore) button for desired backup
- Confirm the warning (this will delete current data!)
- Page will refresh automatically after restore
Setup Weekly Automatic Backups
Follow the instructions in WEEKLY_BACKUP_SETUP.md:
Quick Steps:
- Open Task Scheduler (
Win + R→taskschd.msc) - Create Task → "Recipe DB Weekly Backup"
- Set trigger: Weekly, Sunday, 2:00 AM
- Set action: Run
C:\Path\To\backend\run_backup.bat - Configure to run even when not logged in
📁 Files Created/Modified
Backend
- ✅
backup_restore_api.py- Core backup/restore functions - ✅
main.py- Added admin endpoints - ✅
requirements.txt- Added boto3 dependency - ✅
.env- Added R2 credentials - ✅
run_backup.bat- Windows batch script for scheduled tasks - ✅
BACKUP_README.md- Complete documentation - ✅
WEEKLY_BACKUP_SETUP.md- Task Scheduler setup guide
Frontend
- ✅
backupApi.js- API calls for backup operations - ✅
components/AdminPanel.jsx- Admin UI component - ✅
components/TopBar.jsx- Added admin button - ✅
App.jsx- Added admin view and navigation - ✅
App.css- Added admin panel styles
🔐 Security
- Admin-only access: All backup endpoints check
is_adminflag - Non-admin users: Cannot see the admin button or access backup endpoints
- 403 Forbidden: Returned if non-admin tries to access admin endpoints
💾 Backup Details
What's Backed Up
- Complete PostgreSQL database (recipes_db)
- All tables: users, recipes, grocery lists, shares, notifications
Backup Process
- Uses
pg_dumpto export database - Compresses with gzip (typically 80-90% size reduction)
- Uploads to Cloudflare R2 with timestamp
- Filename format:
recipes_db_YYYYMMDD_HHMMSS.sql.gz - Local backups auto-cleanup (keeps last 3)
Restore Process
- Downloads from R2
- Decompresses file
- Drops all existing tables (CASCADE)
- Restores from SQL file
- Cleans up temporary files
🧪 Testing
Test Manual Backup
cd backend
python backup_db.py
Test Manual Restore
cd backend
python restore_db.py
Test via Web UI
- Login as admin
- Navigate to Admin Panel
- Click "צור גיבוי חדש"
- Check R2 bucket for new file
⚠️ Important Notes
- Restore is destructive: It deletes ALL current data
- Admin access required: Set user's
is_admin = truein database - R2 credentials: Already configured in
.env - Weekly backups: Manual setup required (follow WEEKLY_BACKUP_SETUP.md)
- PostgreSQL tools: Must have
pg_dumpandpsqlin system PATH
🔧 Troubleshooting
"Admin access required" error
- Check if user has
is_admin = truein database - Run:
SELECT username, is_admin FROM users;in psql
Backup fails
- Check
backend/backup.logfor errors - Verify R2 credentials in
.env - Verify database credentials in
.env - Test:
python backup_db.pymanually
Can't see admin button
- Verify user's
is_adminflag in database - Refresh page after changing admin status
- Check browser console for errors
Scheduled backup doesn't run
- Check Task Scheduler → Task History
- Verify
run_backup.batpath is correct - Check
backend/backup.logfor errors - Test batch file manually first
📊 What Admins Can Do
✅ Create manual backups anytime
✅ View all backups with dates and sizes
✅ Restore from any backup point
✅ See backup history in table format
✅ All regular user features (recipes, grocery lists, etc.)
Next Steps
- ✅ Test the system: Create a manual backup from Admin Panel
- 📅 Setup weekly backups: Follow WEEKLY_BACKUP_SETUP.md
- 🔒 Secure admin access: Only give admin rights to trusted users
- 📝 Document your backup strategy: When/how often you back up
Your database is now protected with automated backups! 🎉