5 * Contains \Drupal\Console\Command\Database\DumpCommand.
8 namespace Drupal\Console\Command\Database;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\ConnectTrait;
17 use Drupal\Console\Core\Utils\ShellProcess;
18 use Drupal\Console\Core\Style\DrupalStyle;
20 class DumpCommand extends Command
30 protected $shellProcess;
33 * DumpCommand constructor.
36 * @param ShellProcess $shellProcess
38 public function __construct(
40 ShellProcess $shellProcess
42 $this->appRoot = $appRoot;
43 $this->shellProcess = $shellProcess;
44 parent::__construct();
50 protected function configure()
53 ->setName('database:dump')
54 ->setDescription($this->trans('commands.database.dump.description'))
57 InputArgument::OPTIONAL,
58 $this->trans('commands.database.dump.arguments.database'),
64 InputOption::VALUE_OPTIONAL,
65 $this->trans('commands.database.dump.options.file')
70 InputOption::VALUE_NONE,
71 $this->trans('commands.database.dump.options.gz')
73 ->setHelp($this->trans('commands.database.dump.help'));
79 protected function execute(InputInterface $input, OutputInterface $output)
81 $io = new DrupalStyle($input, $output);
83 $database = $input->getArgument('database');
84 $file = $input->getOption('file');
85 $learning = $input->getOption('learning');
86 $gz = $input->getOption('gz');
88 $databaseConnection = $this->resolveConnection($io, $database);
91 $date = new \DateTime();
95 $databaseConnection['database'],
96 $date->format('Y-m-d-h-i-s')
102 if ($databaseConnection['driver'] == 'mysql') {
104 'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
105 $databaseConnection['username'],
106 $databaseConnection['password'],
107 $databaseConnection['host'],
108 $databaseConnection['port'],
109 $databaseConnection['database'],
112 } elseif ($databaseConnection['driver'] == 'pgsql') {
114 'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
115 $databaseConnection['password'],
116 $databaseConnection['username'],
117 $databaseConnection['host'],
118 $databaseConnection['port'],
119 $databaseConnection['database'],
125 $io->commentBlock($command);
128 if ($this->shellProcess->exec($command, $this->appRoot)) {
131 if (substr($file, -3) != '.gz') {
132 $resultFile = $file . ".gz";
142 if ($resultFile != $file) {
150 $this->trans('commands.database.dump.messages.success'),