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 Drupal\Console\Core\Command\Command;
15 use Drupal\Console\Command\Shared\ConnectTrait;
16 use Drupal\Console\Core\Utils\ShellProcess;
18 class DumpCommand extends Command
27 protected $shellProcess;
30 * DumpCommand constructor.
33 * @param ShellProcess $shellProcess
35 public function __construct(
37 ShellProcess $shellProcess
39 $this->appRoot = $appRoot;
40 $this->shellProcess = $shellProcess;
41 parent::__construct();
47 protected function configure()
50 ->setName('database:dump')
51 ->setDescription($this->trans('commands.database.dump.description'))
54 InputArgument::OPTIONAL,
55 $this->trans('commands.database.dump.arguments.database'),
61 InputOption::VALUE_OPTIONAL,
62 $this->trans('commands.database.dump.options.file')
67 InputOption::VALUE_NONE,
68 $this->trans('commands.database.dump.options.gz')
70 ->setHelp($this->trans('commands.database.dump.help'))
71 ->setAliases(['dbdu']);
77 protected function execute(InputInterface $input, OutputInterface $output)
79 $database = $input->getArgument('database');
80 $file = $input->getOption('file');
81 $learning = $input->getOption('learning');
82 $gz = $input->getOption('gz');
84 $databaseConnection = $this->resolveConnection($database);
87 $date = new \DateTime();
91 $databaseConnection['database'],
92 $date->format('Y-m-d-H-i-s')
98 if ($databaseConnection['driver'] == 'mysql') {
100 'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
101 $databaseConnection['username'],
102 $databaseConnection['password'],
103 $databaseConnection['host'],
104 $databaseConnection['port'],
105 $databaseConnection['database'],
108 } elseif ($databaseConnection['driver'] == 'pgsql') {
110 'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
111 $databaseConnection['password'],
112 $databaseConnection['username'],
113 $databaseConnection['host'],
114 $databaseConnection['port'],
115 $databaseConnection['database'],
121 $this->getIo()->commentBlock($command);
124 if ($this->shellProcess->exec($command, $this->appRoot)) {
127 if (substr($file, -3) != '.gz') {
128 $resultFile = $file . ".gz";
138 if ($resultFile != $file) {
143 $this->getIo()->success(
146 $this->trans('commands.database.dump.messages.success'),