BackupService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\upgrade;
  12. use app\dict\addon\AddonDict;
  13. use core\util\DbBackup;
  14. /**
  15. * 框架及插件升级备份
  16. * @package app\service\core\upgrade
  17. */
  18. class BackupService extends UpgradeService
  19. {
  20. /**
  21. * 备份代码
  22. * @return void
  23. */
  24. public function backupCode() {
  25. $backup_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
  26. // 创建目录
  27. dir_mkdir($backup_dir);
  28. // 备份admin
  29. dir_copy($this->root_path . 'admin', $backup_dir . 'admin', exclude_dirs:[ '.vscode', 'node_modules', 'dist']);
  30. // 备份uni-app
  31. dir_copy($this->root_path . 'uni-app', $backup_dir . 'uni-app', exclude_dirs:['node_modules', 'dist']);
  32. // 备份web
  33. dir_copy($this->root_path . 'web', $backup_dir . 'web', exclude_dirs:['node_modules', '.nuxt', '.output']);
  34. // 备份niucloud
  35. $niucloud_dir = $backup_dir . 'niucloud' . DIRECTORY_SEPARATOR;
  36. if ($this->upgrade_task['upgrade']['app_key'] == AddonDict::FRAMEWORK_KEY) {
  37. dir_copy($this->root_path . 'niucloud', $niucloud_dir, exclude_dirs:['addon', 'config', 'public', 'vendor', 'runtime']);
  38. // 备份版本文件
  39. $version_file = $this->root_path . 'niucloud' .DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'version.php';
  40. $to_version_file = $niucloud_dir . 'config' . DIRECTORY_SEPARATOR . 'version.php';
  41. file_copy($version_file, $to_version_file);
  42. } else {
  43. $addon = $this->upgrade_task['upgrade']['app_key'];
  44. $addon_dir = $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR . 'addon' . DIRECTORY_SEPARATOR . $addon;
  45. $to_addon_dir = $niucloud_dir . 'addon' . DIRECTORY_SEPARATOR . $addon;
  46. dir_copy($addon_dir, $to_addon_dir);
  47. }
  48. // 备份前端文件
  49. if (is_dir(public_path() . 'admin')) {
  50. dir_copy(public_path() . 'admin', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'admin');
  51. }
  52. if (is_dir(public_path() . 'wap')) {
  53. dir_copy(public_path() . 'wap', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'wap');
  54. }
  55. if (is_dir(public_path() . 'web')) {
  56. dir_copy(public_path() . 'web', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'web');
  57. }
  58. return true;
  59. }
  60. /**
  61. * 备份数据库
  62. * @return void
  63. */
  64. public function backupSql() {
  65. $backup_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR;
  66. // 创建目录
  67. dir_mkdir($backup_dir);
  68. $db = new DbBackup([
  69. 'path' => $backup_dir,//数据库备份路径
  70. 'part' => 1048576,//数据库备份卷大小
  71. 'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
  72. 'level' => 9 //数据库备份文件压缩级别 1普通 4 一般 9最高
  73. ]);
  74. $prefix = config('database.connections.'.config('database.default'))['prefix'];
  75. if ($this->upgrade_task['upgrade']['app_key'] == AddonDict::FRAMEWORK_KEY) {
  76. // 不需要备份的表
  77. $noot_need_backup = ["{$prefix}sys_user_log", "{$prefix}jobs", "{$prefix}jobs_failed"];
  78. $tables = array_diff(array_column($db->dataList(), 'name'), $noot_need_backup);
  79. } else {
  80. $tables = [];
  81. $table_prefix = "{$prefix}{$this->upgrade_task['upgrade']['app_key']}";
  82. foreach ($db->dataList() as $table) {
  83. if (strpos($table['name'], $table_prefix) === 0) {
  84. $tables[] = $table['name'];
  85. }
  86. }
  87. }
  88. foreach ($tables as $table) {
  89. $db->setFile()->backup($table);
  90. }
  91. return true;
  92. }
  93. }