abmundi/database-commands-bundle
Symfony bundle to automate MySQL database dumps and imports via a db:dump console command and Capifony tasks. Creates timestamped dumps (with hard link to latest), supports download from remote and import into local production/testing environments.
Installation:
composer require abmundi/database-commands-bundle:dev-master
Register the bundle in config/bundles.php (Laravel 5.4+):
return [
// ...
Abmundi\DatabaseCommandsBundle\AbmundiDatabaseCommandsBundle::class => ['all' => true],
];
Verify Dependencies:
Ensure your server has mysql, mysqldump, and bunzip2 installed and available in $PATH.
First Use Case:
Dump your database to a .sql file:
php artisan db:dump
This generates a dump in var/dumps/ (default path). Check the README for other commands like db:restore, db:optimize, etc.
Database Dumps:
db:dump to create backups before migrations or deployments.
php artisan db:dump --path=custom/path --compress=1
db:dump via cron or Capistrano (see Capifony tasks below) for automated backups.Restoration:
php artisan db:restore --file=path/to/backup.sql
--drop to drop tables before restoring (useful for clean migrations):
php artisan db:restore --file=backup.sql --drop=1
Optimization:
db:optimize to repair and optimize tables:
php artisan db:optimize --tables=table1,table2
If using Capifony (or Capistrano), leverage the 5 provided tasks:
db:dump: Trigger a dump before deployment.db:restore: Restore from a dump post-deployment.db:optimize: Run optimizations during maintenance windows.db:backup: Alias for db:dump (check task list).db:cleanup: Remove old dumps (configure retention in config.yml).Example Capifony Task:
# config/deploy.rb
after "deploy:update_code", "deploy:db_dump"
namespace :deploy do
task :db_dump, roles: :db do
run "cd #{current_path} && php app/console db:dump --compress=1"
end
end
Artisan Command Aliases:
Add aliases in app/Console/Kernel.php for brevity:
protected $commands = [
// ...
'db:dump' => \Abmundi\DatabaseCommandsBundle\Command\DumpCommand::class,
];
Customizing Output Paths: Publish the config file to override defaults:
php artisan vendor:publish --provider="Abmundi\DatabaseCommandsBundle\AbmundiDatabaseCommandsBundle" --tag=config
Update config/abmundi_database_commands.php:
return [
'dump_path' => storage_path('app/dumps'),
'compression' => 'bzip2',
];
Event Listeners:
Trigger dumps/restores via Laravel events (e.g., deploying or deployed events in Forge/Senv):
// app/Providers/EventServiceProvider.php
public function boot()
{
if ($this->app->environment('production')) {
event(new Deploying);
}
}
Dependency Conflicts:
mysql, mysqldump, and bunzip2 are globally available. On Laravel Forge/Senv, ensure these are installed in the server’s base image or user’s $PATH.# In your deploy script
run "sudo apt-get install -y mysql-client coreutils" # Ubuntu/Debian
Permission Issues:
var/dumps/ (Symfony) or storage/app/dumps (Laravel). Ensure the web server user (e.g., www-data) has write permissions.run "chmod -R 775 storage/"
run "chown -R www-data:www-data storage/"
Large Database Dumps:
memory_limit or timeout. Increase limits in php.ini or use --chunk (if supported):
php artisan db:dump --memory=1024M
mysqldump directly with Laravel’s task scheduler:
php artisan schedule:run --command="mysqldump -uDB_USER -pDB_PASS DB_NAME > dump.sql"
Capifony Task Naming:
app/console path. In Laravel, override the task path in Capistrano:
set :console, "php artisan"
Command Output:
php artisan db:dump -v
storage/logs/laravel.log) for errors.Dump Integrity:
mysql -uDB_USER -pDB_PASS DB_NAME < dump.sql --force
bunzip2 dump.sql.bz2
Configuration Overrides:
config/abmundi_database_commands.php is loaded. Clear config cache if needed:
php artisan config:clear
Custom Commands:
Extend the bundle by creating a new command class (e.g., php artisan db:backup-to-s3) and reuse its logic:
use Abmundi\DatabaseCommandsBundle\Command\BaseCommand;
class S3DumpCommand extends BaseCommand {
protected function execute(InputInterface $input, OutputInterface $output) {
$dump = $this->dumpDatabase();
Storage::disk('s3')->put('dumps/backup.sql', $dump);
}
}
Pre/Post Hooks:
Override the bundle’s services to add logic before/after dumps. Example in config/services.php:
'abmundi.database_commands.dump.listener' => function ($container) {
return new class {
public function onDump(\Abmundi\DatabaseCommandsBundle\Event\DumpEvent $event) {
// Add custom logic (e.g., log dump start/end)
}
};
},
Database-Specific Logic:
The bundle defaults to MySQL. For PostgreSQL or SQLite, subclass BaseCommand and override getDumpCommand():
protected function getDumpCommand() {
return "pg_dump -U{$this->getDbUser()} {$this->getDbName()} > {$this->getDumpPath()}";
}
Testing:
Mock the Process component in PHPUnit to test commands:
$process = $this->createMock(\Symfony\Component\Process\Process::class);
$process->method('run')->willReturn(0);
$this->container->instance(\Symfony\Component\Process\Process::class, $process);
How can I help you explore Laravel packages today?