AzharStore Runbook¶
This runbook provides step-by-step procedures for common operational tasks for the AzharStore Medusa backend.
Table of Contents¶
- Initial Setup
- Starting the Application
- Stopping the Application
- Database Operations
- Admin User Management
- Data Migration
- Backup and Restore
- Monitoring and Logs
- Troubleshooting
- Deployment
Initial Setup¶
1. Clone the Repository¶
2. Configure Environment Variables¶
Edit .env with your configuration:
# Database
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/medusa
# Redis
REDIS_URL=redis://localhost:6379
# CORS
STORE_CORS=http://localhost:3000
ADMIN_CORS=http://localhost:9000
# Secrets
JWT_SECRET=your-jwt-secret-change-this
COOKIE_SECRET=your-cookie-secret-change-this
# Admin
ADMIN_EMAIL=admin@azhar.store
ADMIN_PASSWORD=your-admin-password-change-this
3. Start Services¶
4. Run Database Migrations¶
5. Seed Admin User¶
Starting the Application¶
Start All Services¶
Start Specific Services¶
# Start only backend
docker compose up -d postgres redis medusa
# Start only frontend
docker compose up -d store
# Start only database
docker compose up -d postgres redis
Start in Development Mode¶
# Backend development (without Docker)
cd backend
npm install
npm run dev
# Frontend development (without Docker)
cd frontend/store
npm install
npm run dev
Stopping the Application¶
Stop All Services¶
Stop and Remove Volumes¶
Stop Specific Services¶
docker compose stop medusa
docker compose stop store
docker compose stop postgres
docker compose stop redis
Database Operations¶
Run Migrations¶
Create a New Migration¶
Access PostgreSQL Shell¶
Backup Database¶
Restore Database¶
View Database Tables¶
Inspect Custom Tables¶
# Advertisement table
docker compose exec postgres psql -U postgres -d medusa -c "SELECT * FROM advertisement;"
# Settings table
docker compose exec postgres psql -U postgres -d medusa -c "SELECT * FROM setting;"
# Translation table
docker compose exec postgres psql -U postgres -d medusa -c "SELECT * FROM translation;"
Admin User Management¶
Create Admin User¶
This uses the ADMIN_EMAIL and ADMIN_PASSWORD from .env.
Reset Admin Password¶
-
Access PostgreSQL:
-
Update the admin user (adjust query based on Medusa's user table structure):
-
Exit PostgreSQL:
List Admin Users¶
docker compose exec postgres psql -U postgres -d medusa -c "SELECT id, email FROM user WHERE role = 'admin';"
Data Migration¶
Migrate from Supabase¶
-
Add Supabase credentials to
.env: -
Run migration script:
-
Verify migration:
Rollback Migration¶
# Truncate custom tables
docker compose exec postgres psql -U postgres -d medusa -c "TRUNCATE TABLE advertisement, setting, translation CASCADE;"
Backup and Restore¶
Full Backup¶
# Backup database
docker compose exec postgres pg_dump -U postgres medusa > backup-$(date +%Y%m%d).sql
# Backup environment file
cp .env .env.backup
Scheduled Backup (Linux Cron)¶
Add to crontab:
0 2 * * * cd /path/to/az-main && docker compose exec postgres pg_dump -U postgres medusa > /backups/backup-$(date +\%Y\%m\%d).sql
Restore from Backup¶
# Restore database
cat backup-20240101.sql | docker compose exec -T postgres psql -U postgres medusa
# Restore environment file
cp .env.backup .env
Monitoring and Logs¶
View All Logs¶
View Service-Specific Logs¶
# Medusa logs
docker compose logs -f medusa
# PostgreSQL logs
docker compose logs -f postgres
# Redis logs
docker compose logs -f redis
# Store logs
docker compose logs -f store
View Recent Logs (Last 100 lines)¶
Check Service Health¶
# Check if services are running
docker compose ps
# Check Medusa health
curl http://localhost:9000/health
# Check PostgreSQL connection
docker compose exec postgres pg_isready -U postgres
# Check Redis connection
docker compose exec redis redis-cli ping
Monitor Database Performance¶
# Active queries
docker compose exec postgres psql -U postgres -d medusa -c "SELECT * FROM pg_stat_activity WHERE state = 'active';"
# Table sizes
docker compose exec postgres psql -U postgres -d medusa -c "SELECT schemaname, tablename, pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) FROM pg_tables WHERE schemaname = 'public';"
Troubleshooting¶
Services Won't Start¶
-
Check Docker logs:
-
Check port conflicts:
-
Restart services:
Database Connection Errors¶
-
Check PostgreSQL is running:
-
Check DATABASE_URL in
.env: -
Test connection:
Redis Connection Errors¶
-
Check Redis is running:
-
Check REDIS_URL in
.env: -
Test connection:
Migration Errors¶
-
Check migration status:
-
Force rollback:
-
Re-run migrations:
API Errors¶
-
Check CORS configuration:
-
Check backend logs:
-
Test API endpoint:
Frontend Not Loading¶
-
Check store service:
-
Check Nginx configuration:
-
Check API base URL in frontend:
Deployment¶
Deploy to Production¶
- Prepare Environment Variables
Create production .env file:
- Build Docker Images
- Push to Registry (if using remote registry)
docker tag az-main-medusa:latest your-registry/az-main-medusa:latest
docker push your-registry/az-main-medusa:latest
- Deploy to Server
# Copy files to server
scp -r . user@server:/path/to/az-main
# SSH into server
ssh user@server
# Navigate to project
cd /path/to/az-main
# Start services
docker compose -f docker-compose.yml up -d
- Verify Deployment
# Check services
docker compose ps
# Check logs
docker compose logs -f
# Test endpoints
curl https://api.azhar.store/store/translations
Update Deployment¶
- Pull Latest Code
- Rebuild and Restart
- Run Migrations
- Verify
Rollback Deployment¶
- Revert Code
- Rebuild and Restart
- Rollback Database (if needed)
Maintenance Tasks¶
Daily Tasks¶
- Check service logs for errors
- Monitor disk space usage
- Review error rates
Weekly Tasks¶
- Review database performance
- Check backup integrity
- Review security logs
Monthly Tasks¶
- Update dependencies
- Review and rotate secrets
- Audit user access
- Test disaster recovery procedures
Security Tasks¶
Rotate Secrets¶
-
Generate new secrets:
-
Update
.envfile with new secrets -
Restart services:
Update Dependencies¶
SSL Certificate Renewal¶
If using Let's Encrypt with Certbot:
Performance Optimization¶
Clear Redis Cache¶
Vacuum Database¶
Rebuild Indexes¶
Emergency Procedures¶
Emergency Shutdown¶
Emergency Restart¶
Database Emergency Access¶
Force Kill Hung Process¶
Contact and Support¶
- Documentation: See
docs/directory - Issues: Open a GitHub issue
- Emergency: Contact system administrator