|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Breadlesscode\Backups\Step; |
| 4 | + |
| 5 | +use Neos\Flow\Annotations as Flow; |
| 6 | + |
| 7 | +class MysqlDatabaseExportStep extends StepAbstract |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Folder name in backup file |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $name = 'MySqlDatabaseExport'; |
| 15 | + |
| 16 | + /** |
| 17 | + * @Flow\InjectConfiguration(path="persistence.backendOptions", package="Neos.Flow") |
| 18 | + * @var array |
| 19 | + */ |
| 20 | + protected $dbConfig; |
| 21 | + |
| 22 | + public function backup(): bool |
| 23 | + { |
| 24 | + $mysqlDumpPath = $this->config['mysqlDumpBinPath'] ?? 'mysqldump'; |
| 25 | + $mysqlDumpOptions = $this->config['mysqlDumpOptions'] ?? []; |
| 26 | + |
| 27 | + $command = vsprintf('%s -h %s -u %s -p%s %s %s 2>/dev/null 1> %s', [ |
| 28 | + $mysqlDumpPath, |
| 29 | + escapeshellarg($this->dbConfig['host']), |
| 30 | + escapeshellarg($this->dbConfig['user']), |
| 31 | + escapeshellarg($this->dbConfig['password']), |
| 32 | + escapeshellarg($this->dbConfig['dbname']), |
| 33 | + implode(' ', $mysqlDumpOptions), |
| 34 | + $this->getFilenameForBackup() |
| 35 | + ]); |
| 36 | + |
| 37 | + exec($command); |
| 38 | + |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + public function restore(): bool |
| 43 | + { |
| 44 | + $mysqlPath = $this->config['mysqlBinPath'] ?? 'mysql'; |
| 45 | + |
| 46 | + $command = vsprintf('%s -h %s -u %s -p%s %s 2>/dev/null < %s', [ |
| 47 | + $mysqlPath, |
| 48 | + escapeshellarg($this->dbConfig['host']), |
| 49 | + escapeshellarg($this->dbConfig['user']), |
| 50 | + escapeshellarg($this->dbConfig['password']), |
| 51 | + escapeshellarg($this->dbConfig['dbname']), |
| 52 | + $this->getFilenameForBackup() |
| 53 | + ]); |
| 54 | + |
| 55 | + exec($command); |
| 56 | + |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + public function getRestoreWarning(): ?string |
| 61 | + { |
| 62 | + return 'The database "' . $this->dbConfig['dbname'] . '" will be overwritten:' . PHP_EOL; |
| 63 | + } |
| 64 | + |
| 65 | + protected function getFilenameForBackup(): string |
| 66 | + { |
| 67 | + $backupPath = $this->getBackupStepPath(); |
| 68 | + return $backupPath . '/' . $this->dbConfig['dbname'] . '.sql'; |
| 69 | + } |
| 70 | +} |
0 commit comments