This commit is contained in:
liyukun 2021-10-31 01:55:17 +08:00
parent 09eddda97f
commit 999f462663
3 changed files with 65 additions and 35 deletions

View File

@ -179,6 +179,10 @@ class Databases extends AuthController
return !count(glob($path)) ? app("json")->success("备份文件删除成功",'code') : app("json")->fail("备份文件删除失败,请检查权限");
}
/**
* 备份数据库
* @return mixed
@ -233,7 +237,8 @@ class Databases extends AuthController
//创建备份文件
$Database = new Database($file, $config);
$res = $Database->create();
return $res ? app("json")->success("初始化成功",'code') : app("json")->fail("初始化失败,备份文件创建失败!");
$tab = array('id' => 0, 'start' => 0);
return $res ? app("json")->success("初始化成功", array('tables' => $data['ids'], 'tab' => $tab)) : app("json")->fail("初始化失败,备份文件创建失败!");
} elseif (request()->isGet() && is_numeric($data['id']) && is_numeric($data['start'])) { //备份数据
$data['ids'] = session('backup_tables');
//备份指定表
@ -243,7 +248,8 @@ class Databases extends AuthController
$this->error('备份出错!');
} elseif (0 === $data['start']) { //下一表
if(isset($data['ids'][++$data['id']])){
return app("json")->success("备份完成",'code');
$tab = array('id' => $data['id'], 'start' => 0);
return app("json")->success($data['ids'][$data['id']]."备份完成",array('tab' => $tab));
} else { //备份完成,清空缓存
unlink(session('backup_config.path') . 'backup.lock');
session('backup_tables', null);
@ -252,8 +258,9 @@ class Databases extends AuthController
return app("json")->success("备份完成",'code');
}
} else {
$tab = array('id' => $data['id'], 'start' => $data['start'][0]);
$rate = floor(100 * ($data['start'][0] / $data['start'][1]));
return app("json")->success("正在备份...({$rate}%)",'code');
return app("json")->success("正在备份...({$rate}%)",array('tab' => $tab));
}
} else { //出错

View File

@ -3,7 +3,17 @@
<head>
<title>{$mate_title} - {:system_config("title")}</title>
{include file="public/header" /}
<link rel="stylesheet" href="__ADMIN_PATH__css/more.css">
<style>
.div-backdata{
width:400px;
padding:60px 20px;
text-align: center;
}
.div-backdata .layui-icon{
font-size: 50px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container-fluid p-t-15">
@ -35,6 +45,7 @@
{include file="public/footer"/}
<script type="text/javascript">
var loadIndex;
$('#tb_departments').bootstrapTable({
classes: 'table table-bordered table-hover table-striped',
url: '/admin/databases/Lst',
@ -150,39 +161,51 @@
checkID += selectedItem[i]['name']+",";
}
if (checkID=="") return lightyear.notify("没有选中项", 'danger', 3000, 'mdi mdi-emoticon-neutral', 'top', 'center');
$.confirm({
title: '重要提醒!',
content: '您确认要执行该操作吗?',
backgroundDismiss: true,
buttons: {
ok: {
text: '确认',
btnClass: 'btn-danger',
action: function () {
$.post("/admin/databases/export",data={ids:checkID},function (res) {
if (res.status == 200) {lightyear.notify(res.msg, 'success', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');location.reload();}
else lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-neutral', 'top', 'center');
})
loadIndex = parent.layer.open({
type: 1,
title:'',
area:'440px',
skin: 'layui-layer-demo', //样式类名
closeBtn: 0, //不显示关闭按钮
anim: 2,
content: '<div id="zz-div-backdata" class="zz-div-backdata"><i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i><div class="content">正在备份数据库,请稍等...</div></div>'
});
window.onbeforeunload = function(){ return "正在备份数据库,请不要关闭!" }
}
},
cancel: {
text: '取消',
btnClass: 'btn-primary'
}
}
loadIndex = parent.layer.open({
type: 1,
title:'',
area:'440px',
skin: 'layui-layer-demo', //样式类名
closeBtn: 0, //不显示关闭按钮
anim: 2,
content: '<div id="div-backdata" class="div-backdata"><i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i><div class="content">正在备份数据库,请稍等...</div></div>'
});
$.post("/admin/databases/export",data={ids:checkID},function (res) {
if (res.status == 200) {
updateBackDataInfo("开始备份,请不要关闭本页面!");
backup(res.data.tab);
window.onbeforeunload = function(){ return "正在备份数据库,请不要关闭!" }
} else{
parent.layer.close(loadIndex);
parent.layer.msg(res.msg?res.msg:'操作失败',{icon: 2,time:3*1000});
}
},"json")
return false;
})
function updateBackDataInfo(info) {
$('#div-backdata>.content', parent.document).text(info);
}
function backup(tab){
$.get("/admin/databases/export", tab, function(res){
if(res.status == 200){
updateBackDataInfo(res.msg);
if(!res.data){
parent.layer.close(loadIndex);
parent.layer.msg(res.msg?res.msg:'操作成功',{icon: 1,time:3*1000});
window.onbeforeunload = function(){ return null }
return;
}
backup(res.data.tab);
} else {
parent.layer.close(loadIndex);
parent.layer.msg(res.msg?res.msg:'操作失败',{icon: 2,time:3*1000});
}
}, "json");
}
$("#btn-optimize").click(function () {
var checkID = "";
var selectedItem = $('#tb_departments').bootstrapTable('getAllSelections');

View File

@ -1,7 +1,7 @@
<?php
namespace app\common\service;
use think\db;
use think\facade\Db;
//数据导出模型
class Database{