Skip to content

Commit

Permalink
添加:数据库备份
Browse files Browse the repository at this point in the history
  • Loading branch information
dxkite committed May 6, 2017
1 parent b2f9bb7 commit 603a0f9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 52 deletions.
6 changes: 4 additions & 2 deletions docs/release.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

v1.2.0
----------
- 添加
- 网页控制面板
- 废弃
- 控制台控制命令
- 修复
- 删除文件夹
- 数据库备份


v1.2.1-dev 1.2.1 预览版
--------------------------
预览点:添加路由网页管理控制
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<h3>当前备份点 {{ date('Y-m-d H:i:s',$:time) }}</h3>
<div class="table-responsive">
<div class="backup-bar">
<button type="button" class="btn btn-primary" data-op-url="@u('datebase_progress',['option'=>'backup'])">备份选中</button>
<button type="button" class="btn btn-info" data-op-url="@u('datebase_progress',['option'=>'recovery'])">恢复选中</button>
<button type="button" class="btn btn-danger" data-op-url="@u('datebase_progress',['option'=>'delete'])">删除选中</button>
<button type="button" class="btn btn-primary" data-op-url="@u('datebase_progress',['option'=>'backup','name'=>$:current_name])">备份选中</button>
<button type="button" class="btn btn-info" data-op-url="@u('datebase_progress',['option'=>'recovery','name'=>$:current_name])">恢复选中</button>
<button type="button" class="btn btn-danger" data-op-url="@u('datebase_progress',['option'=>'delete','name'=>$:current_name])">删除选中</button>
<button type="button" class="btn btn-success" data-url="@u('datebase_progress',['option'=>'backup','all'=>'yes'])">新建备份</button>
</div>
<form>
Expand All @@ -36,6 +36,7 @@ <h3>当前备份点 {{ date('Y-m-d H:i:s',$:time) }}</h3>
<th>#</th>
<th>选项</th>
<th>模块</th>
<th>大小</th>
<th>操作</th>
</tr>
</thead>
Expand All @@ -45,10 +46,11 @@ <h3>当前备份点 {{ date('Y-m-d H:i:s',$:time) }}</h3>
<td>{{ $id }}</td>
<td> <input type="checkbox" name="select[{{$name}}]"> </td>
<td>{{ $name }}</td>
<td>{{ $:current_size[$id] }} B</td>
<td>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'backup','module'=>$name,'backup'=>$:current_name])">备份</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'recovery','module'=>$name,'backup'=>$:current_name])">恢复</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'delete','module'=>$name,'backup'=>$:current_name])">删除</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'backup','module'=>$name,'name'=>$:current_name])">备份</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'recovery','module'=>$name,'name'=>$:current_name])">恢复</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'delete','module'=>$name,'name'=>$:current_name])">删除</a>
</td>
</tr>
@endforeach
Expand All @@ -72,8 +74,8 @@ <h4>历史备份</h4>
<td>{{ date('Y-m-d H:i:s',$item['time']) }}</td>
<td>
<a href="@u('suda:admin_database',['current'=>$name])">操作</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'recovery','name'=>$name])">恢复</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'delete','name'=>$name])">删除</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'recovery','name'=>$name,'all'=>'yes'])">恢复</a>
<a href="#" data-url="@u('suda:datebase_progress',['option'=>'delete','name'=>$name,'all'=>'yes'])">删除</a>
</td>
</tr>
@endforeach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ public static function getInstance()
* @param string $name 文档名
* @return $this
*/
public function archive(string $name)
public function archive(string $name=null)
{
if (is_null($name)) {
$name=$this->name??time();
}
$this->name=$name;
$this->dirname=self::$root.'/'.$name;
return $this;
Expand All @@ -89,27 +92,38 @@ public function parseDTOs(array $modules=null)
{
$modules= $modules ?? Application::getModules();
foreach ($modules as $module) {
self::log('parse module dtos:'.$module);
self::log('parse module dtos > '.$module);
self::parseModuleDTOs($module);
}
return $this;
}

public function createTables(array $modules=null)
{
$modules= $modules ?? Application::getModules();
foreach ($modules as $module) {
self::log('create module tables:'.$module);
self::log('create module tables > '.$module);
self::createTable($module);
}
return $this;
}


public static function read(string $backup)
{
$config_path=self::$root.'/'.$backup.'/config.php';
_D()->trace($config_path);
if (Storage::exist($config_path)) {
return include $config_path;
$config=include $config_path;
foreach ($config['module'] as $index=>$name) {
$module_dir=Application::getModuleDir($name);
$datafile=self::$root.'/'.$backup.'/data/'.$module_dir.'.php';
if (file_exists($datafile)) {
$config['module_size'][$index]=filesize($datafile);
} else {
$config['module_size'][$index]=0;
}
}
return $config;
}
return false;
}
Expand All @@ -122,7 +136,12 @@ public static function readList()
foreach ($readDirs as $dir) {
$config_path=self::$root.'/'.$dir.'/config.php';
if (Storage::exist($config_path)) {
$config[$dir]= include $config_path;
$conf=include $config_path;
if (isset($conf['module'])) {
$config[$dir]= $conf;
} else {
Storage::rmdirs(self::$root.'/'.$dir);
}
}
}
return $config;
Expand All @@ -132,26 +151,28 @@ public function backupTables(array $modules=null)
{
$modules= $modules ?? Application::getModules();
// 新建备份
self::archive(time());
self::archive();
$config_path=$this->dirname .'/config.php';
foreach ($modules as $module) {
self::parseDTOs($modules);
$config['time']=time();
$config['time']=intval($this->name);
$config['module']=$modules;
Storage::path($this->dirname);
ArrayHelper::export($config_path, '_config', $config);
self::log('backup module:'.$module);
self::log('backup module > '.$module);
self::parseModuleDTOs($module);
self::backupModule($module);
}
return $this;
}

public function deleteTables(array $modules=null)
{
if (is_null($modules) && $this->dirname) {
self::log('delete > '.$this->dirname);
Storage::rmdirs($this->dirname);
return true;
return $this;
}

$config_path=$this->dirname.'/config.php';
if (Storage::exist($config_path)) {
$config= include $config_path;
Expand All @@ -171,7 +192,7 @@ public function deleteTables(array $modules=null)
}
ArrayHelper::export($this->dirname.'/config.php', '_config', $config);
}
return false;
return $this;
}

