/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * * @throws \InvalidArgumentException * @return int|void */ protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $this->isTypeAllowed(); $this->detectMagento($output); $this->dbHelper = $this->getHelper('database'); $this->showProgress = $input->getOption('format') == null; if ($input->getOption('table')) { $resolvedTables = array($this->dbHelper->resolveTables(array('@check'), array('check' => array('tables' => $input->getOption('table'))))); $tables = $resolvedTables[0]; } else { $tables = $this->dbHelper->getTables(); } $allTableStatus = $this->dbHelper->getTablesStatus(); $tableOutput = array(); /** @var \Symfony\Component\Console\Helper\ProgressHelper $progress */ $progress = $this->getHelper('progress'); if ($this->showProgress) { $progress->start($output, count($tables)); } $methods = array('InnoDB' => 1, 'MEMORY' => 1, 'MyISAM' => 1); foreach ($tables as $tableName) { if (isset($allTableStatus[$tableName]) && isset($methods[$allTableStatus[$tableName]['Engine']])) { $m = '_check' . $allTableStatus[$tableName]['Engine']; $tableOutput = array_merge($tableOutput, $this->{$m}($tableName)); } else { $tableOutput[] = array('table' => $tableName, 'operation' => 'not supported', 'type' => '', 'status' => ''); } $this->progressAdvance(); } if ($this->showProgress) { $progress->finish(); } $this->getHelper('table')->setHeaders(array('Table', 'Operation', 'Type', 'Status'))->renderByFormat($this->output, $tableOutput, $this->input->getOption('format')); }
/** * PHP alternative to dump database without exec * * @param InputInterface $input * @param OutputInterface $output * @throws \Exception */ private function createBackupWithoutExec(InputInterface $input, OutputInterface $output) { $magerun = $this->getMagerun(); $filePath = $this->getFilePath($input); $stripOptions = $input->getOption('strip') ?: '@development'; // Exec must be unavailable so use PHP alternative (match output) $dbHelper = new DatabaseHelper(); $dbHelper->setHelperSet($magerun->getHelperSet()); $dbHelper->detectDbSettings(new NullOutput()); $magerunConfig = $magerun->getConfig(); $stripTables = $dbHelper->resolveTables(explode(' ', $stripOptions), $dbHelper->getTableDefinitions($magerunConfig['commands']['N98\\Magento\\Command\\Database\\DumpCommand'])); $output->writeln(array('', $magerun->getHelperSet()->get('formatter')->formatBlock('Dump MySQL Database (without exec)', 'bg=blue;fg=white', true), '')); $dbSettings = $dbHelper->getDbSettings(); $username = (string) $dbSettings['username']; $password = (string) $dbSettings['password']; $dbName = (string) $dbSettings['dbname']; try { $output->writeln('<comment>No-data export for: <info>' . implode(' ', $stripTables) . '</info></comment>'); $output->writeln('<comment>Start dumping database <info>' . $dbSettings['dbname'] . '</info> to file <info>' . $filePath . '</info>'); // Dump Structure for tables that we are not to receive data from $dumpStructure = new Mysqldump(sprintf('%s;dbname=%s', $dbHelper->dsn(), $dbName), $username, $password, array('include-tables' => $stripTables, 'no-data' => true)); $dumpStructure->start($filePath . '.structure'); $dump = new Mysqldump(sprintf('%s;dbname=%s', $dbHelper->dsn(), $dbName), $username, $password, array('exclude-tables' => $stripTables)); $dump->start($filePath . '.data'); // Now merge two files $fhData = fopen($filePath . '.data', 'a+'); $fhStructure = fopen($filePath . '.structure', 'r'); if ($fhData && $fhStructure) { while (!feof($fhStructure)) { fwrite($fhData, fgets($fhStructure, 4096)); } } fclose($fhStructure); // Gzip rewind($fhData); $zfh = gzopen($filePath, 'wb'); while (!feof($fhData)) { gzwrite($zfh, fgets($fhData, 4096)); } gzclose($zfh); fclose($fhData); } catch (\Exception $e) { throw new \Exception("Unable to export database."); } }
/** * Create database backup in tmp directory. * Use magerun db:dump if available. Otherwise use php alternative if exec not available. * * @param InputInterface $input * @param OutputInterface $output * * @throws \Exception */ private function createBackup(InputInterface $input, OutputInterface $output) { $magerun = $this->getMagerun(); $filePath = $this->getFilePath($input); try { /** @var \N98\Magento\Command\Database\DumpCommand $dumpCommand */ $dumpCommand = $magerun->find("db:dump"); $dumpInput = new ArrayInput(array('filename' => $filePath, '--strip' => '@development', '--compression' => 'gzip')); if ($dumpCommand->run($dumpInput, $output)) { throw new \Exception("magerun db:dump failed to create backup.."); } } catch (\InvalidArgumentException $e) { // Exec must be unavailable so use PHP alternative (match output) $dbHelper = new DatabaseHelper(); $dbHelper->setHelperSet($magerun->getHelperSet()); $dbHelper->detectDbSettings(new NullOutput()); $stripTables = $dbHelper->resolveTables(explode(' ', '@development'), $dbHelper->getTableDefinitions($magerun->getConfig()['commands']['N98\\Magento\\Command\\Database\\DumpCommand'])); $output->writeln(array('', $magerun->getHelperSet()->get('formatter')->formatBlock('Dump MySQL Database (without exec)', 'bg=blue;fg=white', true), '')); $dbSettings = $dbHelper->getDbSettings(); $username = (string) $dbSettings['username']; $password = (string) $dbSettings['password']; $dbName = (string) $dbSettings['dbname']; try { $dump = new Mysqldump(sprintf('%s;dbname=%s', $dbHelper->dsn(), $dbName), $username, $password, array('compress' => Mysqldump::GZIP, 'exclude-tables' => $stripTables)); $output->writeln('<comment>No-data export for: <info>' . implode(' ', $stripTables) . '</info></comment>'); $output->writeln('<comment>Start dumping database <info>' . $dbSettings['dbname'] . '</info> to file <info>' . $filePath . '</info>'); $dump->start($filePath); } catch (\Exception $e) { throw new \Exception("Unable to export database."); } $output->writeln('<info>Finished</info>'); } }