Database.php 3.7 KB

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