public function importTables(array $modules=null)
Expand All @@ -181,12 +202,13 @@ public function importTables(array $modules=null)
$module_dir=Application::getModuleDir($module);
$datafile=$this->dirname.'/data/'.$module_dir.'.php';
if (Storage::exist($datafile)) {
self::log('import module:'.$module);
self::log('import module > '.$module);
self::execFile($datafile);
} else {
self::log("file no found :${datafile}");
}
}
return $this;
}


Expand All @@ -200,6 +222,7 @@ public function backupModule(string $module)
$tables=include $tablefile;
file_put_contents($datafile, '<?php /* create:'.date('Y-m-d H:i:s')."*/\r\n".self::$dtohead."\r\n".str_repeat('#', 64)."\r\n");
foreach ($tables as $table) {
self::log('backup '.$module.' table > '.$table);
$data=self::createDataString($table);
file_put_contents($datafile, '/* table '.$table.'*/'.$data."\r\n", FILE_APPEND);
}
Expand All @@ -222,6 +245,7 @@ public function createTable(string $module)
$module_dir=Application::getModuleDir($module);
$create=$this->dirname.'/create/'.$module_dir.'.php';
if (Storage::exist($create)) {
self::log('create module tables > '.$create);
self::execFile($create);
} else {
self::log("file no found :${create}");
Expand All @@ -242,6 +266,7 @@ public function parseModuleDTOs(string $module)
$tables=Storage::readDirFiles($dto_path, true, '/\.dto$/', true);
file_put_contents($create, '<?php /* create:'.date('Y-m-d H:i:s')."*/\r\n".self::$dtohead."\r\n".str_repeat('#', 64)."\r\n");
foreach ($tables as $table) {
self::log('parse dto > '.$dto_path.'/'.$table);
$name=pathinfo($table, PATHINFO_FILENAME);
$namespace=preg_replace('/\\\\\//', '\\', dirname($table));
$table_name=self::tablename($namespace, $name);
Expand All @@ -260,7 +285,8 @@ public function parseModuleDTOs(string $module)
Storage::path(dirname($tablefile));
ArrayHelper::export($tablefile, '_tables', $table_names);
file_put_contents($create, str_repeat('#', 64)."\r\n".self::$dtoend, FILE_APPEND);
self::log('output file: '.$create);
self::log('output file > '.$create);
self::log('output tablefile > '.$tablefile);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
<?php
namespace dxkite\suda\response;

use suda\core\{Session,Cookie,Request,Query};
use suda\core\Session;
use suda\core\Cookie;
use suda\core\Request;
use suda\core\Query;
use dxkite\suda\DBManager;

/**
* visit url /system/database as all method to run this class.
* you call use u('admin_database',Array) to create path.
* @template: default:admin_db.tpl.html
* @name: admin_database
* @url: /system/database
* @param:
* @param:
*/
class AdminDb extends \suda\core\Response
{
public function onRequest(Request $request)
{
$backupname= $request->get()->current ?? DBManager::LASTER;
$backupname= $request->get()->current ?? DBManager::selectLaster();
$page=$this->page('suda:admin_db')
->set('title',_T('数据管理'))
->set('header_select','system_admin');
->set('title', _T('数据管理'))
->set('header_select', 'system_admin');


$list=DBManager::readList();
$read=DBManager::read($backupname);

$page->set('backup_list',$list);
$page->set('time',$read['time']);
$page->set('current_name',$backupname);
$page->set('current',$read['module'] ?? [] );

if ($read) {
$page->set('backup_list', $list);
$page->set('time', $read['time']??0);
$page->set('current_name', $backupname);
$page->set('current', $read['module'] ?? []);
$page->set('current_size', $read['module_size'] ?? []);
} else {
$this->setHeader('Location:'.u('admin_database'));
}

return $page->render();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ class DbProgress extends \suda\core\Response
public function onRequest(Request $request)
{
$this->type('html');

//var_dump(DBManager::getInstance()->archive(DBManager::selectLaster() ?? time())->parseDTOs());


// 操作
$option=strtolower($request->get()->option);
// 操作全部
$all=strtolower($request->get()->all('no'));
if ($name=$request->get()->name) {
DBManager::getInstance()->archive($name);
}
// 操作全部
if ($all==='yes' || $request->get()->name) {
if ($name=$request->get()->name){
DBManager::archive($name);
}
if ($all==='yes') {
if ($option==='recovery') {
DBManager::getInstance()->importTables();
DBManager::getInstance()->createTables()->importTables();
} elseif ($option==='delete') {
DBManager::getInstance()->deleteTables();
} elseif ($option==='backup') {
DBManager::getInstance()->backupTables();
DBManager::getInstance()->archive(time())->backupTables();
}
} else {
// 操作多个模块
Expand All @@ -47,22 +43,22 @@ public function onRequest(Request $request)
_D()->info($select);
$tables=array_keys($select);
if ($option==='recovery') {
DBManager::importTables($tables);
DBManager::getInstance()->createTables($tables)->importTables($tables);
} elseif ($option==='delete') {
DBManager::deleteTables($tables);
DBManager::getInstance()->deleteTables($tables);
} elseif ($option==='backup') {
DBManager::backupTables($tables);
DBManager::getInstance()->backupTables($tables);
}
}
// 操作单个模块
else if ($module=$request->get()->module){
elseif ($module=$request->get()->module) {
$tables=[$module];
if ($option==='recovery') {
DBManager::importTables($tables);
DBManager::getInstance()->createTables($tables)->importTables($tables);
} elseif ($option==='delete') {
DBManager::deleteTables($tables);
DBManager::getInstance()->deleteTables($tables);
} elseif ($option==='backup') {
DBManager::backupTables($tables);
DBManager::getInstance()->backupTables($tables);
}
}
}
Expand Down

0 comments on commit 603a0f9

Please sign in to comment.