@@ -35,11 +35,13 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public function __construct(string $backupFilePath, string $targetFolder, $io = IOPriority::IDLE) |
| 37 | 37 | { |
| 38 | - if (file_exists($backupFilePath)) |
|
| 39 | - throw new Exception("Backup file exists."); |
|
| 38 | + if (file_exists($backupFilePath)) { |
|
| 39 | + throw new Exception("Backup file exists."); |
|
| 40 | + } |
|
| 40 | 41 | |
| 41 | - if (!file_exists($targetFolder)) |
|
| 42 | - throw new Exception("Target folder not exists."); |
|
| 42 | + if (!file_exists($targetFolder)) { |
|
| 43 | + throw new Exception("Target folder not exists."); |
|
| 44 | + } |
|
| 43 | 45 | |
| 44 | 46 | $this->module = Yii::$app->getModule('backup'); |
| 45 | 47 | $this->backupFilePath = $backupFilePath; |
@@ -58,11 +60,13 @@ discard block |
||
| 58 | 60 | $zipPath = $this->module->binaries['zip']; |
| 59 | 61 | $command = "cd {$this->targetFolder} && {$ionicePath} -c{$this->io} {$zipPath} -r -0 {$this->backupFilePath} * > /dev/null"; |
| 60 | 62 | exec($command); |
| 61 | - if (!file_exists($this->backupFilePath)) |
|
| 62 | - throw new FolderDumpException(); |
|
| 63 | + if (!file_exists($this->backupFilePath)) { |
|
| 64 | + throw new FolderDumpException(); |
|
| 65 | + } |
|
| 63 | 66 | |
| 64 | - if ($this->module->chmod) |
|
| 65 | - chmod($this->backupFilePath, $this->module->chmod); |
|
| 67 | + if ($this->module->chmod) { |
|
| 68 | + chmod($this->backupFilePath, $this->module->chmod); |
|
| 69 | + } |
|
| 66 | 70 | |
| 67 | 71 | return true; |
| 68 | 72 | } |
@@ -34,14 +34,17 @@ |
||
| 34 | 34 | { |
| 35 | 35 | $this->module = Yii::$app->getModule('backup'); |
| 36 | 36 | |
| 37 | - if (!file_exists($absoluteFilePath)) |
|
| 38 | - throw new FileNotFoundException(); |
|
| 37 | + if (!file_exists($absoluteFilePath)) { |
|
| 38 | + throw new FileNotFoundException(); |
|
| 39 | + } |
|
| 39 | 40 | |
| 40 | - if (!is_array($this->module->configs) || empty($this->module->configs)) |
|
| 41 | - throw new ModuleNotConfiguredException(); |
|
| 41 | + if (!is_array($this->module->configs) || empty($this->module->configs)) { |
|
| 42 | + throw new ModuleNotConfiguredException(); |
|
| 43 | + } |
|
| 42 | 44 | |
| 43 | - if (!is_array($this->module->configs[$config_id])) |
|
| 44 | - throw new ConfigurationNotFoundException(); |
|
| 45 | + if (!is_array($this->module->configs[$config_id])) { |
|
| 46 | + throw new ConfigurationNotFoundException(); |
|
| 47 | + } |
|
| 45 | 48 | |
| 46 | 49 | $this->currentConfig = $this->module->configs[$config_id]; |
| 47 | 50 | $this->model = new Backup(); |
@@ -52,8 +52,9 @@ discard block |
||
| 52 | 52 | public function actionRestore(string $backup_id) |
| 53 | 53 | { |
| 54 | 54 | $model = Backup::findOne((int)$backup_id); |
| 55 | - if (!$model) |
|
| 56 | - throw new ErrorException('Backup not found.'); |
|
| 55 | + if (!$model) { |
|
| 56 | + throw new ErrorException('Backup not found.'); |
|
| 57 | + } |
|
| 57 | 58 | |
| 58 | 59 | Yii::createObject(BackupRestore::class, [$model])->run(); |
| 59 | 60 | $this->stdout('Backup restored.' . PHP_EOL, Console::FG_GREEN); |
@@ -69,14 +70,16 @@ discard block |
||
| 69 | 70 | { |
| 70 | 71 | $models = Backup::find()->orderBy('id DESC')->all(); |
| 71 | 72 | |
| 72 | - if (empty($models)) |
|
| 73 | - return $this->stderr('Backups not found.' . PHP_EOL, Console::FG_YELLOW); |
|
| 73 | + if (empty($models)) { |
|
| 74 | + return $this->stderr('Backups not found.' . PHP_EOL, Console::FG_YELLOW); |
|
| 75 | + } |
|
| 74 | 76 | |
| 75 | - foreach ($models as $model) |
|
| 76 | - $this->stdout("{$model->id}: " . Yii::$app->formatter->asDatetime($model->date) . "\t\t{$model->config_id}\t\t" . |
|
| 77 | + foreach ($models as $model) { |
|
| 78 | + $this->stdout("{$model->id}: " . Yii::$app->formatter->asDatetime($model->date) . "\t\t{$model->config_id}\t\t" . |
|
| 77 | 79 | BackupType::$list[$model->type] . |
| 78 | 80 | PHP_EOL, |
| 79 | 81 | $model->status ? Console::FG_GREEN : Console::FG_RED); |
| 82 | + } |
|
| 80 | 83 | } |
| 81 | 84 | |
| 82 | 85 | /** |
@@ -89,9 +92,10 @@ discard block |
||
| 89 | 92 | public function actionImport(string $config_id, string $absoluteFilePath) |
| 90 | 93 | { |
| 91 | 94 | $importer = new BackupImporter($config_id, $absoluteFilePath); |
| 92 | - if ($importer->import()) |
|
| 93 | - $this->stdout("Backup imported: {$absoluteFilePath}\n", Console::FG_GREEN); |
|
| 94 | - else |
|
| 95 | - $this->stdout("Something went wrong.\n", Console::FG_RED); |
|
| 95 | + if ($importer->import()) { |
|
| 96 | + $this->stdout("Backup imported: {$absoluteFilePath}\n", Console::FG_GREEN); |
|
| 97 | + } else { |
|
| 98 | + $this->stdout("Something went wrong.\n", Console::FG_RED); |
|
| 99 | + } |
|
| 96 | 100 | } |
| 97 | 101 | } |
@@ -148,8 +148,9 @@ |
||
| 148 | 148 | protected function getBackup(int $id) |
| 149 | 149 | { |
| 150 | 150 | $this->model = Backup::findOne($id); |
| 151 | - if (!$this->model) |
|
| 152 | - throw new NotFoundHttpException(Yii::t('app.f12.backup', 'Backup is not found.')); |
|
| 151 | + if (!$this->model) { |
|
| 152 | + throw new NotFoundHttpException(Yii::t('app.f12.backup', 'Backup is not found.')); |
|
| 153 | + } |
|
| 153 | 154 | } |
| 154 | 155 | |
| 155 | 156 | } |
@@ -19,8 +19,9 @@ discard block |
||
| 19 | 19 | $ionicePath = $this->module->binaries['ionice']; |
| 20 | 20 | $command = "PGPASSWORD='{$this->password}' {$ionicePath} -c{$this->io} {$binaryPath} -h {$this->host} -p {$this->port} -U {$this->username} -Fc -Z1 {$this->database} -f {$this->backupFilePath}"; |
| 21 | 21 | exec($command, $return); |
| 22 | - if (!empty($return)) |
|
| 23 | - throw new PostgresDumpException(); |
|
| 22 | + if (!empty($return)) { |
|
| 23 | + throw new PostgresDumpException(); |
|
| 24 | + } |
|
| 24 | 25 | } |
| 25 | 26 | |
| 26 | 27 | public function restore(array $tableNames = []): void |
@@ -28,8 +29,9 @@ discard block |
||
| 28 | 29 | $binaryPath = $this->module->binaries['pg_restore']; |
| 29 | 30 | $command = "PGPASSWORD='{$this->password}' {$binaryPath} -c -Fc -j 4 -h {$this->host} -p {$this->port} -U {$this->username} -d {$this->database} {$this->backupFilePath}"; |
| 30 | 31 | exec($command, $return); |
| 31 | - if (!empty($return)) |
|
| 32 | - throw new PostgresDumpException(); |
|
| 32 | + if (!empty($return)) { |
|
| 33 | + throw new PostgresDumpException(); |
|
| 34 | + } |
|
| 33 | 35 | } |
| 34 | 36 | |
| 35 | 37 | public function init() |
@@ -56,13 +56,15 @@ |
||
| 56 | 56 | $dsn = substr($dsn, strpos($dsn, ':') + 1); |
| 57 | 57 | $dsnParts = explode(';', $dsn); |
| 58 | 58 | |
| 59 | - if (empty($dsnParts)) |
|
| 60 | - throw new DsnParseException(); |
|
| 59 | + if (empty($dsnParts)) { |
|
| 60 | + throw new DsnParseException(); |
|
| 61 | + } |
|
| 61 | 62 | |
| 62 | 63 | foreach ($dsnParts as $part) { |
| 63 | 64 | $row = explode('=', $part); |
| 64 | - if (isset($row[0]) && isset($row[1])) |
|
| 65 | - $this->parsedDsn[$row[0]] = $row[1]; |
|
| 65 | + if (isset($row[0]) && isset($row[1])) { |
|
| 66 | + $this->parsedDsn[$row[0]] = $row[1]; |
|
| 67 | + } |
|
| 66 | 68 | } |
| 67 | 69 | |
| 68 | 70 | $this->host = $this->parsedDsn['host'] ?: null; |
@@ -22,8 +22,9 @@ discard block |
||
| 22 | 22 | $this->checkBinary($ionicePath); |
| 23 | 23 | $command = "{$ionicePath} -c{$this->io} {$mysqldumpPath} -h {$this->host} -P {$this->port} -u {$this->username} -p{$this->password} {$this->database} | {$gzipPath} -9 -c > {$this->backupFilePath}"; |
| 24 | 24 | (exec($command)); |
| 25 | - if (!file_exists($this->backupFilePath) || filesize($this->backupFilePath) < 100) |
|
| 26 | - throw new MysqlDumpException(); |
|
| 25 | + if (!file_exists($this->backupFilePath) || filesize($this->backupFilePath) < 100) { |
|
| 26 | + throw new MysqlDumpException(); |
|
| 27 | + } |
|
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | public function restore(array $tableNames = []): void |
@@ -34,7 +35,8 @@ discard block |
||
| 34 | 35 | $this->checkBinary($mysqlPath); |
| 35 | 36 | $command = "{$zcatPath} {$this->backupFilePath} | {$mysqlPath} -h {$this->host} -P {$this->port} -u {$this->username} -p{$this->password} {$this->database}"; |
| 36 | 37 | exec($command, $return); |
| 37 | - if (!empty($return)) |
|
| 38 | - throw new PostgresDumpException(); |
|
| 38 | + if (!empty($return)) { |
|
| 39 | + throw new PostgresDumpException(); |
|
| 40 | + } |
|
| 39 | 41 | } |
| 40 | 42 | } |