后台代码格式化,内核更新
This commit is contained in:
@@ -18,52 +18,52 @@ class Database extends Admin {
|
||||
*/
|
||||
public function index($type = null) {
|
||||
switch ($type) {
|
||||
/* 数据还原 */
|
||||
case 'import':
|
||||
//列出备份文件列表
|
||||
$path = config('data_backup_path');
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
$path = realpath($path);
|
||||
$flag = \FilesystemIterator::KEY_AS_FILENAME;
|
||||
$glob = new \FilesystemIterator($path, $flag);
|
||||
|
||||
$list = array();
|
||||
foreach ($glob as $name => $file) {
|
||||
if (preg_match('/^\d{8,8}-\d{6,6}-\d+\.sql(?:\.gz)?$/', $name)) {
|
||||
$name = sscanf($name, '%4s%2s%2s-%2s%2s%2s-%d');
|
||||
|
||||
$date = "{$name[0]}-{$name[1]}-{$name[2]}";
|
||||
$time = "{$name[3]}:{$name[4]}:{$name[5]}";
|
||||
$part = $name[6];
|
||||
|
||||
if (isset($list["{$date} {$time}"])) {
|
||||
$info = $list["{$date} {$time}"];
|
||||
$info['part'] = max($info['part'], $part);
|
||||
$info['size'] = $info['size'] + $file->getSize();
|
||||
} else {
|
||||
$info['part'] = $part;
|
||||
$info['size'] = $file->getSize();
|
||||
}
|
||||
$extension = strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
|
||||
$info['compress'] = ($extension === 'SQL') ? '-' : $extension;
|
||||
$info['time'] = strtotime("{$date} {$time}");
|
||||
|
||||
$list["{$date} {$time}"] = $info;
|
||||
/* 数据还原 */
|
||||
case 'import':
|
||||
//列出备份文件列表
|
||||
$path = config('data_backup_path');
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
$path = realpath($path);
|
||||
$flag = \FilesystemIterator::KEY_AS_FILENAME;
|
||||
$glob = new \FilesystemIterator($path, $flag);
|
||||
|
||||
$list = array();
|
||||
foreach ($glob as $name => $file) {
|
||||
if (preg_match('/^\d{8,8}-\d{6,6}-\d+\.sql(?:\.gz)?$/', $name)) {
|
||||
$name = sscanf($name, '%4s%2s%2s-%2s%2s%2s-%d');
|
||||
|
||||
$date = "{$name[0]}-{$name[1]}-{$name[2]}";
|
||||
$time = "{$name[3]}:{$name[4]}:{$name[5]}";
|
||||
$part = $name[6];
|
||||
|
||||
if (isset($list["{$date} {$time}"])) {
|
||||
$info = $list["{$date} {$time}"];
|
||||
$info['part'] = max($info['part'], $part);
|
||||
$info['size'] = $info['size'] + $file->getSize();
|
||||
} else {
|
||||
$info['part'] = $part;
|
||||
$info['size'] = $file->getSize();
|
||||
}
|
||||
$extension = strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
|
||||
$info['compress'] = ($extension === 'SQL') ? '-' : $extension;
|
||||
$info['time'] = strtotime("{$date} {$time}");
|
||||
|
||||
$list["{$date} {$time}"] = $info;
|
||||
}
|
||||
$title = '数据还原';
|
||||
}
|
||||
$title = '数据还原';
|
||||
break;
|
||||
/* 数据备份 */
|
||||
case 'export':
|
||||
$Db = \think\Db::connect();
|
||||
$list = $Db->query('SHOW TABLE STATUS');
|
||||
$list = array_map('array_change_key_case', $list);
|
||||
$title = '数据备份';
|
||||
/* 数据备份 */
|
||||
case 'export':
|
||||
$Db = \think\Db::connect();
|
||||
$list = $Db->query('SHOW TABLE STATUS');
|
||||
$list = array_map('array_change_key_case', $list);
|
||||
$title = '数据备份';
|
||||
break;
|
||||
default:
|
||||
return $this->error('参数错误!');
|
||||
default:
|
||||
return $this->error('参数错误!');
|
||||
}
|
||||
//渲染模板
|
||||
$this->setMeta($title);
|
||||
@@ -80,8 +80,8 @@ class Database extends Admin {
|
||||
$Db = \think\Db::connect();
|
||||
if (is_array($tables)) {
|
||||
$tables = implode('`,`', $tables);
|
||||
$list = $Db->query("OPTIMIZE TABLE `{$tables}`");
|
||||
|
||||
$list = $Db->query("OPTIMIZE TABLE `{$tables}`");
|
||||
|
||||
if ($list) {
|
||||
return $this->success("数据表优化完成!");
|
||||
} else {
|
||||
@@ -109,8 +109,8 @@ class Database extends Admin {
|
||||
$Db = \think\Db::connect();
|
||||
if (is_array($tables)) {
|
||||
$tables = implode('`,`', $tables);
|
||||
$list = $Db->query("REPAIR TABLE `{$tables}`");
|
||||
|
||||
$list = $Db->query("REPAIR TABLE `{$tables}`");
|
||||
|
||||
if ($list) {
|
||||
return $this->success("数据表修复完成!");
|
||||
} else {
|
||||
@@ -155,13 +155,14 @@ class Database extends Admin {
|
||||
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
|
||||
*/
|
||||
public function export($tables = null, $id = null, $start = null) {
|
||||
if (IS_POST && !empty($tables) && is_array($tables)) { //初始化
|
||||
if (IS_POST && !empty($tables) && is_array($tables)) {
|
||||
//初始化
|
||||
$path = config('data_backup_path');
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
//读取备份配置
|
||||
$config = array('path' => realpath($path) . DIRECTORY_SEPARATOR, 'part' => config('data_backup_part_size'), 'compress' => config('data_backup_compress'), 'level' => config('data_backup_compress_level'),);
|
||||
$config = array('path' => realpath($path) . DIRECTORY_SEPARATOR, 'part' => config('data_backup_part_size'), 'compress' => config('data_backup_compress'), 'level' => config('data_backup_compress_level'));
|
||||
//检查是否有正在执行的任务
|
||||
$lock = "{$config['path']}backup.lock";
|
||||
if (is_file($lock)) {
|
||||
@@ -176,7 +177,7 @@ class Database extends Admin {
|
||||
}
|
||||
session('backup_config', $config);
|
||||
//生成备份文件信息
|
||||
$file = array('name' => date('Ymd-His', time()), 'part' => 1,);
|
||||
$file = array('name' => date('Ymd-His', time()), 'part' => 1);
|
||||
session('backup_file', $file);
|
||||
//缓存要备份的表
|
||||
session('backup_tables', $tables);
|
||||
@@ -188,18 +189,22 @@ class Database extends Admin {
|
||||
} else {
|
||||
return $this->error('初始化失败,备份文件创建失败!');
|
||||
}
|
||||
} elseif (IS_GET && is_numeric($id) && is_numeric($start)) { //备份数据
|
||||
} elseif (IS_GET && is_numeric($id) && is_numeric($start)) {
|
||||
//备份数据
|
||||
$tables = session('backup_tables');
|
||||
//备份指定表
|
||||
$Database = new \com\Database(session('backup_file'), session('backup_config'));
|
||||
$start = $Database->backup($tables[$id], $start);
|
||||
if (false === $start) { //出错
|
||||
$start = $Database->backup($tables[$id], $start);
|
||||
if (false === $start) {
|
||||
//出错
|
||||
return $this->error('备份出错!');
|
||||
} elseif (0 === $start) { //下一表
|
||||
} elseif (0 === $start) {
|
||||
//下一表
|
||||
if (isset($tables[++$id])) {
|
||||
$tab = array('id' => $id, 'start' => 0);
|
||||
return $this->success('备份完成!', '', array('tab' => $tab));
|
||||
} else { //备份完成,清空缓存
|
||||
} else {
|
||||
//备份完成,清空缓存
|
||||
unlink(session('backup_config.path') . 'backup.lock');
|
||||
session('backup_tables', null);
|
||||
session('backup_file', null);
|
||||
@@ -207,11 +212,12 @@ class Database extends Admin {
|
||||
return $this->success('备份完成!');
|
||||
}
|
||||
} else {
|
||||
$tab = array('id' => $id, 'start' => $start[0]);
|
||||
$tab = array('id' => $id, 'start' => $start[0]);
|
||||
$rate = floor(100 * ($start[0] / $start[1]));
|
||||
return $this->success("正在备份...({$rate}%)", '', array('tab' => $tab));
|
||||
}
|
||||
} else { //出错
|
||||
} else {
|
||||
//出错
|
||||
return $this->error('参数错误!');
|
||||
}
|
||||
}
|
||||
@@ -220,16 +226,17 @@ class Database extends Admin {
|
||||
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
|
||||
*/
|
||||
public function import($time = 0, $part = null, $start = null) {
|
||||
if (is_numeric($time) && is_null($part) && is_null($start)) { //初始化
|
||||
if (is_numeric($time) && is_null($part) && is_null($start)) {
|
||||
//初始化
|
||||
//获取备份文件信息
|
||||
$name = date('Ymd-His', $time) . '-*.sql*';
|
||||
$path = realpath(config('data_backup_path')) . DIRECTORY_SEPARATOR . $name;
|
||||
$name = date('Ymd-His', $time) . '-*.sql*';
|
||||
$path = realpath(config('data_backup_path')) . DIRECTORY_SEPARATOR . $name;
|
||||
$files = glob($path);
|
||||
$list = array();
|
||||
$list = array();
|
||||
foreach ($files as $name) {
|
||||
$basename = basename($name);
|
||||
$match = sscanf($basename, '%4s%2s%2s-%2s%2s%2s-%d');
|
||||
$gz = preg_match('/^\d{8,8}-\d{6,6}-\d+\.sql.gz$/', $basename);
|
||||
$basename = basename($name);
|
||||
$match = sscanf($basename, '%4s%2s%2s-%2s%2s%2s-%d');
|
||||
$gz = preg_match('/^\d{8,8}-\d{6,6}-\d+\.sql.gz$/', $basename);
|
||||
$list[$match[6]] = array($match[6], $name, $gz);
|
||||
}
|
||||
ksort($list);
|
||||
@@ -243,14 +250,15 @@ class Database extends Admin {
|
||||
}
|
||||
} elseif (is_numeric($part) && is_numeric($start)) {
|
||||
$list = session('backup_list');
|
||||
|
||||
|
||||
$db = new \com\Database($list[$part], array('path' => realpath(config('data_backup_path')) . DIRECTORY_SEPARATOR, 'compress' => $list[$part][2]));
|
||||
|
||||
|
||||
$start = $db->import($start);
|
||||
|
||||
|
||||
if (false === $start) {
|
||||
return $this->error('还原数据出错!');
|
||||
} elseif (0 === $start) { //下一卷
|
||||
} elseif (0 === $start) {
|
||||
//下一卷
|
||||
if (isset($list[++$part])) {
|
||||
$data = array('part' => $part, 'start' => 0);
|
||||
return $this->success("正在还原...#{$part}", '', $data);
|
||||
@@ -263,8 +271,7 @@ class Database extends Admin {
|
||||
if ($start[1]) {
|
||||
$rate = floor(100 * ($start[0] / $start[1]));
|
||||
return $this->success("正在还原...#{$part} ({$rate}%)", '', $data);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$data['gz'] = 1;
|
||||
return $this->success("正在还原...#{$part}", '', $data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user