captjm/backup-symfony-bundle
Installation
composer require captjm/backup-symfony-bundle
Ensure your project meets requirements: PHP ≥8.0, Symfony ≥5.4.
Configure Routes
Add to config/routes.yaml:
captjm_backup:
resource: '../vendor/captjm/backup-symfony-bundle/src/Controller/'
type: annotation
Configure Services
Update config/services.yaml with your DATABASE_URL:
parameters:
captjm.database_url: '%env(DATABASE_URL)%'
services:
Captjm\BackupSymfonyBundle\Controller\CaptjmBackupSymfonyController:
tags: ['controller.service_arguments']
arguments: ['@parameter_bag']
Add Admin Menu Link
In Controller/Admin/DashboardController.php, inject the backup route into the admin menu:
public function configureMenuItems(): iterable {
yield MenuItem::linkToRoute('Backup', 'fas fa-download', 'captjm_backup');
}
First Use Case
Access /backup (or your configured route) to trigger a database backup. The bundle generates a .sql file in a default location (check src/Controller/CaptjmBackupSymfonyController.php for logic).
Backup Trigger
Backup) to initiate a backup via the controller.Backup Customization
backup() method in CaptjmBackupSymfonyController to:
$filename = 'backup_' . date('Y-m-d_His') . '.sql';
$this->notifyTeam('Backup started');
// ... backup logic ...
$this->notifyTeam('Backup completed');
Storage Integration
use Aws\S3\S3Client;
$s3 = new S3Client([/* config */]);
$s3->putObject([/* upload backup */]);
Scheduled Backups
CronBundle or EasyAdmin schedules to automate backups:
# config/packages/easy_admin.yaml
cron:
backup_daily:
command: 'app:backup' # Create a custom command (see below)
schedule: '0 2 * * *' # Daily at 2 AM
Custom Commands Create a console command for CLI backups:
php bin/console make:command BackupCommand
Inject the controller or use the bundle’s logic directly:
use Captjm\BackupSymfonyBundle\Service\BackupService;
class BackupCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
$backupService = $this->getBackupService();
$backupService->runBackup();
}
}
Database Connection Issues
captjm.database_url in services.yaml matches your .env DATABASE_URL.php bin/console doctrine:database:status
File Permissions
chmod -R 775 var/backups
Large Database Dumps
mysqldump may time out. Adjust PHP settings:
max_execution_time = 300
memory_limit = 1G
--single-transaction in the dump command.Admin Menu Not Showing
DashboardController is properly autowired and the configureMenuItems() method is called.php bin/console cache:clear
Log Backup Output Redirect dump output to a log for debugging:
$command = "mysqldump -u{$user} -p{$password} {$database} > {$file}";
exec($command . " 2>&1", $output, $return);
file_put_contents('var/log/backup.log', implode("\n", $output));
Test Locally First
Environment-Specific Config
captjm.database_url per environment:
# config/packages/dev/captjm_backup.yaml
parameters:
captjm.database_url: '%env(DATABASE_URL)%'
Backup Formats
backup() method.Encryption
Defuse/PHP-Encryption to encrypt backups before storage:
$encrypted = \Defuse\Crypto\File::encrypt($filePath, $key);
Retention Policy
$backups = glob('var/backups/*.sql');
array_map('unlink', array_diff($backups, array_slice($backups, -7)));
Multi-Database Support
DATABASE_URLs in services.yaml and dump each:
$databases = $this->getParameter('captjm.databases');
foreach ($databases as $db) {
$this->dumpDatabase($db['url'], $db['name']);
}
How can I help you explore Laravel packages today?