TP5框架实现的数据库备份功能示例
程序员文章站
2023-11-25 14:23:28
本文实例讲述了tp5框架实现的数据库备份功能。分享给大家供大家参考,具体如下:1、效果图2、下载扩展类( \extands\org\baksql.php)3、在 \public\static 里新建一...
本文实例讲述了tp5框架实现的数据库备份功能。分享给大家供大家参考,具体如下:
1、效果图
2、下载扩展类( \extands\org\baksql.php)
3、在 \public\static 里新建一个data 文件夹用来存放 .sql 的文件
4、使用方法
controller
<?php namespace app\index\controller; use think\controller; class backup extends controller { //数据库备份 public function bak(){ $type=input("tp"); $name=input("name"); $sql=new \org\baksql(\think\config::get("database")); switch ($type) { case "backup": //备份 $info = $sql->backup(); $this->success("$info",'index/backup/bak'); break; case "dowonload": //下载 $info = $sql->downloadfile($name); $this->success("$info",'index/backup/bak'); break; case "restore": //还原 $info = $sql->restore($name); $this->success("$info",'index/backup/bak'); break; case "del": //删除 $info = $sql->delfilename($name); $this->success("$info",'index/backup/bak'); break; default: //获取备份文件列表 return $this->fetch("bak",["list"=>$sql->get_filelist()]); } } }
view
<!--我用的是 h+ 的模板,样式可以自己调--> <body class="gray-bg"> <div class="wrapper wrapper-content animated fadeinright"> <div class="row"> <div class="col-sm-12"> <div class="ibox float-e-margins"> <div class="ibox-title"> <h5>数据库备份</h5> <div class="ibox-tools"> <a class="collapse-link"> <i class="fa fa-chevron-up"></i> </a> </div> </div> <div class="ibox-content"> <div class=""> <a class="btn btn-primary " href="{:url('bak',['tp'=>'backup'])}" rel="external nofollow" οnclick="return confirm('备份数据的时间较长,确定要备份所有数据吗?')"><i class="glyphicon glyphicon-plus"></i> <span class="bold">添加备份</span></a> </div> <table class="table table-striped table-bordered table-hover datatables-example" style="text-align: center"> <thead> <tr> <td>序号</td> <td>备份名称</td> <td>备份时间</td> <td>备份大小</td> <td>操作</td> </tr> </thead> <tbody> {volist name="list" id="vo"} <tr class="gradex" > <td>{$key+1}</td> <td>{$vo.name}</td> <td>{$vo.time}</td> <td>{$vo.size}</td> <td width="25%"> <a href="{:url('bak',['tp'=>'dowonload','name'=>$vo.name])}" rel="external nofollow" class="btn btn-success "><i class="glyphicon glyphicon-download-alt"></i> <span class="bold">下载</span></a> <a href="{:url('bak',['tp'=>'restore','name'=>$vo.name])}" rel="external nofollow" class="btn btn-info " οnclick="return confirm('备份还原后仅会显示当前备份的数据库的信息,您确定还原备份吗 ?')"><i class="glyphicon glyphicon-repeat"></i> <span class="bold">还原</span></a> <a href="{:url('bak',['tp'=>'del','name'=>$vo.name])}" rel="external nofollow" class="btn btn-warning" οnclick="return confirm('数据库备份一旦删除不可找回,您确定操作吗?')"><i class="fa fa-warning"></i> <span class="bold">删除</span></a> </td> </tr> {/volist} </tbody> </table> </div> </div> </div> </div> </div> </body>
5、refresh the web page!!!over,over,over