Database.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/2
  12. */
  13. namespace app\admin\controller;
  14. use app\common\controller\Backend;
  15. use lemo\helper\StringHelper;
  16. use think\facade\Request;
  17. use think\facade\View;
  18. use app\admin\controller\Backup;
  19. // 需要修改\tp5er\backup connect()
  20. //以及 $info['name'] = $file->getFilename();
  21. class Database extends Backend
  22. {
  23. protected $db = '';
  24. function initialize(){
  25. parent::initialize();
  26. $this->config=array(
  27. 'path' => './Data/',//数据库备份路径
  28. 'part' => 20971520,//数据库备份卷大小
  29. 'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
  30. 'level' => 9 //数据库备份文件压缩级别 1普通 4 一般 9最高
  31. );
  32. $this->db = new Backup($this->config);
  33. }
  34. public function index(){
  35. if(Request::isPost()){
  36. $list = $this->db->dataList();
  37. $total = 0;
  38. foreach ($list as $k => $v) {
  39. $list[$k]['size'] = StringHelper::formatBytes($v['data_length']);
  40. $total += $v['data_length'];
  41. }
  42. return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'total'=>StringHelper::formatBytes($total),'tableNum'=>count($list),'rel'=>1];
  43. }
  44. return View::fetch();
  45. }
  46. //优化
  47. public function optimize() {
  48. $tables = Request::param('tables');
  49. if (empty($tables)) {
  50. $this->success(lang('please choose table')) ;
  51. }
  52. if($this->db->optimize($tables)){
  53. $this->success(lang('optimize success')) ;
  54. }else{
  55. $this->error(lang('optimize fail'));
  56. }
  57. }
  58. //修复
  59. public function repair() {
  60. $tables = Request::param('tables');
  61. if (empty($tables)) {
  62. $this->error(lang('please choose table'));
  63. }
  64. if($this->db->repair($tables)){
  65. $this->success(lang('repair success')) ;
  66. }else{
  67. $this->error(lang('repair fail'));
  68. }
  69. }
  70. //备份
  71. public function backup(){
  72. $tables = Request::param('tables');
  73. if (!empty($tables)) {
  74. foreach ($tables as $table) {
  75. $this->db->setFile()->backup($table, 0);
  76. }
  77. $this->success(lang('backup success')) ;
  78. } else {
  79. $this->error(lang('please choose table')) ;
  80. }
  81. }
  82. //备份列表
  83. public function restore(){
  84. if(Request::isPost()){
  85. $list = $this->db->fileList();
  86. return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$list,'rel'=>1];
  87. }
  88. return View::fetch();
  89. }
  90. //执行还原数据库操作
  91. public function import($time) {
  92. $list = $this->db->getFile('timeverif',$time);
  93. $this->db->setFile($list)->import(1);
  94. $this->success('restore success') ;
  95. }
  96. //下载
  97. public function downFile($time) {
  98. $this->db->downloadFile($time);
  99. }
  100. //删除sql文件
  101. public function delSqlFiles() {
  102. $time = input('post.time');
  103. if($this->db->delFile($time)){
  104. $this->success(lang('delete success')) ;
  105. }else{
  106. $this->error(lang('delete fail')) ;
  107. }
  108. }
  109. }