cdwv/cronos-database-dumper
Installation:
composer require cdwv/mysql-dumper-command-bundle cdwv/cronos-database-dumper
Register bundles in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
return [
// ...
CodeWave\CronosDatabaseDumperBundle\CodeWaveCronosDatabaseDumperBundle::class,
CodeWave\MysqlDumperCommandBundle\CodeWaveMysqlDumperCommandBundle::class,
];
Configure:
Override defaults in config/packages/code_wave_cronos_database_dumper.yaml:
code_wave_cronos_database_dumper:
minute: 0
hour: 3
dumps_location: "%kernel.project_dir%/var/backups"
php_path: "/usr/bin/php"
env: "%kernel.environment%"
clean_older_than: 7 # days
First Use Case: Trigger an immediate backup:
php bin/console cdwv:database:dump
Verify backups appear in dumps_location.
Database-Specific Backups:
Override config/packages/doctrine.yaml to define connections explicitly:
doctrine:
dbal:
connections:
main_db: { url: "mysql://user:pass@localhost/db1" }
legacy_db: { url: "mysql://user:pass@localhost/db2" }
The package will dump all configured connections.
Custom Backup Paths: Use environment variables for flexibility:
dumps_location: "%env(DB_BACKUP_PATH)%"
Set in .env:
DB_BACKUP_PATH=/custom/backup/path
Conditional Backups: Extend the command to skip backups during deployments:
// src/Command/DumpDatabaseCommand.php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DumpDatabaseCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
if ($this->getApplication()->getKernel()->getEnvironment() === 'deploy') {
$output->writeln('Skipping backup during deployment.');
return Command::SUCCESS;
}
// Original logic...
}
}
Post-Processing Hooks: Use Symfony’s event system to process backups after creation:
# config/services.yaml
services:
App\EventListener\DatabaseBackupListener:
tags:
- { name: kernel.event_listener, event: cdwv.database.dump.post, method: onBackup }
// src/EventListener/DatabaseBackupListener.php
class DatabaseBackupListener {
public function onBackup(DatabaseDumpEvent $event) {
$backupPath = $event->getBackupPath();
// Compress, notify, or upload logic here
}
}
Artisan Scheduling (Symfony 5.3+):
Replace crontab with Laravel-like scheduling in config/packages/framework.yaml:
framework:
console:
schedule:
'0 3 * * *': 'cdwv:database:dump'
Run php bin/console cache:clear and php bin/console cronos:run to execute.
Remote Storage:
Use post_dump event to sync backups to S3:
use Aws\S3\S3Client;
class S3BackupListener {
public function onBackup(DatabaseDumpEvent $event) {
$s3 = new S3Client([...]);
$s3->putObject([...]);
}
}
Missing mysql-client:
mysqldump: command not found.sudo apt-get install mysql-client # Debian/Ubuntu
sudo yum install mysql # CentOS/RHEL
Permission Issues:
Permission denied.dumps_location is writable by the web server user (e.g., www-data):
chown -R www-data:www-data /path/to/backups
chmod -R 755 /path/to/backups
Crontab Path Resolution:
__PATH_TO_YOUR_PROJECT__ not replaced in crontab.php_path and dumps_location:
php_path: "/var/www/project/bin/php"
dumps_location: "/var/www/project/var/backups"
Environment Mismatch:
dev environment despite env: prod config.--env in crontab or use kernel.environment in config.Large Database Timeouts:
mysqldump hangs or times out.0 3 * * * /usr/bin/php -d max_execution_time=3600 __PATH__/bin/console cdwv:database:dump
Dry Run: Test configuration without writing files:
php bin/console cdwv:database:dump --dry-run
Verbose Output: Enable debug mode for command details:
php bin/console cdwv:database:dump -vvv
Log Backups:
Add logging to DatabaseDumpCommand:
use Psr\Log\LoggerInterface;
class DatabaseDumpCommand {
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this->logger->info('Backup started for databases: ' . json_encode($this->getDatabaseConnections()));
// ...
}
}
Custom Dump Command:
Extend MysqlDumperCommand to support additional options (e.g., compression):
// src/Command/CustomDumpCommand.php
use CodeWave\MysqlDumperCommandBundle\Command\MysqlDumperCommand;
class CustomDumpCommand extends MysqlDumperCommand {
protected function configure() {
$this->addOption('compress', null, InputOption::VALUE_NONE, 'Compress backup with gzip');
}
protected function execute(InputInterface $input, OutputInterface $output) {
parent::execute($input, $output);
if ($input->getOption('compress')) {
$this->compressBackup($this->getBackupPath());
}
}
}
Database Filtering:
Override getDatabaseConnections() to exclude specific databases:
// src/Command/FilteredDumpCommand.php
use Doctrine\DBAL\Connection;
class FilteredDumpCommand extends MysqlDumperCommand {
protected function getDatabaseConnections() {
$connections = parent::getDatabaseConnections();
return array_filter($connections, fn(Connection $conn) => strpos($conn->getDatabase(), 'test_') === false);
}
}
Pre/Post Hooks: Dispatch events for custom logic:
// src/Event/DatabaseDumpEvent.php
class DatabaseDumpEvent {
private $backupPath;
public function __construct(string $backupPath) {
$this->backupPath = $backupPath;
}
public function getBackupPath(): string { return $this->backupPath; }
}
Dispatch in MysqlDumperCommand:
$event = new DatabaseDumpEvent($backupPath);
$this->dispatcher->dispatch(new DatabaseDumpEvent($event));
How can I help you explore Laravel packages today?