Database.php 4.3 KB

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