# ELMIS Backup System - Command Reference

## 🚀 Quick Start Commands

### Setup & Configuration
```bash
# Run setup script (Windows)
setup-backup.bat

# Run setup script (Linux/Mac)
chmod +x setup-backup.sh && ./setup-backup.sh

# Test entire system
php artisan backup:test --verbose

# Setup Google Drive integration (interactive)
php artisan backup:setup-google-drive
```

## 📦 Backup Operations

### Create Backups
```bash
# Create database backup (default: compressed, phpMyAdmin importable)
php artisan backup:database

# Create uncompressed SQL backup (larger file size)
php artisan backup:database --format=sql

# Create both SQL and compressed formats
php artisan backup:database --format=both

# Create backup with notification (recommended)
php artisan backup:database --notify

# Create backup with specific storage and notification
php artisan backup:database --storage=google --notify
php artisan backup:database --storage=cpanel --notify

# Create backup with notification and cleanup
php artisan backup:database --notify --cleanup

# Full application backup (using Spatie package)
php artisan backup:run

# Backup with custom disk
php artisan backup:run --only-db --disk=backup_google_drive
```

### Backup Status & Monitoring
```bash
# Show backup status overview
php artisan backup:status

# Show detailed backup information
php artisan backup:status --detailed

# Check specific storage
php artisan backup:status --storage=google --detailed

# Monitor backup health (Spatie command)
php artisan backup:monitor
```

## 📂 Backup Management

### List Backups
```bash
# List local backups
php artisan backup:restore --list
# List remote backups
php artisan backup:restore --list --storage=google
php artisan backup:restore --list --storage=cpanel

### Restore Operations
```bash
# Interactive restore (shows available backups)
php artisan backup:restore

# Restore specific file
php artisan backup:restore filename.sql.gz

# List available backup files
php artisan backup:restore --list

# Restore from specific storage
php artisan backup:restore --storage=google
php artisan backup:restore --storage=cpanel

# Force restore (skip confirmation)
php artisan backup:restore filename.sql.gz --force
```

### phpMyAdmin Import
```bash
# Default compressed backups (.sql.gz) can be directly imported in phpMyAdmin:
# 1. Open phpMyAdmin in your browser
# 2. Select your database (e.g., '2025_elmis')
# 3. Click "Import" tab
# 4. Choose File: Select .sql.gz file from storage/app/backups/
# 5. Format: Auto-detects as "SQL"
# 6. Click "Go" - phpMyAdmin automatically decompresses and imports!

# File locations:
# Windows: D:\xampp\htdocs\2025\Full Stack\ELMIS\backend\storage\app\backups\
# Linux: /path/to/project/storage/app/backups/
```

### Cleanup Operations
```bash
# Clean old backups (Spatie command)
php artisan backup:clean

# Clean with specific disk
php artisan backup:clean --disk=backup_local
```

## ⚙️ Testing & Diagnostics

### System Tests
```bash
# Test all components
php artisan backup:test

# Test specific storage
php artisan backup:test --storage=local
php artisan backup:test --storage=cpanel
php artisan backup:test --storage=google

# Test without creating actual backup
php artisan backup:test --skip-backup

# Verbose testing output (use Laravel's built-in verbose flag)
php artisan backup:test -v
php artisan backup:test --verbose
```

### Schedule Testing
```bash
# List scheduled tasks
php artisan schedule:list

# Test schedule (development)
php artisan schedule:work

# Run scheduled commands manually
php artisan schedule:run
```

## 🔧 Environment Configuration

### Required .env Variables
```env
# Basic Configuration
BACKUP_STORAGE=local              # local, cpanel, google
BACKUP_NAME="ELMIS-Database"
BACKUP_MAIL_TO=admin@elmis.afga.org

# Local Storage (default)
BACKUP_DISK=local

# cPanel/FTP Configuration
FTP_HOST=ftp.yourdomain.com
FTP_USERNAME=your-username
FTP_PASSWORD=your-password
FTP_PORT=21
FTP_ROOT=/public_html/backups

