123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace app\controller\admin\system\safety;
- use ln\exceptions\UploadFailException;
- use ln\services\MysqlBackupService;
- use think\App;
- use ln\basic\BaseController;
- use think\facade\Db;
- use think\facade\Env;
- class Database extends BaseController
- {
- protected $service;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $config = array(
- 'level' => 5,//数据库备份卷大小
- 'compress' => 1,//数据库备份文件是否启用压缩 0不压缩 1 压缩
- );
- $this->service = new MysqlBackupService($config);
- }
- /**
- * @Author:Qinii
- * @Date: 2020/5/21
- * @return mixed
- */
- public function lst()
- {
- return app('json')->success( $this->service->dataList());
- }
- /**
- * @Author:Qinii
- * @Date: 2020/5/21
- * @return mixed
- */
- public function fileList()
- {
- $files = $this->service->fileList();
- $data = [];
- foreach ($files as $key => $t) {
- $data[] = [
- 'filename' => $t['filename'],
- 'part' => $t['part'],
- 'size' => $t['size'] . 'B',
- 'compress' => $t['compress'],
- 'backtime' => $key,
- 'time' => $t['time'],
- ];
- }
- // krsort($data);//根据时间降序
- return app('json')->success($data);
- }
- /**
- * @Author:Qinii
- * @Date: 2020/5/21
- * @param $name
- * @return mixed
- */
- public function detail($name)
- {
- $database = Env::get("database.database");
- $result = Db::query("select COLUMN_NAME,COLUMN_TYPE,COLUMN_DEFAULT,IS_NULLABLE,EXTRA,COLUMN_COMMENT from information_schema.columns where table_name = '" . $name . "' and table_schema = '" . $database . "'");
- return app('json')->success($result);
- }
- /**
- * @Author:Qinii
- * @Date: 2020/5/21
- * @param $name
- * @return mixed
- */
- public function backups($name)
- {
- $data = [];
- if(is_array($name)){
- foreach($name as $item){
- if(!$this->detail($item))
- return app('json')->fail('不存在的表名');
- $res = $this->service->backup($item,0);
- if ($res == false && $res != 0) {
- $data .= $item . '|';
- }
- }
- }
- if($data) return app('json')->fail('备份失败' . $data);
- return app('json')->success('备份成功');
- }
- /**
- * @Author:Qinii
- * @Date: 2020/5/21
- * @param $name
- * @return mixed
- */
- public function optimize($name)
- {
- $this->service->optimize($name);
- return app('json')->success('优化成功');
- }
- /**
- * @Author:Qinii
- * @Date: 2020/5/21
- * @param $name
- * @return mixed
- */
- public function repair($name)
- {
- foreach ($name as $item){
- $this->service->repair($item);
- }
- return app('json')->success('修复成功');
- }
- /**
- * @Author:Qinii
- * @Date: 2020/5/25
- * @return \think\response\File
- */
- public function downloadFile()
- {
- try {
- $time = intval($this->request->param('feilname'));
- $file =$this->service->getFile('time', $time);
- $fileName = $file[0];
- return download($fileName,$time);
- }catch (UploadFailException $e){
- return app('json')->fail('下载失败');
- }
- }
- public function deleteFile()
- {
- $feilname = intval($this->request->param('feilname'));
- $files = $this->service->delFile($feilname);
- return app('json')->success('删除成功');
- }
- }
|