cdwv/database-dumper-command-bundle
Symfony bundle adding a console command to create database backups/dumps. Install via Composer, register the bundle, then run app/console cdwv:database:dump to generate a dump for your configured database.
Installation
composer require cdwv/database-dumper-command-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3/2):
CodeWave\DatabaseDumperCommandBundle\CodeWaveDatabaseDumperCommandBundle::class => ['all' => true],
First Use Case Run the dump command to generate a SQL backup:
php bin/console cdwv:database:dump
Outputs a .sql file in var/dumps/ (default path).
Configuration
Check config/packages/cdwv_database_dumper.yaml (Symfony 4+) or app/config/config.yml (Symfony 3/2) for default settings:
cdwv_database_dumper:
dump_path: '%kernel.project_dir%/var/dumps'
dump_filename: 'database_%date%.sql'
dump_compression: false
Scheduled Backups
Integrate with Symfony’s scheduler (e.g., cron or symfony/process) to run nightly:
# config/packages/schedule.yaml
schedule:
database_backup:
command: 'cdwv:database:dump'
cron: '0 2 * * *' # Daily at 2 AM
Custom Dump Filename Logic Extend the command to append environment-specific tags:
// src/Command/CustomDumpCommand.php
use Symfony\Component\Console\Command\Command;
use CodeWave\DatabaseDumperCommandBundle\Command\DatabaseDumpCommand;
class CustomDumpCommand extends DatabaseDumpCommand {
protected function configure() {
$this->setName('app:custom-dump');
$this->setDescription('Dump DB with custom filename');
}
protected function getDumpFilename() {
return 'backup_' . date('Y-m-d') . '_' . getenv('APP_ENV') . '.sql';
}
}
Post-Dump Actions
Chain commands using Symfony’s Process or EventDispatcher:
// src/EventListener/DumpListener.php
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use CodeWave\DatabaseDumperCommandBundle\Event\DumpEvent;
class DumpListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
DumpEvent::POST_DUMP => 'onPostDump',
];
}
public function onPostDump(DumpEvent $event) {
$dumpPath = $event->getDumpPath();
// Upload to S3, notify Slack, etc.
}
}
Laravel Adaptation
Use Symfony’s Console/Kernel as a bridge:
// bootstrap/app.php (Laravel)
$kernel = new Symfony\Component\HttpKernel\Kernel(
'dev',
false
);
$kernel->boot();
$command = new \CodeWave\DatabaseDumperCommandBundle\Command\DatabaseDumpCommand();
$command->run(new Symfony\Component\Console\Input\ArrayInput([]), new Symfony\Component\Console\Output\ConsoleOutput());
Environment-Specific Paths
Override dump_path in .env:
DUMP_PATH=/backups/production
Then update the config:
dump_path: '%env(DUMP_PATH)%'
Deprecated Symfony Version
symfony/process + mysqldump directly or migrate to spatie/laravel-backup (Laravel) or snc/redis-bundle for modern alternatives.No Compression by Default Enable gzip in config to reduce file size:
dump_compression: true
Permission Issues
Ensure var/dumps/ is writable:
mkdir -p var/dumps && chmod -R 775 var/dumps
Large Database Dumps
--quick flag in mysqldump (if supported) or split dumps:
dump_additional_options: '--quick --single-transaction'
max_execution_time or run via CLI with nohup.Verify Dump Path Check if the path exists and is writable:
// Debug in a command
$this->getHelper('dialog')->ask('Dump path exists?', $this->getDumpPath());
Log Dump Events Enable Symfony’s profiler or add logging:
# config/packages/monolog.yaml
handlers:
dump:
type: stream
path: var/log/dump.log
level: debug
Custom Dump Format
Override the dump() method to use pg_dump (PostgreSQL) or other tools:
protected function dump() {
$process = new Process(['pg_dump', '-U', $this->getDbUser(), $this->getDbName()]);
$process->run();
file_put_contents($this->getDumpPath(), $process->getOutput());
}
Pre/Post-Dump Hooks
Use Symfony’s EventDispatcher to trigger actions:
// src/EventSubscriber/DumpSubscriber.php
use CodeWave\DatabaseDumperCommandBundle\Event\DumpEvent;
class DumpSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
DumpEvent::PRE_DUMP => 'preDump',
DumpEvent::POST_DUMP => 'postDump',
];
}
public function preDump(DumpEvent $event) {
// Validate DB connection, send notification, etc.
}
}
Remote Storage Extend the bundle to upload dumps to S3/Backblaze:
use Aws\S3\S3Client;
class S3DumpCommand extends DatabaseDumpCommand {
protected function postDump() {
$s3 = new S3Client(['version' => 'latest']);
$s3->putObject([
'Bucket' => 'my-backups',
'Key' => 'dumps/' . basename($this->getDumpPath()),
'Body' => file_get_contents($this->getDumpPath()),
]);
}
}
dump_filename to avoid collisions:
dump_filename: 'backup_%Y-%m-%d_%H-%M-%S.sql'
mysqldump flags via dump_additional_options:
dump_additional_options: '--no-tablespaces --skip-comments'
How can I help you explore Laravel packages today?