SystemDatabackup.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\v1\system;
  12. use think\exception\ValidateException;
  13. use think\facade\App;
  14. use think\facade\Session;
  15. use app\controller\admin\AuthController;
  16. use app\services\system\SystemDatabackupServices;
  17. /**
  18. * 数据备份
  19. * Class SystemDatabackup
  20. * @package app\controller\admin\v1\system
  21. *
  22. */
  23. class SystemDatabackup extends AuthController
  24. {
  25. /**
  26. * 构造方法
  27. * SystemDatabackup constructor.
  28. * @param App $app
  29. * @param SystemDatabackupServices $services
  30. */
  31. public function __construct(App $app, SystemDatabackupServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. //生产模式下不允许清除数据
  36. if (!env('APP_DEBUG', false)) {
  37. throw new ValidateException('生产模式下,禁止操作');
  38. }
  39. }
  40. /**
  41. * 获取数据库表
  42. */
  43. public function index()
  44. {
  45. return $this->success($this->services->getDataList());
  46. }
  47. /**
  48. * 查看表结构 详情
  49. */
  50. public function read()
  51. {
  52. $tablename = request()->param('tablename', '', 'htmlspecialchars');
  53. return $this->success($this->services->getRead($tablename));
  54. }
  55. /**
  56. * 优化表
  57. */
  58. public function optimize()
  59. {
  60. $tables = $this->request->param('tables', '', 'htmlspecialchars');
  61. $res = $this->services->getDbBackup()->optimize($tables);
  62. return $this->success($res ? '优化成功' : '优化失败');
  63. }
  64. /**
  65. * 修复表
  66. */
  67. public function repair()
  68. {
  69. $tables = $this->request->param('tables', '', 'htmlspecialchars');
  70. $res = $this->services->getDbBackup()->repair($tables);
  71. return $this->success($res ? '修复成功' : '修复失败');
  72. }
  73. /**
  74. * 备份表
  75. */
  76. public function backup()
  77. {
  78. $tables = $this->request->param('tables', '', 'htmlspecialchars');
  79. $data = $this->services->backup($tables);
  80. return $this->success($data ? '备份失败' . $data : '备份成功');
  81. }
  82. /**
  83. * 获取备份记录表
  84. */
  85. public function fileList()
  86. {
  87. return $this->success($this->services->getBackup());
  88. }
  89. /**
  90. * 删除备份记录表
  91. */
  92. public function delFile()
  93. {
  94. $filename = intval(request()->post('filename'));
  95. $files = $this->services->getDbBackup()->delFile($filename);
  96. return $this->success('删除成功');
  97. }
  98. /**
  99. * 导入备份记录表
  100. */
  101. public function import()
  102. {
  103. [$part, $start, $time] = $this->request->postMore([
  104. [['part', 'd'], 0],
  105. [['start', 'd'], 0],
  106. [['time', 'd'], 0],
  107. ], true);
  108. $db = $this->services->getDbBackup();
  109. if (is_numeric($time) && !$start) {
  110. $list = $db->getFile('timeverif', $time);
  111. if (is_array($list)) {
  112. session::set('backup_list', $list);
  113. return $this->success('初始化完成!', array('part' => 1, 'start' => 0));
  114. } else {
  115. return $this->fail('备份文件可能已经损坏,请检查!');
  116. }
  117. } else if (is_numeric($part) && is_numeric($start) && $part && $start) {
  118. $list = session::get('backup_list');
  119. $start = $db->setFile($list)->import($start);
  120. if (false === $start) {
  121. return $this->fail('还原数据出错!');
  122. } elseif (0 === $start) {
  123. if (isset($list[++$part])) {
  124. $data = array('part' => $part, 'start' => 0);
  125. return $this->success("正在还原...#{$part}", $data);
  126. } else {
  127. session::delete('backup_list');
  128. return $this->success('还原完成!');
  129. }
  130. } else {
  131. $data = array('part' => $part, 'start' => $start[0]);
  132. if ($start[1]) {
  133. $rate = floor(100 * ($start[0] / $start[1]));
  134. return $this->success("正在还原...#{$part}({$rate}%)", $data);
  135. } else {
  136. $data['gz'] = 1;
  137. return $this->success("正在还原...#{$part}", $data);
  138. }
  139. return $this->success("正在还原...#{$part}");
  140. }
  141. } else {
  142. return $this->fail('参数错误!');
  143. }
  144. }
  145. /**
  146. * 下载备份记录表
  147. */
  148. public function downloadFile()
  149. {
  150. $time = intval(request()->param('time'));
  151. return $this->success(['key' => $this->services->getDbBackup()->downloadFile($time, 0, true)]);
  152. }
  153. }