RestoreService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 core\util\DbBackup;
  13. /**
  14. * 框架及插件升级恢复备份
  15. * @package app\service\core\upgrade
  16. */
  17. class RestoreService extends UpgradeService
  18. {
  19. /**
  20. * 恢复代码备份
  21. * @return true
  22. */
  23. public function restoreCode() {
  24. $backup_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
  25. if (is_dir($backup_dir)) {
  26. // 删除前端文件
  27. if (is_dir(public_path() . 'admin')) del_target_dir(public_path() . 'admin', true);
  28. if (is_dir(public_path() . 'wap')) del_target_dir(public_path() . 'wap', true);
  29. if (is_dir(public_path() . 'web')) del_target_dir(public_path() . 'web', true);
  30. dir_copy($backup_dir, rtrim($this->root_path, DIRECTORY_SEPARATOR));
  31. }
  32. return true;
  33. }
  34. /**
  35. * 恢复数据库备份
  36. * @return true
  37. */
  38. public function restoreSql() {
  39. $backup_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR;
  40. if (is_dir($backup_dir)) {
  41. $db = new DbBackup([
  42. 'path' => $backup_dir //数据库备份路径
  43. ]);
  44. $file_list = $db->fileList();
  45. if (!empty($file_list)) {
  46. $db->setSqlMode();
  47. foreach ($file_list as $file) {
  48. $db->setFile($file)->import(0, $file['time']);
  49. }
  50. }
  51. }
  52. return true;
  53. }
  54. }