Edit file File name : DEVELOPMENT.md Content :# Development Guide This guide is for developers who want to extend or contribute to the Database Backup Manager project. ## Architecture Overview ### Models - **DatabaseConfiguration**: Stores database credentials and settings - **BackupHistory**: Tracks all backup executions with status - **BackupSchedule**: Manages automated backup schedules - **GoogleDriveSetting**: Stores Google Drive OAuth tokens ### Services - **BackupService**: Core backup logic - `backupDatabase()`: Execute mysqldump and upload - `deleteOldBackups()`: Cleanup old backups - **GoogleDriveService**: Google Drive API operations - `handleCallback()`: OAuth token handling - `uploadFile()`: Upload backup to Drive - `deleteFile()`: Remove from Drive - `refreshAccessToken()`: Handle token refresh ### Controllers - **DashboardController**: Statistics and overview - **DatabaseConfigurationController**: CRUD for database configs - **BackupController**: Manual backups and schedules - **GoogleDriveController**: OAuth flow ### Commands - **RunScheduledBackups**: Executes scheduled backups ## Adding Features ### Adding a New Backup Destination (e.g., AWS S3) 1. Create a new service: `app/Services/S3BackupService.php` 2. Implement interface or base class 3. Add S3 configuration to `.env` 4. Update `BackupService` to support multiple destinations 5. Add migration for S3 settings table ### Adding Database Type Support (e.g., PostgreSQL) 1. Update `BackupService->executeMysqlDump()` to detect DB type 2. Add PostgreSQL command support using `pg_dump` 3. Test with PostgreSQL credentials ### Adding Backup Encryption 1. Create `app/Services/EncryptionService.php` 2. Use Laravel's `Crypt` facade 3. Store encryption key in `.env` 4. Update `BackupService` to encrypt before upload ### Adding Email Notifications 1. Create `app/Notifications/BackupCompletedNotification.php` 2. Use Laravel's notification system 3. Add email configuration to database 4. Update `RunScheduledBackups` command to send notifications ## Database Schema ### database_configurations ``` id, name, host, port, database, username, password, is_active, timestamps ``` ### backup_histories ``` id, database_configuration_id, filename, file_path, google_drive_file_id, file_size, status, error_message, backup_started_at, backup_completed_at, timestamps ``` ### backup_schedules ``` id, database_configuration_id, frequency, time, day_of_week, day_of_month, is_active, last_run_at, next_run_at, timestamps ``` ### google_drive_settings ``` id, access_token, refresh_token, folder_id, email, token_expires_at, is_connected, timestamps ``` ## Testing Create tests in `tests/` directory: ```php // tests/Feature/BackupTest.php use Tests\TestCase; use App\Models\DatabaseConfiguration; class BackupTest extends TestCase { public function test_can_create_backup() { $db = DatabaseConfiguration::factory()->create(); // Test backup logic } } ``` ## Code Style Follow PSR-12 standards: ```bash # Format code php artisan pint # Check standards ./vendor/bin/pint --test ``` ## Git Workflow ```bash # Create feature branch git checkout -b feature/new-feature # Make changes and commit git add . git commit -m "Add new feature" # Push and create PR git push origin feature/new-feature ``` ## Performance Optimization ### Large Backup Optimization For databases > 1GB: - Implement streaming upload to Google Drive - Add progress tracking - Consider chunked uploads ### Query Optimization ```php // Use eager loading $backups = BackupHistory::with('databaseConfiguration') ->latest() ->paginate(20); ``` ## Logging Add logging for debugging: ```php use Illuminate\Support\Facades\Log; Log::info('Backup started', ['database' => $db->name]); Log::error('Backup failed', ['error' => $e->getMessage()]); ``` ## API Development If adding REST API endpoints: ```php // routes/api.php Route::apiResource('backups', BackupApiController::class); // Use API resources for responses return new BackupResource($backup); ``` ## Security Considerations ### When Adding New Features: 1. Validate all user input 2. Sanitize database queries (use ORM) 3. Check for SQL injection 4. Validate file paths (prevent directory traversal) 5. Use HTTPS for external APIs 6. Never log sensitive data (passwords, tokens) 7. Implement rate limiting for API endpoints ### Common Vulnerabilities to Avoid: - ✅ Use `escapeshellarg()` for shell commands - ✅ Validate file uploads - ✅ Use Laravel's CSRF protection - ✅ Hash passwords properly - ✅ Implement access control ## Troubleshooting Development ### Service Provider Issues If new services aren't registering: ```bash php artisan config:clear php artisan cache:clear ``` ### Migration Issues ```bash # Rollback and retry php artisan migrate:rollback php artisan migrate ``` ### Class Not Found ```bash # Regenerate autoloader composer dump-autoload ``` ## Documentation Standards When adding features, document: 1. What the feature does 2. How to use it 3. Configuration needed 4. Potential issues and solutions ## Release Checklist Before releasing: - [ ] All tests pass - [ ] Code formatted with pint - [ ] Documentation updated - [ ] CHANGELOG.md updated - [ ] No security vulnerabilities - [ ] Performance tested ## Resources - [Laravel Documentation](https://laravel.com/docs) - [Google Drive API Docs](https://developers.google.com/drive/api) - [PHP Best Practices](https://www.php-fig.org/psr/psr-12/) ## Contributing 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Write tests 5. Update documentation 6. Submit a pull request --- Happy coding! 🚀 Save