SystemDatabackupServices.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\services\BaseServices;
  13. use crmeb\services\MysqlBackupService;
  14. use think\facade\Db;
  15. use think\facade\Env;
  16. /**
  17. * 数据库备份
  18. * Class SystemDatabackupServices
  19. * @package app\services\system
  20. */
  21. class SystemDatabackupServices extends BaseServices
  22. {
  23. /**
  24. *
  25. * @var MysqlBackupService
  26. */
  27. protected $dbBackup;
  28. /**
  29. * 构造方法
  30. * SystemDatabackupServices constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->dbBackup = app()->make(MysqlBackupService::class, [[
  35. //数据库备份卷大小
  36. 'compress' => 1,
  37. //数据库备份文件是否启用压缩 0不压缩 1 压缩
  38. 'level' => 5,
  39. ]]);
  40. }
  41. /**
  42. * 获取数据库列表
  43. * @return array
  44. * @throws \think\db\exception\BindParamException
  45. */
  46. public function getDataList()
  47. {
  48. $list = $this->dbBackup->dataList();
  49. $count = count($list);
  50. return compact('list', 'count');
  51. }
  52. /**
  53. * 获取表详情
  54. * @param string $tablename
  55. * @return array
  56. */
  57. public function getRead(string $tablename)
  58. {
  59. $database = Env::get("database.database");
  60. $list = Db::query("select * from information_schema.columns where table_name = '" . $tablename . "' and table_schema = '" . $database . "'");
  61. $count = count($list);
  62. foreach ($list as $key => $f) {
  63. $list[$key]['EXTRA'] = ($f['EXTRA'] == 'auto_increment' ? '是' : ' ');
  64. }
  65. return compact('list', 'count');
  66. }
  67. /**
  68. * @return MysqlBackupService
  69. */
  70. public function getDbBackup()
  71. {
  72. return $this->dbBackup;
  73. }
  74. /**
  75. * 备份表
  76. * @param string $tables
  77. * @return string
  78. * @throws \think\db\exception\BindParamException
  79. */
  80. public function backup(string $tables)
  81. {
  82. $tables = explode(',', $tables);
  83. $data = '';
  84. ini_set ("memory_limit","-1");
  85. foreach ($tables as $t) {
  86. $res = $this->dbBackup->backup($t, 0);
  87. if ($res == false && $res != 0) {
  88. $data .= $t . '|';
  89. }
  90. }
  91. return $data;
  92. }
  93. /**
  94. * 获取备份列表
  95. * @return array
  96. */
  97. public function getBackup()
  98. {
  99. $files = $this->dbBackup->fileList();
  100. $data = [];
  101. foreach ($files as $key => $t) {
  102. $data[$key]['filename'] = $t['filename'];
  103. $data[$key]['part'] = $t['part'];
  104. $data[$key]['size'] = $t['size'] . 'B';
  105. $data[$key]['compress'] = $t['compress'];
  106. $data[$key]['backtime'] = $key;
  107. $data[$key]['time'] = $t['time'];
  108. }
  109. krsort($data);//根据时间降序
  110. return ['count' => count($data), 'list' => array_values($data)];
  111. }
  112. }