- How do I integrate this package into a Laravel project for automated database backups?
- Start by requiring the package via Composer (`composer require ifsnop/mysqldump-php:^2.11`), then create a custom Artisan command or service class to wrap the dumper. Use Laravel’s filesystem drivers (local/S3) to store backups, and schedule the command via `schedule:run` in your `app/Console/Kernel.php`. For async backups, dispatch the command to a queue.
- Does this package support incremental backups or only full database dumps?
- This package primarily supports full database dumps, but you can use the `--tables` or `--where` options to target specific tables or rows. For true incremental backups, consider combining this with Laravel’s `Schema::get()` or a custom solution that tracks changes via triggers or timestamps. There’s no built-in binary logging support like `mysqldump --master-data`.
- Can I use this for Laravel SaaS multi-tenant backups (e.g., per-tenant SQL exports)?
- Yes, you can filter backups by tenant using the `--where` clause (e.g., `--where='tenant_id = 1'`) or pre-process data with hooks. For large-scale SaaS, pair this with Laravel’s queue system to avoid timeouts. Store tenant-specific backups in separate files or folders, and use Laravel’s filesystem to manage paths dynamically.
- What Laravel versions and PHP requirements does this package support?
- The package works with Laravel 5.7+ and requires PHP 7.2+. Tested on PHP 7.3+ for stability, but older versions (5.3–7.3) are supported in legacy branches. Always pin to `^2.11` in `composer.json` to avoid breaking changes. Check your Laravel version’s PHP requirements first (e.g., Laravel 9 needs PHP 8.0+).
- How do I restore a backup created with this package in Laravel?
- Use the `MySqlDumper::restoreFromFile()` method to restore from a `.sql` or `.sql.gz` file. For Laravel integration, create a custom Artisan command (e.g., `php artisan db:restore`) that reads from storage (local/S3) and pipes the file to `PDO::exec()`. For production, test restores in staging first, and log failures to Laravel’s log channel or Sentry.
- Will this work in CI/CD pipelines (e.g., GitHub Actions) for pre-deploy backups?
- Absolutely. Replace `mysqldump` CLI calls with PHP code in your pipeline (e.g., `php artisan db:backup --storage=s3`). Use environment variables for credentials (via Laravel’s `.env`), and store backups in S3 or another Laravel-supported filesystem. For GitHub Actions, add a step like `run: php artisan db:backup --compress` before deployments.
- How do I handle large databases (e.g., 10GB+) without memory exhaustion?
- Use chunking with `--chunk-size=1000` and `--single-transaction` to avoid locks. For streaming, enable compression (`--compress`) and write directly to a file or S3 stream. In Laravel, dispatch the backup to a queue (e.g., `database` queue) to avoid HTTP timeouts. Monitor memory usage with `memory_get_usage()` and adjust chunk sizes if needed.
- Can I customize the backup output (e.g., exclude tables, modify data with hooks)?
- Yes, use the `--ignore-tables` option to exclude tables or `--where` to filter rows. For data transformation, leverage hooks via `MySqlDumper::addHook()` to modify queries or output. For example, you could anonymize PII before dumping. Hooks are chainable and run during the dump process.
- What are the alternatives to this package, and when should I choose them?
- Alternatives include `spatie/laravel-backup` (Laravel-specific, supports more storage backends) and `percona/percona-toolkit` (CLI-focused). Choose this package if you need pure PHP with no external dependencies, fine-grained control over dump settings, or compatibility with environments without `mysqldump`. Use `spatie/laravel-backup` if you prefer a Laravel-centric solution with built-in monitoring.
- How do I secure credentials when using this package in Laravel?
- Never hardcode credentials. Use Laravel’s `.env` file for database config (e.g., `DB_HOST`, `DB_PASSWORD`), and restrict file permissions. For production, integrate with a secrets manager like HashiCorp Vault or AWS Secrets Manager. Avoid logging credentials, and use Laravel’s `config('database')` to dynamically fetch connection details in your backup service.