Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Backup Symfony Bundle Laravel Package

captjm/backup-symfony-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require captjm/backup-symfony-bundle
    

    Ensure your project meets requirements: PHP ≥8.0, Symfony ≥5.4.

  2. Configure Routes Add to config/routes.yaml:

    captjm_backup:
        resource: '../vendor/captjm/backup-symfony-bundle/src/Controller/'
        type: annotation
    
  3. 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']
    
  4. 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');
    }
    
  5. 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).


Implementation Patterns

Core Workflow

  1. Backup Trigger

    • Use the admin menu link (Backup) to initiate a backup via the controller.
    • Extend the controller to support custom triggers (e.g., cron jobs, CLI commands).
  2. Backup Customization

    • Override the backup() method in CaptjmBackupSymfonyController to:
      • Modify dump file naming (e.g., include timestamps):
        $filename = 'backup_' . date('Y-m-d_His') . '.sql';
        
      • Add pre/post-backup logic (e.g., notifications, cleanup):
        $this->notifyTeam('Backup started');
        // ... backup logic ...
        $this->notifyTeam('Backup completed');
        
  3. Storage Integration

    • Redirect backups to cloud storage (S3, FTP) by extending the controller:
      use Aws\S3\S3Client;
      $s3 = new S3Client([/* config */]);
      $s3->putObject([/* upload backup */]);
      
  4. Scheduled Backups

    • Use Symfony’s 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
      
  5. 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();
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Database Connection Issues

    • Ensure captjm.database_url in services.yaml matches your .env DATABASE_URL.
    • Debug with:
      php bin/console doctrine:database:status
      
  2. File Permissions

    • Backups fail silently if the target directory lacks write permissions. Set:
      chmod -R 775 var/backups
      
  3. Large Database Dumps

    • Default mysqldump may time out. Adjust PHP settings:
      max_execution_time = 300
      memory_limit = 1G
      
    • For huge databases, use --single-transaction in the dump command.
  4. Admin Menu Not Showing

    • Verify DashboardController is properly autowired and the configureMenuItems() method is called.
    • Clear cache:
      php bin/console cache:clear
      

Debugging Tips

  1. 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));
    
  2. Test Locally First

    • Use a local database (e.g., SQLite) to test backup logic before deploying to production.
  3. Environment-Specific Config

    • Override captjm.database_url per environment:
      # config/packages/dev/captjm_backup.yaml
      parameters:
          captjm.database_url: '%env(DATABASE_URL)%'
      

Extension Points

  1. Backup Formats

    • Extend to support JSON/CSV exports by modifying the controller’s backup() method.
  2. Encryption

    • Integrate with Defuse/PHP-Encryption to encrypt backups before storage:
      $encrypted = \Defuse\Crypto\File::encrypt($filePath, $key);
      
  3. Retention Policy

    • Add logic to delete old backups (e.g., keep only the last 7 days):
      $backups = glob('var/backups/*.sql');
      array_map('unlink', array_diff($backups, array_slice($backups, -7)));
      
  4. Multi-Database Support

    • Loop through multiple DATABASE_URLs in services.yaml and dump each:
      $databases = $this->getParameter('captjm.databases');
      foreach ($databases as $db) {
          $this->dumpDatabase($db['url'], $db['name']);
      }
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui