Skip to content

Commit 7443d9b

Browse files
committed
管理员新增备份数据库、删除数据库以及还原数据操作
1 parent 1dc1027 commit 7443d9b

File tree

9 files changed

+344
-250
lines changed

9 files changed

+344
-250
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace app\admin\controller;
4+
5+
use gophp\helper\file;
6+
use gophp\page;
7+
use gophp\request;
8+
use gophp\response;
9+
use gophp\schema;
10+
11+
class db extends auth {
12+
13+
public function index()
14+
{
15+
$db = \gophp\db::instance();
16+
17+
$table_suffix = $db->suffix;
18+
$table_name = $table_suffix .'dbbak';
19+
20+
$sql = "select * from $table_name order by id desc";
21+
22+
$total = count($db->show(false)->query($sql));
23+
24+
$pre_rows = 10;
25+
26+
$page = new page($total, $pre_rows);
27+
28+
$dbbaks = $db->show(false)->query($sql, $pre_rows);
29+
30+
$this->assign('page', $page);
31+
$this->assign('dbbaks', $dbbaks);
32+
33+
$this->display('db/index');
34+
35+
}
36+
37+
/**
38+
* 备份数据
39+
*/
40+
public function backup()
41+
{
42+
43+
if(!request::isAjax()){
44+
45+
response::ajax(['code' => 300, 'msg' => '非法请求方式']);
46+
47+
}
48+
$schema = schema::instance();
49+
$tables = $schema->getTables();
50+
51+
$path = RUNTIME_PATH . '/data/';
52+
53+
$file = $path . date('YmdHis') . '_all.sql';
54+
$sql = '';
55+
foreach ($tables as $table) {
56+
// 如果存在则删除表
57+
$sql .= "DROP TABLE IF EXISTS `" . $table . '`;\r\n';
58+
// 创建表结构
59+
$sql .= $schema->getCreateTableSql($table) . ';\r\n';
60+
61+
$sql .="INSERT INTO `$table` VALUES(";
62+
63+
$data = $schema->query("select * from $table")->fetchAll(\PDO::FETCH_ASSOC);
64+
65+
foreach ($data as $v){
66+
foreach ($v as $v1){
67+
$sql .= '"'.$v1.'",';
68+
}
69+
}
70+
71+
$sql = trim($sql, ',');
72+
73+
$sql .= ");\r\n";
74+
}
75+
76+
if(!file::create($file, $sql)){
77+
78+
response::ajax(['code' => 301, 'msg' => '备份失败,请检查'.$path.'是否有可写权限']);
79+
80+
}
81+
82+
$size = file::getInfo($file,'size')/1024;
83+
84+
$result = db('dbbak')->add(['file' => date('YmdHis') . '_all.sql','size' => $size]);
85+
86+
if($result){
87+
88+
response::ajax(['code' => 200, 'msg' => '备份成功!']);
89+
90+
}
91+
92+
response::ajax(['code' => 302, 'msg' => '备份失败!']);
93+
94+
}
95+
96+
/**
97+
* 下载数据
98+
*/
99+
public function download()
100+
{
101+
102+
$id = request::get('id', 0);
103+
104+
$dbbak = db('dbbak')->find($id);
105+
106+
$file = RUNTIME_PATH . '/data/' . $dbbak['file'];
107+
108+
response::download($file);
109+
110+
response::ajax(['code'=> 200, 'msg'=>'下载成功']);
111+
112+
}
113+
114+
/**
115+
* 删除文件
116+
*/
117+
public function delete()
118+
{
119+
120+
$id = request::post('id', 0);
121+
122+
$dbbak = db('dbbak')->find($id);
123+
124+
$file = RUNTIME_PATH . '/data/' . $dbbak['file'];
125+
126+
if(!file::delete($file)){
127+
128+
response::ajax(['code'=> 300, 'msg'=>'删除失败']);
129+
130+
}
131+
132+
$result = db('dbbak')->delete($id);
133+
134+
if(!$result){
135+
136+
response::ajax(['code'=> 301, 'msg'=>'删除失败']);
137+
138+
}
139+
140+
response::ajax(['code'=> 200, 'msg'=>'删除成功']);
141+
142+
}
143+
144+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
{{include_file name='public/header' title='数据库备份/还原'}}
2+
<style>
3+
.table {
4+
width: 100%;
5+
max-width: 100%;
6+
margin-bottom: 0;
7+
}
8+
</style>
9+
</head>
10+
11+
<body>
12+
13+
<div id="wrapper">
14+
15+
<!-- Navigation -->
16+
{{include_file name='public/nav' sidebar='sidebar'}}
17+
<div id="page-wrapper">
18+
<div class="row">
19+
<div class="col-lg-12">
20+
<div class="page-header">
21+
<h1>数据库备份 </h1>
22+
<div class="opt-btn">
23+
<a href="javascript:void(0);" class="btn btn-sm btn-warning js_backupBtn" data-placement="bottom" data-id="26">备份</a>
24+
</div>
25+
</div>
26+
</div>
27+
<!-- /.col-lg-12 -->
28+
</div>
29+
<!-- /.row -->
30+
31+
<div class="row">
32+
<div class="col-sm-12">
33+
<div class="panel panel-default">
34+
35+
<!-- /.panel-heading -->
36+
<div class="panel-body">
37+
<div class="table-responsive">
38+
<table class="table table-striped table-bordered table-hover">
39+
<thead>
40+
<tr>
41+
<th>#</th>
42+
<th>文件名</th>
43+
<th>文件大小</th>
44+
<th>备份时间</th>
45+
<th>操作</th>
46+
</tr>
47+
</thead>
48+
<tbody>
49+
50+
{{foreach $dbbaks as $k=>$dbbak}}
51+
52+
<tr>
53+
<td>{{$k+1}}</td>
54+
<td>{{$dbbak.file}}</td>
55+
<td>{{$dbbak.size}}KB</td>
56+
<td>{{$dbbak.add_time}}</td>
57+
<td style="width: 25%;">
58+
<a href="{{url("admin/db/download")}}?id={{$dbbak.id}}" class="btn btn-warning btn-xs js_downloadBtn" data-id="{{$dbbak.id}}">下载文件</a>
59+
<button type="button" class="btn btn-danger btn-xs js_deleteBtn" data-id="{{$dbbak.id}}">删除文件</button>
60+
<button type="button" class="btn btn-success btn-xs js_restoreBtn" data-id="{{$dbbak.id}}">还原数据</button>
61+
</td>
62+
</tr>
63+
64+
{{/foreach}}
65+
</tbody>
66+
</table>
67+
</div>
68+
<!-- /.table-responsive -->
69+
</div>
70+
<!-- /.panel-body -->
71+
</div>
72+
</div>
73+
<!-- /.panel -->
74+
<div class="col-sm-12">
75+
{{include_file name='public/page'}}
76+
</div>
77+
</div>
78+
79+
80+
<!-- /#page-wrapper -->
81+
82+
</div>
83+
<!-- /#wrapper -->
84+
85+
<hr>
86+
<p class="text-center">{{get_config('copyright')}}</p>
87+
<script>
88+
$(function () {
89+
// 备份
90+
$(".js_backupBtn").click(function () {
91+
var url = "{{url('admin/db/backup')}}";
92+
$.post(url, {}, function(json){
93+
94+
alert(json.msg, 1000, function(){
95+
96+
window.location.reload();
97+
98+
});
99+
100+
}, 'json');
101+
});
102+
// 删除文件
103+
$(".js_deleteBtn").click(function () {
104+
var thisObj = $(this);
105+
var url = "{{url('admin/db/delete')}}";
106+
var id = thisObj.data('id');
107+
108+
confirm('确认要删除备份文件?', function(){
109+
110+
$.post(url, { id:id }, function(json){
111+
112+
if(json.code == 200){
113+
114+
alert(json.msg, 500, function () {
115+
thisObj.closest('tr').remove();
116+
});
117+
118+
}else{
119+
120+
alert(json.msg, 2000);
121+
122+
}
123+
124+
}, 'json');
125+
});
126+
127+
});
128+
129+
// 还原数据
130+
$(".js_restoreBtn").click(function () {
131+
var thisObj = $(this);
132+
var url = "{{url('admin/db/delete')}}";
133+
var id = thisObj.data('id');
134+
135+
alert('暂不支持,敬请期待', 1000);return;
136+
137+
confirm('确认要将数据还原?', function(){
138+
139+
$.post(url, { id:id }, function(json){
140+
141+
if(json.code == 200){
142+
143+
alert(json.msg, 500, function () {
144+
thisObj.closest('tr').remove();
145+
});
146+
147+
}else{
148+
149+
alert(json.msg, 2000);
150+
151+
}
152+
153+
}, 'json');
154+
});
155+
156+
});
157+
});
158+
</script>
159+
{{include_file name='public/footer'}}

application/admin/view/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ <h1 class="page-header">管理中心</h1>
145145
</div>
146146
<!-- /.panel-heading -->
147147
<div class="panel-body">
148-
<p>程序版本:V{{$system.version}} [<a href="">查看最新版本</a>]</p>
148+
<p>程序版本:V{{$system.version}} [<a href="https://github.com/gouguoyin/phprap" target="_blank">查看最新版本</a>]</p>
149149
<p>服务器环境:{{PHP_OS}}</p>
150-
<p>文件上传大小:10M</p>
150+
<p>使用技术:PHP+MySQL+Bootstrap+JQuery</p>
151151
<p>系统开发:<a target="_blank" href="http://www.gouguoyin.cn/about.html">勾国印</a></p>
152-
<p>演示网站:<a target="_blank" href="http://apidoc.gouguoyin.cn/">apidoc.gouguoyin.cn</a></p>
152+
<p>帮助中心:<a target="_blank" href="https://github.com/gouguoyin/apidoc/blob/master/README.md">help.gouguoyin.cn</a></p>
153+
<p>官方网站:<a target="_blank" href="http://phprap.gouguoyin.cn/">phprap.gouguoyin.cn</a></p>
153154
<p>官方QQ群:<a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=cf9440f673d6a29150bb8f033ed5fdf0e4e685f92350b7006778660bf8580f57"><img border="0" src="//pub.idqqimg.com/wpa/images/group.png" alt="GoPHP官方交流群" title="GoPHP官方交流群"></a></p>
154-
<p>帮助中心:<a href="https://github.com/gouguoyin/apidoc/blob/master/README.md">help.gouguoyin.cn</a></p>
155155
</div>
156156
<!-- /.panel-body -->
157157
</div>

application/admin/view/public/sidebar.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<a href="{{url('admin/history/login')}}"><i class="fa fa-history fa-fw"></i> 登录历史</a>
1919
</li>
2020

21+
<li>
22+
<a href="{{url('admin//db/index')}}"><i class="fa fa-database fa-fw"></i> 数据备份</a>
23+
</li>
24+
2125
<li>
2226
<a href="{{url('admin/setting')}}"><i class="fa fa-gear fa-fw"></i> 系统设置</a>
2327
</li>

application/admin/view/setting/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ <h1 class="page-header">系统设置</h1>
3636

3737
<div class="form-group">
3838
<label>网站描述</label>
39-
<textarea class="form-control" name="config[description]" rows="2" ignore="ignore" datatype="*" nullmsg="请输入版权信息!">{{$config['description']}}</textarea>
39+
<textarea class="form-control" name="config[description]" rows="1" ignore="ignore" datatype="*" nullmsg="请输入版权信息!">{{$config['description']}}</textarea>
4040
</div>
4141

4242
<div class="form-group">

application/home/controller/test.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use gophp\backup;
1111
use gophp\config;
1212
use gophp\db;
13+
use gophp\helper\file;
1314
use gophp\helper\url;
1415
use gophp\schema;
1516

@@ -21,17 +22,15 @@ class test {
2122
*/
2223
public function index(){
2324

25+
$a = RUNTIME_PATH.'/data/20171028223004_all.sql';
2426

27+
$b = file::getInfo($a,'size')/1024;
2528

26-
$str = "{jkl:334}";
2729

28-
$a = preg_replace('/([a-zA-z]+)(?=:)/g', "$", $str);
30+
dump($b);
2931

3032

3133

32-
dump($a);
33-
34-
3534
}
3635

3736
public function pdo_ping($dbconn){

gophp/bootstrap/const.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
defined('GOPHP_PATH') or define('GOPHP_PATH',dirname(dirname(__FILE__)));
88

99
// 定义框架版本
10-
defined('GOPHP_VERSION') or define('GOPHP_VERSION', '2.0.1');
10+
defined('GOPHP_VERSION') or define('GOPHP_VERSION', '1.0.0.0');
1111

1212
// 定义框架类库目录
1313
defined('GOPHP_LIB') or define('GOPHP_LIB', GOPHP_PATH . DS . 'library');

0 commit comments

Comments
 (0)