From 4b9abaf9df01097c22b6dd6dfd61e46a76d8848a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Thu, 9 Feb 2023 17:24:43 +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=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=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 | 4 ++++ app/Console/Commands/Panel.php | 25 +++++++++++++++---------- config/panel.php | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0ddda42..71b68d4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ## [20230130] - 常规更新 +- 优化数据库备份流程 + +## [20230130] - 常规更新 + - 代码格式化 - 更新许可证 - 修复更新备份逻辑错误 diff --git a/app/Console/Commands/Panel.php b/app/Console/Commands/Panel.php index 827302d8..dbc371a5 100644 --- a/app/Console/Commands/Panel.php +++ b/app/Console/Commands/Panel.php @@ -377,7 +377,7 @@ class Panel extends Command // 备份MySQL数据库 $password = Setting::query()->where('name', 'mysql_root_password')->value('value'); $backupFile = $path.'/'.$name.'_'.date('YmdHis').'.sql.zip'; - $tempFile = '/tmp/'.$name.'_'.date('YmdHis').'.sql'; + $tempFile = $name.'_'.date('YmdHis').'.sql'; // 判断数据库是否存在 $name = escapeshellarg($name); $check = shell_exec("mysql -u root -p".$password." -e 'use ".$name."' 2>&1"); @@ -385,22 +385,26 @@ class Panel extends Command $this->error('数据库不存在'); return; } - shell_exec("mysqldump -u root -p".$password." ".$name." > ".$tempFile." 2>&1"); + // 傻逼MySQL,瞎jb输出警告 + //shell_exec("mysqldump -u root -p".$password." ".$name." 2> /dev/null > /tmp/".$tempFile); + putenv('MYSQL_PWD='.$password); + shell_exec("mysqldump -u root ".$name." > /tmp/".$tempFile); // zip压缩 - shell_exec('zip -r '.$tempFile.'.zip '.escapeshellarg($tempFile).' 2>&1'); + shell_exec('cd /tmp && zip -r '.$tempFile.'.zip '.escapeshellarg($tempFile).' 2>&1'); // 移动文件 if (file_exists($backupFile)) { $this->error('检测到备份已存在,已跳过此次备份'); + unlink('/tmp/'.$tempFile); return; } - rename($tempFile.'.zip', $backupFile); + rename('/tmp/'.$tempFile.'.zip', $backupFile); // 删除临时文件 - unlink($tempFile); + unlink('/tmp/'.$tempFile); $this->info('成功'); } elseif ($type == 'postgresql') { // 备份PostgreSQL数据库 $backupFile = $path.'/'.$name.'_'.date('YmdHis').'.sql.zip'; - $tempFile = '/tmp/'.$name.'_'.date('YmdHis').'.sql'; + $tempFile = $name.'_'.date('YmdHis').'.sql'; // 判断数据库是否存在 $check = shell_exec('su - postgres -c "psql -l" 2>&1'); if (!str_contains($check, $name)) { @@ -408,17 +412,18 @@ class Panel extends Command return; } $name = escapeshellarg($name); - shell_exec('su - postgres -c "pg_dump '.$name.'" > '.$tempFile.' 2>&1'); + shell_exec('su - postgres -c "pg_dump '.$name.'" > /tmp/'.$tempFile.' 2>&1'); // zip压缩 - shell_exec('zip -r '.$tempFile.'.zip '.escapeshellarg($tempFile).' 2>&1'); + shell_exec('cd /tmp && zip -r '.$tempFile.'.zip '.escapeshellarg($tempFile).' 2>&1'); // 移动文件 if (file_exists($backupFile)) { $this->error('检测到备份已存在,已跳过此次备份'); + unlink('/tmp/'.$tempFile); return; } - rename($tempFile.'.zip', $backupFile); + rename('/tmp/'.$tempFile.'.zip', $backupFile); // 删除临时文件 - unlink($tempFile); + unlink('/tmp/'.$tempFile); $this->info('成功'); } else { $this->error('参数错误'); diff --git a/config/panel.php b/config/panel.php index ff4b4d19..137429a4 100644 --- a/config/panel.php +++ b/config/panel.php @@ -1,6 +1,6 @@ '耗子Linux面板', - 'version' => '20230130', + 'version' => '20230209', 'plugin_dir' => '/www/panel/plugins', ]; \ No newline at end of file