You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemDatabackupServices.php 2.8KB

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