Skip to content

Commit

Permalink
Fixed restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiv Charan Panjeta committed Aug 23, 2017
1 parent 0d84b8e commit 97443cb
Show file tree
Hide file tree
Showing 8 changed files with 523 additions and 360 deletions.
4 changes: 3 additions & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Module extends \yii\base\Module {
public $fileList;
public function init() {
parent::init ();

if (\Yii::$app instanceof \yii\console\Application) {
$this->controllerNamespace = 'spanjeta\modules\backup\commands';
}
// custom initialization code goes here
}
public function getFileList() {
Expand Down
82 changes: 82 additions & 0 deletions commands/BackupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace spanjeta\modules\backup\commands;

use spanjeta\modules\backup\helpers\MysqlBackup;
use yii\console\Controller;

class BackupController extends Controller {
public $dryrun=false;
public $file=null;
public function options($actionID) {
return [
'dryrun',
'file'
];
}
public function optionAliases() {
return [
'd' => 'dryrun',
'f'=>'file'
];
}
public function log($string) {
echo $string . PHP_EOL;
}
protected function getFileList($ext = '*.sql') {
$sql = new MysqlBackup ();
$path = $sql->path;
$dataArray = array ();
$list = array ();
$list_files = glob ( $path . $ext );
if ($list_files) {
$list = array_map ( 'basename', $list_files );
sort ( $list );
}
return $list;
}
public function actionCreate() {
$sql = new MysqlBackup ();

if (! $sql->startBackup ()) {
$this->log ( __FUNCTION__ . ":Started" );
}
$tables = $sql->getTables ();
foreach ( $tables as $tableName ) {
$sql->getColumns ( $tableName );
}
foreach ( $tables as $tableName ) {
$sql->getData ( $tableName );
}

$sqlFile = $sql->endBackup ();
$this->log ( __FUNCTION__ . ":Finished : " . $sqlFile );
return $sqlFile;
}
public function actionRestore() {
$message = 'NOK';
$dryrun=$this->dryrun;
$file = $this->file;
if ($file == null) {
$files = $this->getFileList ();
if (empty ( $files )) {
$this->log ( __FUNCTION__ . " : No Files dound" );
}
$file = $files [0];
}

$this->log ( __FUNCTION__ . " : " . $file );
$sql = new MysqlBackup ();

$sqlZipFile = $file;
if (! file_exists ( $file )) {
$sqlZipFile = $sql->path . basename ( $file );
}
$sqlFile = $sql->unzip ( $sqlZipFile );
if (! $dryrun)
$message = $sql->execSqlFile ( $sqlFile );

$this->log ( __FUNCTION__ . " : " . $message );
return $message;
}
}
Loading

0 comments on commit 97443cb

Please sign in to comment.