智慧教务系统
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.
 
 
 
 
 
 

122 lines
5.2 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\service\admin\upgrade;
use app\dict\addon\AddonDict;
use app\service\admin\generator\GenerateService;
use core\util\DbBackup;
/**
* 框架及插件升级备份
* @package app\service\core\upgrade
*/
class BackupService extends UpgradeService
{
/**
* 备份代码
* @return void
*/
public function backupCode()
{
$backup_dir = $this->upgrade_dir . $this->upgrade_task[ 'key' ] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
// 创建目录
dir_mkdir($backup_dir);
// 备份admin
dir_copy($this->root_path . 'admin', $backup_dir . 'admin', exclude_dirs:[ '.vscode', 'node_modules', 'dist' ]);
// 备份uni-app
dir_copy($this->root_path . 'uni-app', $backup_dir . 'uni-app', exclude_dirs:[ 'node_modules', 'dist' ]);
// 备份web
dir_copy($this->root_path . 'web', $backup_dir . 'web', exclude_dirs:[ 'node_modules', '.nuxt', '.output' ]);
// 备份niucloud
$niucloud_dir = $backup_dir . 'niucloud' . DIRECTORY_SEPARATOR;
if ($this->upgrade_task[ 'upgrade' ][ 'app_key' ] == AddonDict::FRAMEWORK_KEY) {
dir_copy($this->root_path . 'niucloud', $niucloud_dir, exclude_dirs:[ 'addon', 'config', 'public', 'vendor', 'runtime' ]);
// 备份版本文件
$version_file = $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'version.php';
$to_version_file = $niucloud_dir . 'config' . DIRECTORY_SEPARATOR . 'version.php';
file_copy($version_file, $to_version_file);
} else {
$addon = $this->upgrade_task[ 'upgrade' ][ 'app_key' ];
$addon_dir = $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR . 'addon' . DIRECTORY_SEPARATOR . $addon;
$to_addon_dir = $niucloud_dir . 'addon' . DIRECTORY_SEPARATOR . $addon;
dir_copy($addon_dir, $to_addon_dir);
}
// 备份前端文件
if (is_dir(public_path() . 'admin')) {
dir_copy(public_path() . 'admin', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'admin');
}
if (is_dir(public_path() . 'wap')) {
dir_copy(public_path() . 'wap', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'wap');
}
if (is_dir(public_path() . 'web')) {
dir_copy(public_path() . 'web', $niucloud_dir . 'public' . DIRECTORY_SEPARATOR . 'web');
}
return true;
}
/**
* 备份数据库
* @return void
*/
public function backupSql()
{
$backup_dir = $this->upgrade_dir . $this->upgrade_task[ 'key' ] . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'sql' . DIRECTORY_SEPARATOR;
// 创建目录
dir_mkdir($backup_dir);
$db = new DbBackup([
'path' => $backup_dir,//数据库备份路径
'part' => 1048576,//数据库备份卷大小
'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
'level' => 9 //数据库备份文件压缩级别 1普通 4 一般 9最高
]);
$tables = [];
$prefix = config('database.connections.' . config('database.default'))[ 'prefix' ];
if ($this->upgrade_task[ 'upgrade' ][ 'app_key' ] == AddonDict::FRAMEWORK_KEY) {
// 不需要备份的表
$noot_need_backup = [ "{$prefix}sys_schedule_log", "{$prefix}sys_user_log", "{$prefix}jobs", "{$prefix}jobs_failed" ];
$sys_models = ( new GenerateService() )->getModels([ 'addon' => 'system' ]);
foreach ($sys_models as $model) {
$name = "\\$model";
$class = new $name();
if (!in_array($class->getTable(), $noot_need_backup)) {
$tables[] = $class->getTable();
}
}
} else {
$addon_models = ( new GenerateService() )->getModels([ 'addon' => $this->upgrade_task[ 'upgrade' ][ 'app_key' ] ]);
foreach ($addon_models as $model) {
try {
// 不需要备份的表
$noot_need_backup = [ "{$prefix}shop_stat", "{$prefix}shop_goods_stat", "{$prefix}shop_goods_browse" ];
$name = "\\$model";
$class = new $name();
if (!in_array($class->getTable(), $noot_need_backup)) {
$tables[] = $class->getTable();
}
} catch (\Exception $e) {
}
}
}
foreach ($tables as $table) {
$db->setFile()->backup($table);
}
return true;
}
}