Database.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\controller\admin\system\safety;
  3. use ln\exceptions\UploadFailException;
  4. use ln\services\MysqlBackupService;
  5. use think\App;
  6. use ln\basic\BaseController;
  7. use think\facade\Db;
  8. use think\facade\Env;
  9. class Database extends BaseController
  10. {
  11. protected $service;
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $config = array(
  16. 'level' => 5,//数据库备份卷大小
  17. 'compress' => 1,//数据库备份文件是否启用压缩 0不压缩 1 压缩
  18. );
  19. $this->service = new MysqlBackupService($config);
  20. }
  21. /**
  22. * @Author:Qinii
  23. * @Date: 2020/5/21
  24. * @return mixed
  25. */
  26. public function lst()
  27. {
  28. return app('json')->success( $this->service->dataList());
  29. }
  30. /**
  31. * @Author:Qinii
  32. * @Date: 2020/5/21
  33. * @return mixed
  34. */
  35. public function fileList()
  36. {
  37. $files = $this->service->fileList();
  38. $data = [];
  39. foreach ($files as $key => $t) {
  40. $data[] = [
  41. 'filename' => $t['filename'],
  42. 'part' => $t['part'],
  43. 'size' => $t['size'] . 'B',
  44. 'compress' => $t['compress'],
  45. 'backtime' => $key,
  46. 'time' => $t['time'],
  47. ];
  48. }
  49. // krsort($data);//根据时间降序
  50. return app('json')->success($data);
  51. }
  52. /**
  53. * @Author:Qinii
  54. * @Date: 2020/5/21
  55. * @param $name
  56. * @return mixed
  57. */
  58. public function detail($name)
  59. {
  60. $database = Env::get("database.database");
  61. $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 . "'");
  62. return app('json')->success($result);
  63. }
  64. /**
  65. * @Author:Qinii
  66. * @Date: 2020/5/21
  67. * @param $name
  68. * @return mixed
  69. */
  70. public function backups($name)
  71. {
  72. $data = [];
  73. if(is_array($name)){
  74. foreach($name as $item){
  75. if(!$this->detail($item))
  76. return app('json')->fail('不存在的表名');
  77. $res = $this->service->backup($item,0);
  78. if ($res == false && $res != 0) {
  79. $data .= $item . '|';
  80. }
  81. }
  82. }
  83. if($data) return app('json')->fail('备份失败' . $data);
  84. return app('json')->success('备份成功');
  85. }
  86. /**
  87. * @Author:Qinii
  88. * @Date: 2020/5/21
  89. * @param $name
  90. * @return mixed
  91. */
  92. public function optimize($name)
  93. {
  94. $this->service->optimize($name);
  95. return app('json')->success('优化成功');
  96. }
  97. /**
  98. * @Author:Qinii
  99. * @Date: 2020/5/21
  100. * @param $name
  101. * @return mixed
  102. */
  103. public function repair($name)
  104. {
  105. foreach ($name as $item){
  106. $this->service->repair($item);
  107. }
  108. return app('json')->success('修复成功');
  109. }
  110. /**
  111. * @Author:Qinii
  112. * @Date: 2020/5/25
  113. * @return \think\response\File
  114. */
  115. public function downloadFile()
  116. {
  117. try {
  118. $time = intval($this->request->param('feilname'));
  119. $file =$this->service->getFile('time', $time);
  120. $fileName = $file[0];
  121. return download($fileName,$time);
  122. }catch (UploadFailException $e){
  123. return app('json')->fail('下载失败');
  124. }
  125. }
  126. public function deleteFile()
  127. {
  128. $feilname = intval($this->request->param('feilname'));
  129. $files = $this->service->delFile($feilname);
  130. return app('json')->success('删除成功');
  131. }
  132. }