-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backups restoring big refactoring with tests.
- Loading branch information
Showing
14 changed files
with
377 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
|
||
namespace floor12\backup\logic; | ||
|
||
|
||
use yii\base\Exception; | ||
use yii\db\Connection; | ||
|
||
class DatabaseBackupRestorer | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $backupFilePath; | ||
/** | ||
* @var Connection | ||
*/ | ||
protected $connection; | ||
/** | ||
* @var string | ||
*/ | ||
protected $sql; | ||
|
||
/** | ||
* DatabaseBackupMaker constructor. | ||
* @param string $backupFilePath | ||
* @param Connection $connection | ||
* @param string $dumperClass | ||
* @throws Exception | ||
*/ | ||
public function __construct(string $backupFilePath, Connection $connection) | ||
{ | ||
if (!file_exists($backupFilePath)) | ||
throw new Exception("Backup file don`t exist."); | ||
|
||
$this->backupFilePath = $backupFilePath; | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* @return bool | ||
* @throws \Exception | ||
*/ | ||
public function execute() | ||
{ | ||
$dbName = $this->connection->createCommand("SELECT DATABASE()")->queryScalar(); | ||
|
||
$this->connection->createCommand("DROP DATABASE `{$dbName}`")->execute(); | ||
$this->connection->createCommand("CREATE DATABASE `{$dbName}`")->execute(); | ||
$this->connection->createCommand("USE `{$dbName}`")->execute(); | ||
|
||
$lines = gzfile($this->backupFilePath); | ||
|
||
foreach ($lines as $line) { | ||
if (substr($line, 0, 2) == '--' || $line == '') | ||
continue; | ||
$this->sql .= $line; | ||
if (substr(trim($line), -1, 1) == ';') { | ||
$this->connection->createCommand($this->sql)->execute(); | ||
$this->sql = ''; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace floor12\backup\logic; | ||
|
||
use yii\base\Exception; | ||
|
||
class FolderBackupRestorer | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $backupFilePath; | ||
/** | ||
* @var string | ||
*/ | ||
protected $targetFolder; | ||
|
||
/** | ||
* DatabaseBackupMaker constructor. | ||
* @param string $backupFilePath | ||
* @param string $targetFolder | ||
* @throws Exception | ||
*/ | ||
public function __construct(string $backupFilePath, string $targetFolder) | ||
{ | ||
if (!file_exists($backupFilePath)) | ||
throw new Exception("Backup file don`t exist.."); | ||
|
||
if (!file_exists($targetFolder)) | ||
throw new Exception("Target folder don`t exist."); | ||
|
||
$this->backupFilePath = $backupFilePath; | ||
$this->targetFolder = $targetFolder; | ||
} | ||
|
||
/** | ||
* @return bool | ||
* @throws \Exception | ||
*/ | ||
public function execute() | ||
{ | ||
$command = "cd {$this->targetFolder} && unzip -o {$this->backupFilePath}"; | ||
exec($command, $r); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
|
||
namespace floor12\backup\tests; | ||
|
||
|
||
use yii\db\Connection; | ||
|
||
class ConnectionMock extends Connection | ||
{ | ||
|
||
public $databaseName; | ||
/** | ||
* @var array | ||
*/ | ||
public $sql = []; | ||
|
||
public function __construct($config = []) | ||
{ | ||
parent::__construct($config); | ||
} | ||
|
||
public function createCommand($sql = null, $params = []) | ||
{ | ||
$this->sql[] = trim($sql); | ||
return $this; | ||
} | ||
|
||
public function execute() | ||
{ | ||
return true; | ||
} | ||
|
||
public function queryScalar() | ||
{ | ||
return $this->databaseName; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Oops, something went wrong.