第一步:composer安装第三方类库
composer require tp5er/tp5-databackup dev-master
第二步:创建控制器(路径:app/portal/controller/) AdminDatabackupController.php,引入类文件
第三步:创建前端页面 (路径:public/themes/admin_simpleboot3/portal/)
文件夹名臣与控制器名称对应 AdminDatabackup 对应 admin_databackup
第四步:后台添加菜单 子菜单 方法栏为 index,importlist
第五步:编写类方法
<?php
namespace app\portal\controller;
use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use \tp5er\Backup;
use think\Db;
/**
* 数据备份
*/
class AdminDatabackupController extends AdminBaseController
{
private $config=array(
'path' => './Data/', //数据库备份路径
'part' => 20971520, //数据库备份卷大小
'compress' => 0, //数据库备份文件是否启用压缩 0不压缩 1 压缩
'level' => 9 //数据库备份文件压缩级别 1 普通 4 一般 9最高
);
// 数据类表列表
public function index()
{
$config = $this->config;
$db= new Backup($config);
return $this->fetch('index',['list'=>$db->dataList()]);
}
// 多表备份
public function backupall($tables)
{
$config = $this->config;
$db= new Backup($config);
foreach ($tables as $key => $value) {
$file=['name'=>date('Ymd-His'),'part'=>$config['part']];
$start = $db->setFile($file)->backup($value, 0);
}
if ($start==0) {
return $this->success('备份成功');
}else{
return $this->error('备份失败');
}
}
//单表备份
public function backup()
{
$param = $this->request->param();
$config = $this->config;
$db= new Backup($config);
$file=['name'=>date('Ymd-His'),'part'=>$config['part']];
$start = $db->setFile($file)->backup($param['table'], 0);
if ($start==0) {
return $this->success('备份成功');
}else{
return $this->error('备份失败');
}
}
//$db->repair($tables)//修复表
public function repair()
{
$param = $this->request->param();
$config = $this->config;
$db= new Backup($config);
$db->repair($param['table']);
return $this->success('修复成功');
}
// $db->optimize($tables)//优化表
public function optimize()
{
$param = $this->request->param();
$config = $this->config;
$db= new Backup($config);
$db->optimize($param['table']);
return $this->success('优化成功');
}
// 备份文件列表
public function importlist()
{
$config = $this->config;
$db= new Backup($config);
return $this->fetch('importlist',['list'=>$db->fileList()]);
}
// 导入备份文件
public function import($time)
{
$config = $this->config;
$db= new Backup($config);
$start= $db->import(0,$time);
if ($start==0) {
return $this->success('还原成功');
}else{
return $this->error('还原失败');
}
}
// 删除备份文件
public function del($time)
{
$config = $this->config;
$db= new Backup($config);
$db->delFile($time);
return $this->success('删除成功');
}
}
?>
前端代码index.html:
<include file="public@header"/>
</head>
<body>
<div class="wrap js-check-wrap">
<ul class="nav nav-tabs">
<li class="active"><a href="javascript:;">数据表</a></li>
</ul>
<form class="well form-inline margin-top-20" method="get" action="{:url('AdminDatabackup/backup')}">
<a class="btn btn-primary" id="btn" >全部备份</a>
</form>
<form class="js-ajax-form" action="" method="post">
<table class="table table-hover table-bordered table-list">
<thead>
<tr>
<th width="15">
<label>
<input type="checkbox" class="js-check-all" data-direction="x" data-checklist="js-check-x">
</label>
</th>
<th>表名</th>
<th width="150">数据量</th>
<th width="160">数据大小</th>
<th width="160">备份时间</th>
<th width="180">操作</th>
</tr>
</thead>
<foreach name="list" item="vo">
<tr>
<td>
<input type="checkbox" class="js-check" data-yid="js-check-y" data-xid="js-check-x" name="tables[]"
value="{$vo.name}" title="table:{$vo.name}">
</td>
<td><b>{$vo.name}</b></td>
<td>{$vo.rows}</td>
<td>{$vo.data_length}</td>
<td>{$vo.create_time}</td>
<td>
<a class="btn btn-xs btn-primary js-ajax-dialog-btn" data-msg="确定备份吗?"
href="{:url('AdminDatabackup/backup',array('table'=>$vo.name))}">备份</a>
<a class="btn btn-xs btn-primary js-ajax-dialog-btn" data-msg="确定优化吗?"
href="{:url('AdminDatabackup/optimize',array('table'=>$vo.name))}">优化表</a>
<a class="btn btn-xs btn-warning js-ajax-dialog-btn" data-msg="确定修复吗?"
href="{:url('AdminDatabackup/repair',array('table'=>$vo.name))}">修复表</a>
</td>
</tr>
</foreach>
<tfoot>
<tr>
<th width="15"><label><input type="checkbox" class="js-check-all" data-direction="x"
data-checklist="js-check-x"></label></th>
<th>表名</th>
<th>备份进度</th>
<th width="150">数据量</th>
<th width="160">数据大小</th>
<th width="160">备份时间</th>
<th width="180">操作</th>
</tr>
</tfoot>
</table>
<ul class="pagination">{$page|default=''}</ul>
</form>
</div>
<script src="__STATIC__/js/admin.js"></script>
<script>
function reloadPage(win) {
win.location.reload();
}
$(function () {
setCookie("refersh_time", 0);
Wind.use('ajaxForm', 'artDialog', 'iframeTools', function () {
//批量复制
$('.js-articles-copy').click(function (e) {
var ids = [];
$("input[name='ids[]']").each(function () {
if ($(this).is(':checked')) {
ids.push($(this).val());
}
});
if (ids.length == 0) {
art.dialog.through({
id: 'error',
icon: 'error',
content: '您没有勾选信息,无法进行操作!',
cancelVal: '关闭',
cancel: true
});
return false;
}
ids = ids.join(',');
art.dialog.open("__ROOT__/index.php?g=portal&m=AdminArticle&a=copy&ids=" + ids, {
title: "批量复制",
width: "300px"
});
});
//批量移动
$('.js-articles-move').click(function (e) {
var ids = [];
$("input[name='ids[]']").each(function () {
if ($(this).is(':checked')) {
ids.push($(this).val());
}
});
if (ids.length == 0) {
art.dialog.through({
id: 'error',
icon: 'error',
content: '您没有勾选信息,无法进行操作!',
cancelVal: '关闭',
cancel: true
});
return false;
}
ids = ids.join(',');
art.dialog.open("__ROOT__/index.php?g=portal&m=AdminArticle&a=move&old_term_id={$term.term_id|default=0}&ids=" + ids, {
title: "批量移动",
width: "300px"
});
});
$('#btn').click(function(){
var tables = [];
$("input[name='tables[]']").each(function () {
if ($(this).is(':checked')) {
tables.push($(this).val());
}
});
if (tables.length == 0) {
art.dialog.through({
id: 'error',
icon: 'error',
content: '您没有勾选信息,无法进行操作!',
cancelVal: '关闭',
cancel: true
});
return false;
}
$.post("{:cmf_url('portal/AdminDatabackup/backupall')}",{'tables':tables},function(re){
if (re.flag) {
art.dialog.through({
id: 'error',
icon: 'error',
content: re.flag,
cancelVal: '关闭',
cancel: true
});
return false;
}else{
art.dialog.through({
id: 'face-smile',
icon: 'face-smile',
content: re.success,
cancelVal: '关闭',
cancel: true
});
return false;
}
},'json');
//alert(tables);return false;
});
});
});
</script>
</body>
</html>
前端importlist.html
<include file="public@header"/>
</head>
<body>
<div class="wrap js-check-wrap">
<ul class="nav nav-tabs">
<li class="active"><a href="javascript:;">备份文件</a></li>
</ul>
<form class="js-ajax-form" action="" method="post">
<table class="table table-hover table-bordered table-list">
<thead>
<tr>
<th>备份名称</th>
<th width="150">卷数</th>
<th width="160">压缩方式</th>
<th width="160">数据大小</th>
<th width="180">操作</th>
</tr>
</thead>
<foreach name="list" item="vo">
<tr>
<td><b>{$vo.time|date="Ymd-His",###}</b></td>
<td>{$vo.part}</td>
<td>{$vo.compress}</td>
<td>{$vo.size}</td>
<td>
<a class="btn btn-xs btn-primary js-ajax-dialog-btn" data-msg="确定还原吗?"
href="{:url('AdminDatabackup/import',array('time'=>$vo.time))}">还原</a>
<a class="btn btn-xs btn-warning js-ajax-delete" data-msg="确定删除吗?"
href="{:url('AdminDatabackup/del',array('time'=>$vo.time))}">删除</a>
</td>
</tr>
</foreach>
<tfoot>
<tr>
<th>备份名称</th>
<th width="150">卷数</th>
<th width="160">压缩方式</th>
<th width="160">数据大小</th>
<th width="180">操作</th>
</tr>
</tfoot>
</table>
<ul class="pagination">{$page|default=''}</ul>
</form>
</div>
<script src="__STATIC__/js/admin.js"></script>
<script>
function reloadPage(win) {
win.location.reload();
}
$(function () {
setCookie("refersh_time", 0);
Wind.use('ajaxForm', 'artDialog', 'iframeTools', function () {
//批量复制
$('.js-articles-copy').click(function (e) {
var ids = [];
$("input[name='ids[]']").each(function () {
if ($(this).is(':checked')) {
ids.push($(this).val());
}
});
if (ids.length == 0) {
art.dialog.through({
id: 'error',
icon: 'error',
content: '您没有勾选信息,无法进行操作!',
cancelVal: '关闭',
cancel: true
});
return false;
}
ids = ids.join(',');
art.dialog.open("__ROOT__/index.php?g=portal&m=AdminArticle&a=copy&ids=" + ids, {
title: "批量复制",
width: "300px"
});
});
//批量移动
$('.js-articles-move').click(function (e) {
var ids = [];
$("input[name='ids[]']").each(function () {
if ($(this).is(':checked')) {
ids.push($(this).val());
}
});
if (ids.length == 0) {
art.dialog.through({
id: 'error',
icon: 'error',
content: '您没有勾选信息,无法进行操作!',
cancelVal: '关闭',
cancel: true
});
return false;
}
ids = ids.join(',');
art.dialog.open("__ROOT__/index.php?g=portal&m=AdminArticle&a=move&old_term_id={$term.term_id|default=0}&ids=" + ids, {
title: "批量移动",
width: "300px"
});
});
});
});
</script>
</body>
</html>
第六步:完成