protected function execute(InputInterface $input, OutputInterface $output)
{
$this->validateInput($input);
$project = $this->getSelectedProject();
$environment = $this->getSelectedEnvironment();
$appName = $this->selectApp($input);
$sshUrl = $environment->getSshUrl($appName);
$timestamp = $input->getOption('timestamp') ? str_replace('+', '', date('Ymd-His-O')) : null;
if (!$input->getOption('stdout')) {
if ($input->getOption('file')) {
$dumpFile = rtrim($input->getOption('file'), '/');
/** @var \Platformsh\Cli\Helper\FilesystemHelper $fsHelper */
$fsHelper = $this->getHelper('fs');
// Insert the timestamp into the filename.
if ($timestamp) {
$basename = basename($dumpFile);
$prefix = substr($dumpFile, 0, -strlen($basename));
if ($dotPos = strrpos($basename, '.')) {
$basename = substr($basename, 0, $dotPos) . '--' . $timestamp . substr($basename, $dotPos);
} else {
$basename .= '--' . $timestamp;
}
$dumpFile = $prefix . $basename;
}
// Make the filename absolute.
$dumpFile = $fsHelper->makePathAbsolute($dumpFile);
// Ensure the filename is not a directory.
if (is_dir($dumpFile)) {
$dumpFile .= '/' . $this->getDefaultDumpFilename($project, $environment, $appName, $timestamp);
}
} else {
$projectRoot = $this->getProjectRoot();
$directory = $projectRoot ?: getcwd();
$dumpFile = $directory . '/' . $this->getDefaultDumpFilename($project, $environment, $appName, $timestamp);
}
}
if (isset($dumpFile)) {
if (file_exists($dumpFile)) {
/** @var \Platformsh\Cli\Helper\QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');
if (!$questionHelper->confirm("File exists: <comment>{$dumpFile}</comment>. Overwrite?", false)) {
return 1;
}
}
$this->stdErr->writeln("Creating SQL dump file: <info>{$dumpFile}</info>");
}
$util = new RelationshipsUtil($this->stdErr);
$database = $util->chooseDatabase($sshUrl, $input);
if (empty($database)) {
return 1;
}
switch ($database['scheme']) {
case 'pgsql':
$dumpCommand = "pg_dump --clean" . " postgresql://{$database['username']}:{$database['password']}@{$database['host']}/{$database['path']}";
break;
default:
$dumpCommand = "mysqldump --no-autocommit --single-transaction" . " --opt -Q {$database['path']}" . " --host={$database['host']} --port={$database['port']}" . " --user={$database['username']} --password={$database['password']}";
break;
}
set_time_limit(0);
$command = 'ssh -C ' . escapeshellarg($sshUrl) . ' ' . escapeshellarg($dumpCommand);
if (isset($dumpFile)) {
$command .= ' > ' . escapeshellarg($dumpFile);
}
return $this->getHelper('shell')->executeSimple($command);
}