BackupService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 app\service\admin\generator\GenerateService;
  14. use core\util\DbBackup;
  15. /**
  16. * 框架及插件升级备份
  17. * @package app\service\core\upgrade
  18. */
  19. class BackupService extends UpgradeService
  20. {
  21. /**
  22. * 备份代码
  23. * @return void
  24. */
  25. public function backupCode()
  26. {
  27. $backup_dir = $this->upgrade_dir . $this->upgrade_task[ 'key' ] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
  28. // 创建目录
  29. dir_mkdir($backup_dir);
  30. // 备份admin
  31. dir_copy($this->root_path . 'admin', $backup_dir . 'admin', exclude_dirs:[ '.vscode', 'node_modules', 'dist' ]);
  32. // 备份uni-app
  33. dir_copy($this->root_path . 'uni-app', $backup_dir . 'uni-app', exclude_dirs:[ 'node_modules', 'dist' ]);
  34. // 备份web
  35. dir_copy($this->root_path . 'web', $backup_dir . 'web', exclude_dirs:[ 'node_modules', '.nuxt', '.output' ]);
  36. // 备份niucloud
  37. $niucloud_dir = $backup_dir . 'niucloud' . DIRECTORY_SEPARATOR;
  38. if ($this->upgrade_task[ 'upgrade' ][ 'app_key' ] == AddonDict::FRAMEWORK_KEY) {
  39. dir_copy($this->root_path . 'niucloud', $niucloud_dir, exclude_dirs:[ 'addon', 'config', 'public', 'vendor', 'runtime' ]);
  40. // 备份版本文件
  41. $version_file = $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'version.php';
  42. $to_version_file = $niucloud_dir . 'config' . DIRECTORY_SEPARATOR . 'version.php';
  43. file_copy($version_file, $to_version_file);
  44. } else {
  45. $addon = $this->upgrade_task[ 'upgrade' ][ 'app_key' ];
  46. $addon_dir = $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR . 'addon' . DIRECTORY_SEPARATOR . $addon;
  47. $to_addon_dir = $niucloud_dir . 'addon' . DIRECTORY_SEPARATOR . $addon;
  48. dir_copy($addon_dir, $to_addon_dir);
  49. }
  50. // 备份前端文件
  51. if (is_dir(public_path() . 'admin')) {
  52. dir_copy(public_path() . 'admin', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'admin');
  53. }
  54. if (is_dir(public_path() . 'wap')) {
  55. dir_copy(public_path() . 'wap', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'wap');
  56. }
  57. if (is_dir(public_path() . 'web')) {
  58. dir_copy(public_path() . 'web', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'web');
  59. }
  60. return true;
  61. }
  62. /**
  63. * 备份数据库
  64. * @return void
  65. */
  66. public function backupSql()
  67. {
  68. $backup_dir = $this->upgrade_dir . $this->upgrade_task[ 'key' ] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR;
  69. // 创建目录
  70. dir_mkdir($backup_dir);
  71. $db = new DbBackup([
  72. 'path' => $backup_dir,//数据库备份路径
  73. 'part' => 1048576,//数据库备份卷大小
  74. 'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
  75. 'level' => 9 //数据库备份文件压缩级别 1普通 4 一般 9最高
  76. ]);
  77. $tables = [];
  78. $prefix = config('database.connections.' . config('database.default'))[ 'prefix' ];
  79. if ($this->upgrade_task[ 'upgrade' ][ 'app_key' ] == AddonDict::FRAMEWORK_KEY) {
  80. // 不需要备份的表
  81. $noot_need_backup = [ "{$prefix}sys_schedule_log", "{$prefix}sys_user_log", "{$prefix}jobs", "{$prefix}jobs_failed" ];
  82. $sys_models = ( new GenerateService() )->getModels([ 'addon' => 'system' ]);
  83. foreach ($sys_models as $model) {
  84. $name = "\\$model";
  85. $class = new $name();
  86. if (!in_array($class->getTable(), $noot_need_backup)) {
  87. $tables[] = $class->getTable();
  88. }
  89. }
  90. } else {
  91. $addon_models = ( new GenerateService() )->getModels([ 'addon' => $this->upgrade_task[ 'upgrade' ][ 'app_key' ] ]);
  92. foreach ($addon_models as $model) {
  93. try {
  94. // 不需要备份的表
  95. $noot_need_backup = [ "{$prefix}shop_stat", "{$prefix}shop_goods_stat", "{$prefix}shop_goods_browse" ];
  96. $name = "\\$model";
  97. $class = new $name();
  98. if (!in_array($class->getTable(), $noot_need_backup)) {
  99. $tables[] = $class->getTable();
  100. }
  101. } catch (\Exception $e) {
  102. }
  103. }
  104. }
  105. foreach ($tables as $table) {
  106. $db->setFile()->backup($table);
  107. }
  108. return true;
  109. }
  110. }