/** * 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>'); } }