Installation
composer require bastsys/backup-bundle
Add to config/bundles.php (Symfony):
return [
// ...
Bastsys\BackupBundle\BastsysBackupBundle::class => ['all' => true],
];
Configuration
Edit config/packages/bastsys_backup.yaml (Symfony) or publish defaults:
php bin/console bastsys:backup:dump --help
Key config fields:
bastsys_backup:
storage:
type: 'local' # or 's3', 'ftp', etc.
path: '%kernel.project_dir%/backups'
databases: ['%env(DATABASE_URL)%']
files: ['%kernel.project_dir%/storage', '%kernel.project_dir%/public/uploads']
First Backup
php bin/console bastsys:backup:dump
Outputs to backups/[timestamp]_backup.zip.
Cron Job
Add to crontab (Linux):
0 3 * * * cd /path/to/project && php bin/console bastsys:backup:dump >> /dev/null 2>&1
(Runs daily at 3 AM.)
Verify Backup
php bin/console bastsys:backup:list
Lists all backups with timestamps.
Define Backup Scope
In config/packages/bastsys_backup.yaml:
bastsys_backup:
databases:
- '%env(DATABASE_URL)%'
- 'mysql://user:pass@localhost/secondary_db'
files:
- '%kernel.project_dir%/storage/app'
- '%kernel.project_dir%/public/media'
storage:
type: 's3'
bucket: 'my-backup-bucket'
credentials:
key: '%env(AWS_ACCESS_KEY_ID)%'
secret: '%env(AWS_SECRET_ACCESS_KEY)%'
Customize Backup Command Extend the bundle’s command (e.g., add pre-backup checks):
// src/Command/CustomBackupCommand.php
namespace App\Command;
use Bastsys\BackupBundle\Command\BackupCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CustomBackupCommand extends BackupCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
// Pre-backup logic (e.g., validate DB connections)
$this->validateEnvironment($output);
parent::execute($input, $output);
}
private function validateEnvironment(OutputInterface $output)
{
// Custom validation
}
}
Register in services.yaml:
services:
App\Command\CustomBackupCommand:
tags: ['console.command']
Post-Backup Actions Use events to trigger actions after backup:
# config/packages/bastsys_backup.yaml
bastsys_backup:
events:
post_backup: ['App\EventListener\BackupListener']
Listener example:
// src/EventListener/BackupListener.php
namespace App\EventListener;
use Bastsys\BackupBundle\Event\BackupEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BackupListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'bastsys.backup.post' => 'onBackupComplete',
];
}
public function onBackupComplete(BackupEvent $event)
{
$backupPath = $event->getBackupPath();
// Send Slack notification, log, etc.
}
}
Laravel-Specific Setup
DATABASE_URL is in .env:
DATABASE_URL="mysql://user:pass@localhost/db_name"
config/backup.php (if using Laravel’s config system).Excluding Files
Use .gitignore-style patterns in config:
bastsys_backup:
files:
- '%kernel.project_dir%/storage/app'
- '!%kernel.project_dir%/storage/app/logs' # Exclude logs
Remote Storage
Configure S3/FTP in config/packages/bastsys_backup.yaml:
storage:
type: 's3'
bucket: 'my-backup-bucket'
credentials:
key: '%env(AWS_ACCESS_KEY_ID)%'
secret: '%env(AWS_SECRET_ACCESS_KEY)%'
region: 'eu-west-1'
Backup Retention
Use the bastsys:backup:prune command to clean old backups:
php bin/console bastsys:backup:prune --days=7
Keeps only backups from the last 7 days.
Database Connection Issues
DATABASE_URL is correct and the user has SELECT permissions on all tables.php bin/console bastsys:backup:dump --verbose to see SQL errors.File Permissions
www-data) has write access to the backup directory:
chmod -R 775 %kernel.project_dir%/backups
chown -R www-data:www-data %kernel.project_dir%/backups
Large File Backups
storage/app/public with 100K files).php.ini:
memory_limit = 2G
--chunk-size flag (if supported):
php bin/console bastsys:backup:dump --chunk-size=100
Symfony vs. Laravel Quirks
DATABASE_URL format may not work out-of-the-box.databases:
- 'mysql://user:pass@127.0.0.1/db_name?unix_socket=/var/run/mysqld/mysqld.sock'
Enable Verbose Output
php bin/console bastsys:backup:dump -v
Shows detailed logs for each step (database dump, file archiving, storage upload).
Check Backup Contents Extract a backup to verify:
unzip backups/2023-01-01_120000_backup.zip -d /tmp/backup_test
Inspect /tmp/backup_test/database/ and /tmp/backup_test/files/.
Log Backup Events
Enable monolog in config/packages/monolog.yaml:
handlers:
backup:
type: stream
path: "%kernel.logs_dir%/backup.log"
level: debug
Then check var/log/backup.log (Symfony) or storage/logs/backup.log (Laravel).
Custom Backup Format
Override the BackupService to change ZIP compression or add encryption:
// src/Service/CustomBackupService.php
namespace App\Service;
use Bastsys\BackupBundle\Service\BackupService as BaseBackupService;
class CustomBackupService extends BaseBackupService
{
protected function createArchive(string $sourceDir, string $destination): void
{
// Custom logic (e.g., use Phar instead of ZipArchive)
}
}
Bind in services.yaml:
services:
Bastsys\BackupBundle\Service\BackupService:
alias: App\Service\CustomBackupService
Add Pre/Post Hooks
Use the BackupEvent to inject logic:
// src/EventListener/EncryptBackupListener.php
namespace App\EventListener;
use Bastsys\BackupBundle\Event\BackupEvent;
class EncryptBackupListener
{
public function onBackupComplete(BackupEvent $event)
{
$backupPath = $event->getBackupPath();
// Encrypt $backupPath using Open
How can I help you explore Laravel packages today?