Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[database:dump | database:restore] Implement the symfony process and remove escapeshellcmd #4092

Merged
merged 17 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 93 additions & 83 deletions src/Command/Database/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Drupal\Console\Command\Shared\ConnectTrait;
use Drupal\Console\Core\Utils\ShellProcess;
use Drupal\Core\Database\Connection;
use Symfony\Component\Process\Process;

class DumpCommand extends Command
{
Expand Down Expand Up @@ -82,10 +83,10 @@ protected function configure()
$this->trans('commands.database.dump.options.gz')
)
->addOption(
'exclude-cache',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.dump.options.exclude.cache')
'exclude-cache',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.dump.options.exclude.cache')
)
->setHelp($this->trans('commands.database.dump.help'))
->setAliases(['dbdu']);
Expand Down Expand Up @@ -140,97 +141,106 @@ protected function execute(InputInterface $input, OutputInterface $output)

$command = null;

if ($databaseConnection['driver'] == 'mysql') {
$command = sprintf(
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);

if ($excludeCache) {
$ignoreTable = '';
foreach ($excludeTables as $table) {
$ignoreTable .= "--ignore-table=\"{$table}\" ";
}

$command = sprintf(
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" %s "%s"> "%s"',
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$ignoreTable,
$databaseConnection['database'],
$file
);

}
if ($databaseConnection['driver'] == 'mysql') {
$command = sprintf(
"mysqldump --user='%s' --password='%s' --host='%s' --port='%s' '%s' > '%s'",
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);

if ($excludeCache) {
$ignoreTable = '';
foreach ($excludeTables as $table) {
$ignoreTable .= "--ignore-table=\"{$table}\" ";
}

$command = sprintf(
"mysqldump --user='%s' --password='%s' --host='%s' --port='%s' %s '%s'> '%s'",
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$ignoreTable,
$databaseConnection['database'],
$file
);

}
} elseif ($databaseConnection['driver'] == 'pgsql') {
$command = sprintf(
'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);

if ($excludeCache) {
$ignoreTable = '';
foreach ($excludeTables as $table) {
$ignoreTable .= "-T \"{$table}\" ";
}

$command = sprintf(
'PGPASSWORD="%s" pg_dump -w -U "%s" -h "%s" -p "%s" -f "%s" %s-d "%s"',
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$file,
$ignoreTable,
$databaseConnection['database']
);
}
$command = sprintf(
"PGPASSWORD='%s' pg_dumpall -w -U '%s' -h '%s' -p '%s' -l '%s' -f '%s'",
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);

if ($excludeCache) {
$ignoreTable = '';
foreach ($excludeTables as $table) {
$ignoreTable .= "-T \"{$table}\" ";
}

$command = sprintf(
"PGPASSWORD='%s' pg_dump -w -U '%s' -h '%s' -p '%s' -f '%s' %s-d '%s'",
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$file,
$ignoreTable,
$databaseConnection['database']
);
}
}

if ($learning) {
$this->getIo()->commentBlock($command);
}

if ($this->shellProcess->exec($command, $this->appRoot)) {
$resultFile = $file;
if ($gz) {
if (substr($file, -3) != '.gz') {
$resultFile = $file . '.gz';
}
file_put_contents(
$resultFile,
gzencode(
file_get_contents(
$file
try {
$process = new Process($command);
$process->setTimeout(null);
$process->setWorkingDirectory($this->appRoot);
$process->run();

if($process->isSuccessful()) {
$resultFile = $file;
if ($gz) {
if (substr($file, -3) != '.gz') {
$resultFile = $file . '.gz';
}
file_put_contents(
$resultFile,
gzencode(
file_get_contents(
$file
)
)
);
if ($resultFile != $file) {
unlink($file);
}
}

$this->getIo()->success(
sprintf(
'%s %s',
$this->trans('commands.database.dump.messages.success'),
$resultFile
)
);
if ($resultFile != $file) {
unlink($file);
}
}

$this->getIo()->success(
sprintf(
'%s %s',
$this->trans('commands.database.dump.messages.success'),
$resultFile
)
);
return 0;
} catch (\Exception $e) {
return 1;
}

return 0;
}
}
15 changes: 7 additions & 8 deletions src/Command/Database/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ConnectTrait;

Expand Down Expand Up @@ -94,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($databaseConnection['driver'] == 'mysql') {
// Drop database first.
$commands[] = sprintf(
'mysql --user=%s --password=%s --host=%s --port=%s -e"DROP DATABASE IF EXISTS %s"',
"mysql --user='%s' --password='%s' --host='%s' --port='%s' -e'DROP DATABASE IF EXISTS %s'",
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
Expand All @@ -104,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Recreate database.
$commands[] = sprintf(
'mysql --user=%s --password=%s --host=%s --port=%s -e"CREATE DATABASE %s"',
"mysql --user='%s' --password='%s' --host='%s' --port='%s' -e'CREATE DATABASE %s'",
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
Expand All @@ -114,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Import dump.
$commands[] = sprintf(
$catCommand . 'mysql --user=%s --password=%s --host=%s --port=%s %s',
$catCommand . "mysql --user='%s' --password='%s' --host='%s' --port='%s' %s",
$file,
$databaseConnection['username'],
$databaseConnection['password'],
Expand All @@ -124,7 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
} elseif ($databaseConnection['driver'] == 'pgsql') {
$commands[] = sprintf(
'PGPASSWORD="%s" ' . $catCommand . 'psql -w -U %s -h %s -p %s -d %s',
"PGPASSWORD='%s' " . $catCommand . "psql -w -U '%s' -h '%s' -p '%s' -d '%s'",
$file,
$databaseConnection['password'],
$databaseConnection['username'],
Expand All @@ -139,11 +139,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->getIo()->commentBlock($command);
}

$processBuilder = new ProcessBuilder(['-v']);
$process = $processBuilder->getProcess();
$process = new Process($command);
$process->setTimeout(null);
$process->setWorkingDirectory($this->appRoot);
$process->setTty($input->isInteractive());
$process->setCommandLine($command);
$process->run();

if (!$process->isSuccessful()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Shared/ConnectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function escapeConnection($databaseConnection) {
];

foreach ($settings as $setting) {
$databaseConnection[$setting] = escapeshellcmd($databaseConnection[$setting]);
$databaseConnection[$setting] = $databaseConnection[$setting];
}

return $databaseConnection;
Expand Down