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

Cronos Database Dumper Laravel Package

cdwv/cronos-database-dumper

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require cdwv/mysql-dumper-command-bundle cdwv/cronos-database-dumper
    

    Register bundles in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):

    return [
        // ...
        CodeWave\CronosDatabaseDumperBundle\CodeWaveCronosDatabaseDumperBundle::class,
        CodeWave\MysqlDumperCommandBundle\CodeWaveMysqlDumperCommandBundle::class,
    ];
    
  2. Configure: Override defaults in config/packages/code_wave_cronos_database_dumper.yaml:

    code_wave_cronos_database_dumper:
        minute: 0
        hour: 3
        dumps_location: "%kernel.project_dir%/var/backups"
        php_path: "/usr/bin/php"
        env: "%kernel.environment%"
        clean_older_than: 7  # days
    
  3. First Use Case: Trigger an immediate backup:

    php bin/console cdwv:database:dump
    

    Verify backups appear in dumps_location.


Implementation Patterns

Workflows

  1. Database-Specific Backups: Override config/packages/doctrine.yaml to define connections explicitly:

    doctrine:
        dbal:
            connections:
                main_db: { url: "mysql://user:pass@localhost/db1" }
                legacy_db: { url: "mysql://user:pass@localhost/db2" }
    

    The package will dump all configured connections.

  2. Custom Backup Paths: Use environment variables for flexibility:

    dumps_location: "%env(DB_BACKUP_PATH)%"
    

    Set in .env:

    DB_BACKUP_PATH=/custom/backup/path
    
  3. Conditional Backups: Extend the command to skip backups during deployments:

    // src/Command/DumpDatabaseCommand.php
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    
    class DumpDatabaseCommand extends Command {
        protected function execute(InputInterface $input, OutputInterface $output) {
            if ($this->getApplication()->getKernel()->getEnvironment() === 'deploy') {
                $output->writeln('Skipping backup during deployment.');
                return Command::SUCCESS;
            }
            // Original logic...
        }
    }
    
  4. Post-Processing Hooks: Use Symfony’s event system to process backups after creation:

    # config/services.yaml
    services:
        App\EventListener\DatabaseBackupListener:
            tags:
                - { name: kernel.event_listener, event: cdwv.database.dump.post, method: onBackup }
    
    // src/EventListener/DatabaseBackupListener.php
    class DatabaseBackupListener {
        public function onBackup(DatabaseDumpEvent $event) {
            $backupPath = $event->getBackupPath();
            // Compress, notify, or upload logic here
        }
    }
    

Integration Tips

  • Artisan Scheduling (Symfony 5.3+): Replace crontab with Laravel-like scheduling in config/packages/framework.yaml:

    framework:
        console:
            schedule:
                '0 3 * * *': 'cdwv:database:dump'
    

    Run php bin/console cache:clear and php bin/console cronos:run to execute.

  • Remote Storage: Use post_dump event to sync backups to S3:

    use Aws\S3\S3Client;
    
    class S3BackupListener {
        public function onBackup(DatabaseDumpEvent $event) {
            $s3 = new S3Client([...]);
            $s3->putObject([...]);
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Missing mysql-client:

    • Error: mysqldump: command not found.
    • Fix: Install as per README:
      sudo apt-get install mysql-client  # Debian/Ubuntu
      sudo yum install mysql              # CentOS/RHEL
      
  2. Permission Issues:

    • Error: Backups fail with Permission denied.
    • Fix: Ensure dumps_location is writable by the web server user (e.g., www-data):
      chown -R www-data:www-data /path/to/backups
      chmod -R 755 /path/to/backups
      
  3. Crontab Path Resolution:

    • Error: __PATH_TO_YOUR_PROJECT__ not replaced in crontab.
    • Fix: Use absolute paths in php_path and dumps_location:
      php_path: "/var/www/project/bin/php"
      dumps_location: "/var/www/project/var/backups"
      
  4. Environment Mismatch:

    • Error: Backups run in dev environment despite env: prod config.
    • Fix: Explicitly set --env in crontab or use kernel.environment in config.
  5. Large Database Timeouts:

    • Error: mysqldump hangs or times out.
    • Fix: Increase PHP timeout in crontab:
      0 3 * * * /usr/bin/php -d max_execution_time=3600 __PATH__/bin/console cdwv:database:dump
      

Debugging

  • Dry Run: Test configuration without writing files:

    php bin/console cdwv:database:dump --dry-run
    
  • Verbose Output: Enable debug mode for command details:

    php bin/console cdwv:database:dump -vvv
    
  • Log Backups: Add logging to DatabaseDumpCommand:

    use Psr\Log\LoggerInterface;
    
    class DatabaseDumpCommand {
        private $logger;
    
        public function __construct(LoggerInterface $logger) {
            $this->logger = $logger;
        }
    
        protected function execute(InputInterface $input, OutputInterface $output) {
            $this->logger->info('Backup started for databases: ' . json_encode($this->getDatabaseConnections()));
            // ...
        }
    }
    

Extension Points

  1. Custom Dump Command: Extend MysqlDumperCommand to support additional options (e.g., compression):

    // src/Command/CustomDumpCommand.php
    use CodeWave\MysqlDumperCommandBundle\Command\MysqlDumperCommand;
    
    class CustomDumpCommand extends MysqlDumperCommand {
        protected function configure() {
            $this->addOption('compress', null, InputOption::VALUE_NONE, 'Compress backup with gzip');
        }
    
        protected function execute(InputInterface $input, OutputInterface $output) {
            parent::execute($input, $output);
            if ($input->getOption('compress')) {
                $this->compressBackup($this->getBackupPath());
            }
        }
    }
    
  2. Database Filtering: Override getDatabaseConnections() to exclude specific databases:

    // src/Command/FilteredDumpCommand.php
    use Doctrine\DBAL\Connection;
    
    class FilteredDumpCommand extends MysqlDumperCommand {
        protected function getDatabaseConnections() {
            $connections = parent::getDatabaseConnections();
            return array_filter($connections, fn(Connection $conn) => strpos($conn->getDatabase(), 'test_') === false);
        }
    }
    
  3. Pre/Post Hooks: Dispatch events for custom logic:

    // src/Event/DatabaseDumpEvent.php
    class DatabaseDumpEvent {
        private $backupPath;
    
        public function __construct(string $backupPath) {
            $this->backupPath = $backupPath;
        }
    
        public function getBackupPath(): string { return $this->backupPath; }
    }
    

    Dispatch in MysqlDumperCommand:

    $event = new DatabaseDumpEvent($backupPath);
    $this->dispatcher->dispatch(new DatabaseDumpEvent($event));
    
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.
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
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager