1, //数据库备份文件是否启用压缩 0不压缩 1 压缩 'level' => 5, ); $this->DB = new Backup($config); } /** * 数据类表列表 */ public function index() { return $this->fetch(); } /** * 获取数据库表 */ public function tablelist() { $db = $this->DB; return Json::result(0, 'sucess', $db->dataList(), count($db->dataList())); } /** * 查看表结构 */ public function seetable() { request()->filter(['strip_tags', 'trim', 'htmlspecialchars']); $database = Config::get("database.connections." . Config::get('database.default') . '.database'); $tablename = request()->param('tablename'); $res = Db::query("select * from information_schema.columns where table_name = '" . $tablename . "' and table_schema = '" . $database . "'"); $html = ''; $html .= ''; $html .= ''; $html .= ''; foreach ($res AS $f) { $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; } $html .= '
字段名数据类型默认值允许非空自动递增备注
' . $f['COLUMN_NAME'] . '' . $f['COLUMN_TYPE'] . '' . $f['COLUMN_DEFAULT'] . '' . $f['IS_NULLABLE'] . '' . ($f['EXTRA'] == 'auto_increment' ? '是' : ' ') . '' . $f['COLUMN_COMMENT'] . '

'; $html .= '

总共:' . count($res) . '个字段

'; $html .= ''; echo ''; echo $html; } /** * 优化表 */ public function optimize() { $tables = request()->post('tables/a'); $db = $this->DB; try { $db->optimize($tables); return Json::successful('优化成功'); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } /** * 修复表 */ public function repair() { $tables = request()->post('tables/a'); $db = $this->DB; try { $db->repair($tables); return Json::successful('修复成功'); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } /** * 备份表 */ public function backup() { $tables = request()->post('tables/a'); $db = $this->DB; $data = ''; foreach ($tables as $t) { $res = $db->backup($t, 0); if ($res == false && $res != 0) { $data .= $t . '|'; } } return Json::successful($data ? '备份失败' . $data : '备份成功'); } /** * 获取备份记录表 */ public function fileList() { $db = $this->DB; $files = $db->fileList(); $data = []; foreach ($files as $key => $t) { $data[$key]['filename'] = $t['filename']; $data[$key]['part'] = $t['part']; $data[$key]['size'] = $t['size'] . 'B'; $data[$key]['compress'] = $t['compress']; $data[$key]['backtime'] = $key; $data[$key]['time'] = $t['time']; } krsort($data);//根据时间降序 return Json::result(0, 'sucess', $data, count($data)); } /** * @param string|null $msg * @param string|null $url * @param array|null $data */ public function success(?string $msg = null, ?string $url = null, ?array $data = []) { return Json::success($msg, $data); } /** * @param string|null $msg */ public function error(?string $msg = null) { return Json::fail($msg); } /** * 删除备份记录表 */ public function delFile() { $feilname = intval(request()->post('feilname')); $files = $this->DB->delFile($feilname); return Json::result(0, 'sucess'); } /** * 导入备份记录表 */ public function import() { $part = request()->post('part') != '' ? intval(request()->post('part')) : null; $start = request()->post('start') != '' ? intval(request()->post('start')) : null; $time = intval(request()->post('time')); $db = $this->DB; if (is_numeric($time) && is_null($part) && is_null($start)) { $list = $db->getFile('timeverif', $time); if (is_array($list)) { session::set('backup_list', $list); $this->success('初始化完成!', '', array('part' => 1, 'start' => 0)); } else { $this->error('备份文件可能已经损坏,请检查!'); } } else if (is_numeric($part) && is_numeric($start)) { $list = session::get('backup_list'); $start = $db->setFile($list)->import($start); if (false === $start) { $this->error('还原数据出错!'); } elseif (0 === $start) { if (isset($list[++$part])) { $data = array('part' => $part, 'start' => 0); $this->success("正在还原...#{$part}", '', $data); } else { session::delete('backup_list'); $this->success('还原完成!'); } } else { $data = array('part' => $part, 'start' => $start[0]); if ($start[1]) { $rate = floor(100 * ($start[0] / $start[1])); $this->success("正在还原...#{$part}({$rate}%)", '', $data); } else { $data['gz'] = 1; $this->success("正在还原...#{$part}", '', $data); } $this->success("正在还原...#{$part}", ''); } } else { $this->error('参数错误!'); } } /** * 下载备份记录表 */ public function downloadFile() { $time = intval(request()->param('feilname')); $this->DB->downloadFile($time); } }