From cc1dc14c8463f88aafac572a59b663b01e1ceb19 Mon Sep 17 00:00:00 2001 From: xboard Date: Sat, 18 Nov 2023 19:54:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=C2=B7=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E5=A4=87=E4=BB=BD=E5=B9=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=88=B0=E8=B0=B7=E6=AD=8C=E4=BA=91=E5=AD=98=E5=82=A8?= =?UTF-8?q?=C2=B7=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 ++ .gitignore | 1 + app/Console/Commands/BackupDatabase.php | 65 +++++++++++++++++++++++++ app/Console/Kernel.php | 2 + composer.json | 1 + config/cloud_storage.php | 10 ++++ config/logging.php | 6 +++ storage/backup/.gitignore | 2 + 8 files changed, 91 insertions(+) create mode 100644 app/Console/Commands/BackupDatabase.php create mode 100644 config/cloud_storage.php create mode 100644 storage/backup/.gitignore diff --git a/.env.example b/.env.example index 481cbfd..a5306a8 100755 --- a/.env.example +++ b/.env.example @@ -44,5 +44,9 @@ MAIL_FROM_NAME=null MAILGUN_DOMAIN= MAILGUN_SECRET= +# google cloud stoage +GOOGLE_CLOUD_KEY_FILE=config/googleCloudStorageKey.json +GOOGLE_CLOUD_STORAGE_BUCKET= + # 用于阻止重复安装 INSTALLED=false \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0f2199f..07258b2 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /node_modules /config/v2board.php +/config/googleCloudStorageKey.json /public/hot /public/storage /public/env.example.js diff --git a/app/Console/Commands/BackupDatabase.php b/app/Console/Commands/BackupDatabase.php new file mode 100644 index 0000000..36d7be5 --- /dev/null +++ b/app/Console/Commands/BackupDatabase.php @@ -0,0 +1,65 @@ +error("❌:缺少必要配置项: $config , 取消备份"); + return; + } + } + + // 数据库备份逻辑(用你自己的逻辑替换) + $databaseBackupPath = storage_path('backup/' . now()->format('Y-m-d_H-i-s') . '_database_backup.sql'); + try{ + if (config('database.default') === 'mysql'){ + $this->info("1️⃣:开始备份Mysql"); + \Spatie\DbDumper\Databases\MySql::create() + ->setDbName(config('database.connections.mysql.database')) + ->setUserName(config('database.connections.mysql.username')) + ->setPassword(config('database.connections.mysql.password')) + ->dumpToFile($databaseBackupPath); + $this->info("2️⃣:Mysql备份完成"); + }elseif(config('database.default') === 'sqlite'){ + $this->info("1️⃣:开始备份Sqlite"); + \Spatie\DbDumper\Databases\Sqlite::create() + ->setDbName(config('database.connections.sqlite.database')) + ->dumpToFile($databaseBackupPath); + $this->info("2️⃣:Sqlite备份完成"); + } + $this->info("3️⃣:开始将备份上传到Google Cloud"); + // Google Cloud Storage 配置 + $storage = new StorageClient([ + 'keyFilePath' => config('cloud_storage.google_cloud.key_file'), + ]); + $bucket = $storage->bucket(config('cloud_storage.google_cloud.storage_bucket')); + $objectName = 'backup/' . now()->format('Y-m-d_H-i-s') . '_database_backup.sql'; + // 上传文件 + $bucket->upload(fopen($databaseBackupPath, 'r'), [ + 'name' => $objectName, + ]); + + // 输出文件链接 + + \Log::channel('backup')->info("🎉:数据库备份已上传到 Google Cloud Storage: $objectName"); + $this->info("🎉:数据库备份已上传到 Google Cloud Storage: $objectName"); + }catch(\Exception $e){ + \Log::channel('backup')->error("😔:数据库备份失败" . $e->getMessage()); + $this->error("😔:数据库备份失败" . $e->getMessage()); + } + // 开始删除本地备份 + \File::delete($databaseBackupPath); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a96e880..46e7f15 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -40,6 +40,8 @@ class Kernel extends ConsoleKernel $schedule->command('send:remindMail')->dailyAt('11:30'); // horizon metrics $schedule->command('horizon:snapshot')->everyFiveMinutes(); + // backup Timing + $schedule->command('backup:upload-cloud')->daily(); } /** diff --git a/composer.json b/composer.json index 77199f3..6aa982e 100755 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "require": { "php": "^8.1", "firebase/php-jwt": "^6.3", + "google/cloud-storage": "^1.35", "google/recaptcha": "^1.2", "guzzlehttp/guzzle": "^7.4.3", "hhxsv5/laravel-s": "~3.7.0", diff --git a/config/cloud_storage.php b/config/cloud_storage.php new file mode 100644 index 0000000..7ba0747 --- /dev/null +++ b/config/cloud_storage.php @@ -0,0 +1,10 @@ + [ + 'key_file' => env('GOOGLE_CLOUD_KEY_FILE') ? base_path(env('GOOGLE_CLOUD_KEY_FILE')) : null, + 'storage_bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'), + ], + +]; \ No newline at end of file diff --git a/config/logging.php b/config/logging.php index 6963847..6e7c4a1 100755 --- a/config/logging.php +++ b/config/logging.php @@ -45,6 +45,12 @@ return [ 'ignore_exceptions' => false, ], + 'backup' => [ + 'driver' => 'single', + 'path' => storage_path('logs/backup.log'), + 'level' => 'debug', + ], + 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), diff --git a/storage/backup/.gitignore b/storage/backup/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/storage/backup/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file