# Google Drive Configuration
GOOGLE_DRIVE_CLIENT_ID=your-client-id
GOOGLE_DRIVE_CLIENT_SECRET=your-client-secret  
GOOGLE_DRIVE_REFRESH_TOKEN=your-refresh-token
GOOGLE_DRIVE_FOLDER=ELMIS-Backups
```

## 📅 Scheduled Operations

### Default Schedule
- **00:00** - Daily database backup (`backup:database`)
- **01:00** - Full application backup (`backup:run`)
- **02:00** - Weekly cleanup (`backup:clean`) - Sundays only
- **03:00** - Daily health monitor (`backup:monitor`)

### Production Cron Setup
```bash
# Add to crontab (crontab -e)
* * * * * cd /path/to/elmis/backend && php artisan schedule:run >> /dev/null 2>&1

# Or for specific user
* * * * * www-data cd /path/to/elmis/backend && php artisan schedule:run >> /dev/null 2>&1
```

## 🔍 Log Files & Monitoring

### Log Locations
```bash
# Backup operation logs
storage/logs/backup.log

# Spatie backup logs  
storage/logs/spatie-backup.log

# Cleanup operation logs
storage/logs/backup-cleanup.log

# Health monitoring logs
storage/logs/backup-monitor.log

# General application logs
storage/logs/laravel.log
```

### Monitor Commands
```bash
# View recent backup logs
tail -f storage/logs/backup.log

# Check Laravel logs for errors
tail -f storage/logs/laravel.log

# View all backup files
ls -la storage/app/backups/

# Check disk space
df -h storage/app/backups/
```

## 🆘 Troubleshooting Commands

### Permission Issues
```bash
# Windows
icacls storage\app\backups /grant Everyone:(OI)(CI)F

# Linux/Mac
chmod -R 755 storage/
chmod -R 777 storage/app/backups/
```

### Database Connection Test
```bash
# Test MySQL connection
php artisan tinker
# Then run: DB::connection()->getPdo();
```

### Storage Connection Tests
```bash
# Test specific storage backend
php artisan backup:test --storage=cpanel --verbose
php artisan backup:test --storage=google --verbose
```

### Clear Cache & Config
```bash
# Clear application cache
php artisan cache:clear
php artisan config:clear
php artisan route:clear

# Recreate config cache
php artisan config:cache
```

## 🚨 Emergency Recovery

### Quick Database Restore
```bash
# 1. List available backups
php artisan backup:restore --list

# 2. Restore latest backup
php artisan backup:restore

# 3. Or restore specific backup
php artisan backup:restore elmis_backup_YYYY-MM-DD_HH-MM-SS.sql.gz --force
```

### Manual Database Operations
```bash
# Create emergency backup before changes
mysqldump -u username -p database_name > emergency_backup.sql

# Restore from SQL file
mysql -u username -p database_name < backup_file.sql
```

## 📊 Performance & Optimization

### Large Database Optimization
```bash
# Use single transaction for InnoDB tables
# Add to config/backup.php:
'dump' => [
    'useSingleTransaction' => true,
]

# Exclude large/temporary tables
'dump' => [
    'excludeTables' => [
        'activity_logs',
        'sessions',
        'cache'
    ]
]
```

### Storage Space Management
```bash
# Check backup sizes
du -sh storage/app/backups/*

# Configure retention policy in config/backup.php
'keep_all_backups_for_days' => 7,
'keep_daily_backups_for_days' => 16,
'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
```

---

💡 **Pro Tips:**
- Always test restore process periodically
- Monitor backup file sizes and storage usage
- Set up email notifications for backup failures
- Keep multiple storage destinations for redundancy
- Document your backup and restore procedures

🔗 **Additional Resources:**
- `BACKUP_SETUP.md` - Detailed setup guide
- `config/backup.php` - Main configuration file
- [Spatie Laravel Backup Documentation](https://spatie.be/docs/laravel-backup)
