/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $database = $input->getArgument('db'); $dumpFile = $input->getArgument('file'); if (!is_file($dumpFile) || !is_readable($dumpFile)) { $output->writeln('<p-error>File is not readable</p-error>'); return 1; } $dumpFileType = PhpUtility::getMimeType($dumpFile); $output->writeln('<h2>Restoring dump "' . $dumpFile . '" into database "' . $database . '"</h2>'); if (DatabaseConnection::databaseExists($database)) { // Dropping $output->writeln('<p>Dropping database</p>'); $query = 'DROP DATABASE IF EXISTS ' . DatabaseConnection::sanitizeSqlDatabase($database); DatabaseConnection::exec($query); } // Creating $output->writeln('<p>Creating database</p>'); $query = 'CREATE DATABASE ' . DatabaseConnection::sanitizeSqlDatabase($database); DatabaseConnection::exec($query); // Inserting putenv('USER='******'MYSQL_PWD=' . DatabaseConnection::getDbPassword()); $commandMysql = new CommandBuilder('mysql', '--user=%s %s --one-database', array(DatabaseConnection::getDbUsername(), $database)); // Set server connection details if ($input->getOption('host')) { $commandMysql->addArgumentTemplate('-h %s', $input->getOption('host')); } if ($input->getOption('port')) { $commandMysql->addArgumentTemplate('-P %s', $input->getOption('port')); } $commandFile = new CommandBuilder(); $commandFile->addArgument($dumpFile); $commandFile->addPipeCommand($commandMysql); switch ($dumpFileType) { case 'application/x-bzip2': $output->writeln('<p>Using BZIP2 decompression</p>'); $commandFile->setCommand('bzcat'); break; case 'application/gzip': case 'application/x-gzip': $output->writeln('<p>Using GZIP decompression</p>'); $commandFile->setCommand('gzcat'); break; case 'application/x-lzma': case 'application/x-xz': $output->writeln('<p>Using LZMA decompression</p>'); $commandFile->setCommand('xzcat'); break; default: $output->writeln('<p>Using plaintext (no decompression)</p>'); $commandFile->setCommand('cat'); break; } $output->writeln('<p>Reading dump</p>'); $commandFile->executeInteractive(); $output->writeln('<h2>Database "' . $database . '" restored</h2>'); return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $database = $input->getArgument('db'); $dumpFile = $input->getArgument('file'); $filter = $input->getOption('filter'); if (!DatabaseConnection::databaseExists($database)) { $output->writeln('<p-error>Database "' . $database . '" does not exists</p-error>'); return 1; } $output->writeln('<h2>Dumping database "' . $database . '" into file "' . $dumpFile . '"</h2>'); $fileExt = pathinfo($dumpFile, PATHINFO_EXTENSION); // Inserting putenv('USER='******'MYSQL_PWD=' . DatabaseConnection::getDbPassword()); $commandCompressor = null; switch ($fileExt) { case 'bz': case 'bz2': case 'bzip2': $output->writeln('<p>Using BZIP2 compression</p>'); $commandCompressor = new CommandBuilder('bzip2'); break; case 'gz': case 'gzip': $output->writeln('<p>Using GZIP compression</p>'); $commandCompressor = new CommandBuilder('gzip'); break; case 'lzma': case 'lz': case 'xz': $output->writeln('<p>Using LZMA compression</p>'); $commandCompressor = new CommandBuilder('xz'); $commandCompressor->addArgument('--compress')->addArgument('--stdout'); break; } $command = new CommandBuilder('mysqldump', '--user=%s %s --single-transaction', array(DatabaseConnection::getDbUsername(), $database)); // Set server connection details if ($input->getOption('host')) { $command->addArgumentTemplate('-h %s', $input->getOption('host')); } if ($input->getOption('port')) { $command->addArgumentTemplate('-P %s', $input->getOption('port')); } if (!empty($filter)) { $command = $this->addFilterArguments($command, $database, $filter); } if (!empty($commandCompressor)) { $command->addPipeCommand($commandCompressor); $commandCompressor->setOutputRedirectToFile($dumpFile); } else { $output->writeln('<p>Using no compression</p>'); $command->setOutputRedirectToFile($dumpFile); } $command->executeInteractive(); $output->writeln('<h2>Database "' . $database . '" stored to "' . $dumpFile . '"</h2>'); }
/** * Create new mysqldump command * * @param null|string $database Database name * * @return RemoteCommandBuilder */ protected function createLocalMySqlDumpCommand($database = null) { $command = new RemoteCommandBuilder('mysqldump'); // Add username if (DatabaseConnection::getDbUsername()) { $command->addArgumentTemplate('-u%s', DatabaseConnection::getDbUsername()); } // Add password if (DatabaseConnection::getDbPassword()) { $command->addArgumentTemplate('-p%s', DatabaseConnection::getDbPassword()); } // Add hostname if (DatabaseConnection::getDbHostname()) { $command->addArgumentTemplate('-h%s', DatabaseConnection::getDbHostname()); } // Add hostname if (DatabaseConnection::getDbPort()) { $command->addArgumentTemplate('-P%s', DatabaseConnection::getDbPort()); } // Add custom options if ($this->contextConfig->exists('mysql.mysqldump.option')) { $command->addArgumentRaw($this->contextConfig->get('mysql.mysqldump.option')); } if ($database !== null) { $command->addArgument($database); } // Transfer compression switch ($this->contextConfig->get('mysql.compression')) { case 'bzip2': // Add pipe compressor (bzip2 compressed transfer via ssh) $command->addPipeCommand(new CommandBuilder('bzip2', '--compress --stdout')); break; case 'gzip': // Add pipe compressor (gzip compressed transfer via ssh) $command->addPipeCommand(new CommandBuilder('gzip', '--stdout')); break; } return $command; }