From a77f8c6eccb929a67ee580b36dfcf5b80198281a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Fri, 13 Jan 2023 10:59:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=B9=E6=80=A7=EF=BC=88=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=EF=BC=89=EF=BC=9A=E4=BC=98=E5=8C=96=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=92=8C=E5=A4=87=E4=BB=BD=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++++ app/Console/Commands/Panel.php | 41 ++++++++++++++++++++++++---------- config/panel.php | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b415c2c..1f2149f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ 所有重要的更改都将在此文件中记录。 +## [20230108] - 常规更新 + +- 优化更新流程 +- 优化备份流程 + ## [20230108] - 常规更新 - 提升面板安全性 diff --git a/app/Console/Commands/Panel.php b/app/Console/Commands/Panel.php index 6f74a787..afe33546 100644 --- a/app/Console/Commands/Panel.php +++ b/app/Console/Commands/Panel.php @@ -10,6 +10,7 @@ use App\Models\Website; use Illuminate\Console\Command; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; +use Symfony\Component\Console\Command\Command as CommandAlias; class Panel extends Command { @@ -32,7 +33,7 @@ class Panel extends Command * * @return int */ - public function handle() + public function handle(): int { // 检测是否以root用户运行 if (trim(shell_exec('whoami')) != 'root') { @@ -99,7 +100,7 @@ class Panel extends Command $this->info('panel deleteSetting {name} 删除面板设置数据'); break; } - return Command::SUCCESS; + return CommandAlias::SUCCESS; } /** @@ -146,6 +147,7 @@ class Panel extends Command } $this->info('正在备份插件...'); shell_exec('rm -rf /tmp/plugins'); + shell_exec('rm -rf /tmp/database.sqlite'); shell_exec('mkdir /tmp/plugins'); shell_exec('\cp -r /www/panel/plugins/* /tmp/plugins'); // 检查备份是否成功 @@ -164,6 +166,7 @@ class Panel extends Command return; } $this->info('正在恢复数据库...'); + shell_exec('rm -rf /www/panel/database/database.sqlite'); shell_exec('\cp /tmp/database.sqlite /www/panel/database/database.sqlite'); // 检查恢复是否成功 if (!file_exists('/www/panel/database/database.sqlite')) { @@ -370,7 +373,8 @@ class Panel extends Command } elseif ($type == 'mysql') { // 备份MySQL数据库 $password = Setting::query()->where('name', 'mysql_root_password')->value('value'); - $backupFile = $path.'/'.$name.'_'.date('YmdHis').'.sql'; + $backupFile = $path.'/'.$name.'_'.date('YmdHis').'.sql.zip'; + $tempFile = '/tmp/'.$name.'_'.date('YmdHis').'.sql'; // 判断数据库是否存在 $name = escapeshellarg($name); $check = shell_exec("mysql -u root -p".$password." -e 'use ".$name."' 2>&1"); @@ -378,15 +382,22 @@ class Panel extends Command $this->error('数据库不存在'); return; } - shell_exec("mysqldump -u root -p".$password." ".$name." > ".$backupFile." 2>&1"); + shell_exec("mysqldump -u root -p".$password." ".$name." > ".$tempFile." 2>&1"); // zip压缩 - shell_exec('zip -r '.$backupFile.'.zip '.escapeshellarg($backupFile).' 2>&1'); - // 删除sql文件 - unlink($backupFile); + shell_exec('zip -r '.$tempFile.'.zip '.escapeshellarg($tempFile).' 2>&1'); + // 移动文件 + if (file_exists($backupFile)) { + $this->error('检测到备份已存在,已跳过此次备份'); + return; + } + rename($tempFile.'.zip', $backupFile); + // 删除临时文件 + unlink($tempFile); $this->info('成功'); } elseif ($type == 'postgresql') { // 备份PostgreSQL数据库 - $backupFile = $path.'/'.$name.'_'.date('YmdHis').'.sql'; + $backupFile = $path.'/'.$name.'_'.date('YmdHis').'.sql.zip'; + $tempFile = '/tmp/'.$name.'_'.date('YmdHis').'.sql'; // 判断数据库是否存在 $check = shell_exec('su - postgres -c "psql -l" 2>&1'); if (!str_contains($check, $name)) { @@ -394,11 +405,17 @@ class Panel extends Command return; } $name = escapeshellarg($name); - shell_exec('su - postgres -c "pg_dump '.$name.'" > '.$backupFile.' 2>&1'); + shell_exec('su - postgres -c "pg_dump '.$name.'" > '.$tempFile.' 2>&1'); // zip压缩 - shell_exec('zip -r '.$backupFile.'.zip '.escapeshellarg($backupFile).' 2>&1'); - // 删除sql文件 - unlink($backupFile); + shell_exec('zip -r '.$tempFile.'.zip '.escapeshellarg($tempFile).' 2>&1'); + // 移动文件 + if (file_exists($backupFile)) { + $this->error('检测到备份已存在,已跳过此次备份'); + return; + } + rename($tempFile.'.zip', $backupFile); + // 删除临时文件 + unlink($tempFile); $this->info('成功'); } else { $this->error('参数错误'); diff --git a/config/panel.php b/config/panel.php index 45d38baf..94af547c 100644 --- a/config/panel.php +++ b/config/panel.php @@ -1,6 +1,6 @@ '耗子Linux面板', - 'version' => '20230108', + 'version' => '20230113', 'plugin_dir' => '/www/panel/plugins', ]; \ No newline at end of file