|
15 | 15 | use Drupal\Console\Command\Shared\ConnectTrait;
|
16 | 16 | use Drupal\Console\Core\Utils\ShellProcess;
|
17 | 17 | use Drupal\Core\Database\Connection;
|
| 18 | +use Symfony\Component\Process\Process; |
18 | 19 |
|
19 | 20 | class DumpCommand extends Command
|
20 | 21 | {
|
@@ -82,10 +83,10 @@ protected function configure()
|
82 | 83 | $this->trans('commands.database.dump.options.gz')
|
83 | 84 | )
|
84 | 85 | ->addOption(
|
85 |
| - 'exclude-cache', |
86 |
| - null, |
87 |
| - InputOption::VALUE_NONE, |
88 |
| - $this->trans('commands.database.dump.options.exclude.cache') |
| 86 | + 'exclude-cache', |
| 87 | + null, |
| 88 | + InputOption::VALUE_NONE, |
| 89 | + $this->trans('commands.database.dump.options.exclude.cache') |
89 | 90 | )
|
90 | 91 | ->setHelp($this->trans('commands.database.dump.help'))
|
91 | 92 | ->setAliases(['dbdu']);
|
@@ -140,97 +141,106 @@ protected function execute(InputInterface $input, OutputInterface $output)
|
140 | 141 |
|
141 | 142 | $command = null;
|
142 | 143 |
|
143 |
| - if ($databaseConnection['driver'] == 'mysql') { |
144 |
| - $command = sprintf( |
145 |
| - 'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"', |
146 |
| - $databaseConnection['username'], |
147 |
| - $databaseConnection['password'], |
148 |
| - $databaseConnection['host'], |
149 |
| - $databaseConnection['port'], |
150 |
| - $databaseConnection['database'], |
151 |
| - $file |
152 |
| - ); |
153 |
| - |
154 |
| - if ($excludeCache) { |
155 |
| - $ignoreTable = ''; |
156 |
| - foreach ($excludeTables as $table) { |
157 |
| - $ignoreTable .= "--ignore-table=\"{$table}\" "; |
158 |
| - } |
159 |
| - |
160 |
| - $command = sprintf( |
161 |
| - 'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" %s "%s"> "%s"', |
162 |
| - $databaseConnection['username'], |
163 |
| - $databaseConnection['password'], |
164 |
| - $databaseConnection['host'], |
165 |
| - $databaseConnection['port'], |
166 |
| - $ignoreTable, |
167 |
| - $databaseConnection['database'], |
168 |
| - $file |
169 |
| - ); |
170 |
| - |
171 |
| - } |
| 144 | + if ($databaseConnection['driver'] == 'mysql') { |
| 145 | + $command = sprintf( |
| 146 | + "mysqldump --user='%s' --password='%s' --host='%s' --port='%s' '%s' > '%s'", |
| 147 | + $databaseConnection['username'], |
| 148 | + $databaseConnection['password'], |
| 149 | + $databaseConnection['host'], |
| 150 | + $databaseConnection['port'], |
| 151 | + $databaseConnection['database'], |
| 152 | + $file |
| 153 | + ); |
| 154 | + |
| 155 | + if ($excludeCache) { |
| 156 | + $ignoreTable = ''; |
| 157 | + foreach ($excludeTables as $table) { |
| 158 | + $ignoreTable .= "--ignore-table=\"{$table}\" "; |
| 159 | + } |
| 160 | + |
| 161 | + $command = sprintf( |
| 162 | + "mysqldump --user='%s' --password='%s' --host='%s' --port='%s' %s '%s'> '%s'", |
| 163 | + $databaseConnection['username'], |
| 164 | + $databaseConnection['password'], |
| 165 | + $databaseConnection['host'], |
| 166 | + $databaseConnection['port'], |
| 167 | + $ignoreTable, |
| 168 | + $databaseConnection['database'], |
| 169 | + $file |
| 170 | + ); |
| 171 | + |
| 172 | + } |
172 | 173 | } elseif ($databaseConnection['driver'] == 'pgsql') {
|
173 |
| - $command = sprintf( |
174 |
| - 'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"', |
175 |
| - $databaseConnection['password'], |
176 |
| - $databaseConnection['username'], |
177 |
| - $databaseConnection['host'], |
178 |
| - $databaseConnection['port'], |
179 |
| - $databaseConnection['database'], |
180 |
| - $file |
181 |
| - ); |
182 |
| - |
183 |
| - if ($excludeCache) { |
184 |
| - $ignoreTable = ''; |
185 |
| - foreach ($excludeTables as $table) { |
186 |
| - $ignoreTable .= "-T \"{$table}\" "; |
187 |
| - } |
188 |
| - |
189 |
| - $command = sprintf( |
190 |
| - 'PGPASSWORD="%s" pg_dump -w -U "%s" -h "%s" -p "%s" -f "%s" %s-d "%s"', |
191 |
| - $databaseConnection['password'], |
192 |
| - $databaseConnection['username'], |
193 |
| - $databaseConnection['host'], |
194 |
| - $databaseConnection['port'], |
195 |
| - $file, |
196 |
| - $ignoreTable, |
197 |
| - $databaseConnection['database'] |
198 |
| - ); |
199 |
| - } |
| 174 | + $command = sprintf( |
| 175 | + "PGPASSWORD='%s' pg_dumpall -w -U '%s' -h '%s' -p '%s' -l '%s' -f '%s'", |
| 176 | + $databaseConnection['password'], |
| 177 | + $databaseConnection['username'], |
| 178 | + $databaseConnection['host'], |
| 179 | + $databaseConnection['port'], |
| 180 | + $databaseConnection['database'], |
| 181 | + $file |
| 182 | + ); |
| 183 | + |
| 184 | + if ($excludeCache) { |
| 185 | + $ignoreTable = ''; |
| 186 | + foreach ($excludeTables as $table) { |
| 187 | + $ignoreTable .= "-T \"{$table}\" "; |
| 188 | + } |
| 189 | + |
| 190 | + $command = sprintf( |
| 191 | + "PGPASSWORD='%s' pg_dump -w -U '%s' -h '%s' -p '%s' -f '%s' %s-d '%s'", |
| 192 | + $databaseConnection['password'], |
| 193 | + $databaseConnection['username'], |
| 194 | + $databaseConnection['host'], |
| 195 | + $databaseConnection['port'], |
| 196 | + $file, |
| 197 | + $ignoreTable, |
| 198 | + $databaseConnection['database'] |
| 199 | + ); |
| 200 | + } |
200 | 201 | }
|
201 | 202 |
|
202 | 203 | if ($learning) {
|
203 | 204 | $this->getIo()->commentBlock($command);
|
204 | 205 | }
|
205 | 206 |
|
206 |
| - if ($this->shellProcess->exec($command, $this->appRoot)) { |
207 |
| - $resultFile = $file; |
208 |
| - if ($gz) { |
209 |
| - if (substr($file, -3) != '.gz') { |
210 |
| - $resultFile = $file . '.gz'; |
211 |
| - } |
212 |
| - file_put_contents( |
213 |
| - $resultFile, |
214 |
| - gzencode( |
215 |
| - file_get_contents( |
216 |
| - $file |
| 207 | + try { |
| 208 | + $process = new Process($command); |
| 209 | + $process->setTimeout(null); |
| 210 | + $process->setWorkingDirectory($this->appRoot); |
| 211 | + $process->run(); |
| 212 | + |
| 213 | + if($process->isSuccessful()) { |
| 214 | + $resultFile = $file; |
| 215 | + if ($gz) { |
| 216 | + if (substr($file, -3) != '.gz') { |
| 217 | + $resultFile = $file . '.gz'; |
| 218 | + } |
| 219 | + file_put_contents( |
| 220 | + $resultFile, |
| 221 | + gzencode( |
| 222 | + file_get_contents( |
| 223 | + $file |
| 224 | + ) |
217 | 225 | )
|
| 226 | + ); |
| 227 | + if ($resultFile != $file) { |
| 228 | + unlink($file); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + $this->getIo()->success( |
| 233 | + sprintf( |
| 234 | + '%s %s', |
| 235 | + $this->trans('commands.database.dump.messages.success'), |
| 236 | + $resultFile |
218 | 237 | )
|
219 | 238 | );
|
220 |
| - if ($resultFile != $file) { |
221 |
| - unlink($file); |
222 |
| - } |
223 | 239 | }
|
224 | 240 |
|
225 |
| - $this->getIo()->success( |
226 |
| - sprintf( |
227 |
| - '%s %s', |
228 |
| - $this->trans('commands.database.dump.messages.success'), |
229 |
| - $resultFile |
230 |
| - ) |
231 |
| - ); |
| 241 | + return 0; |
| 242 | + } catch (\Exception $e) { |
| 243 | + return 1; |
232 | 244 | }
|
233 |
| - |
234 |
| - return 0; |
235 | 245 | }
|
236 | 246 | }
|
0 commit comments