mirror of
https://github.com/cedar2025/Xboard.git
synced 2025-01-22 10:38:14 -05:00
feat: 添加·定时自动备份并上传到谷歌云存储·的功能
This commit is contained in:
parent
aa9ec41921
commit
cc1dc14c84
@ -44,5 +44,9 @@ MAIL_FROM_NAME=null
|
|||||||
MAILGUN_DOMAIN=
|
MAILGUN_DOMAIN=
|
||||||
MAILGUN_SECRET=
|
MAILGUN_SECRET=
|
||||||
|
|
||||||
|
# google cloud stoage
|
||||||
|
GOOGLE_CLOUD_KEY_FILE=config/googleCloudStorageKey.json
|
||||||
|
GOOGLE_CLOUD_STORAGE_BUCKET=
|
||||||
|
|
||||||
# 用于阻止重复安装
|
# 用于阻止重复安装
|
||||||
INSTALLED=false
|
INSTALLED=false
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
/node_modules
|
/node_modules
|
||||||
/config/v2board.php
|
/config/v2board.php
|
||||||
|
/config/googleCloudStorageKey.json
|
||||||
/public/hot
|
/public/hot
|
||||||
/public/storage
|
/public/storage
|
||||||
/public/env.example.js
|
/public/env.example.js
|
||||||
|
65
app/Console/Commands/BackupDatabase.php
Normal file
65
app/Console/Commands/BackupDatabase.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Google\Cloud\Storage\StorageClient;
|
||||||
|
|
||||||
|
class BackupDatabase extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'backup:upload-cloud';
|
||||||
|
protected $description = '备份数据库并上传到 Google Cloud Storage';
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
// 判断是否存在必要配置
|
||||||
|
$requiredConfigs = ['database.connections.mysql', 'cloud_storage.google_cloud.key_file', 'cloud_storage.google_cloud.storage_bucket'];
|
||||||
|
foreach ($requiredConfigs as $config) {
|
||||||
|
if (config($config) === null) {
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
@ -40,6 +40,8 @@ class Kernel extends ConsoleKernel
|
|||||||
$schedule->command('send:remindMail')->dailyAt('11:30');
|
$schedule->command('send:remindMail')->dailyAt('11:30');
|
||||||
// horizon metrics
|
// horizon metrics
|
||||||
$schedule->command('horizon:snapshot')->everyFiveMinutes();
|
$schedule->command('horizon:snapshot')->everyFiveMinutes();
|
||||||
|
// backup Timing
|
||||||
|
$schedule->command('backup:upload-cloud')->daily();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": "^8.1",
|
"php": "^8.1",
|
||||||
"firebase/php-jwt": "^6.3",
|
"firebase/php-jwt": "^6.3",
|
||||||
|
"google/cloud-storage": "^1.35",
|
||||||
"google/recaptcha": "^1.2",
|
"google/recaptcha": "^1.2",
|
||||||
"guzzlehttp/guzzle": "^7.4.3",
|
"guzzlehttp/guzzle": "^7.4.3",
|
||||||
"hhxsv5/laravel-s": "~3.7.0",
|
"hhxsv5/laravel-s": "~3.7.0",
|
||||||
|
10
config/cloud_storage.php
Normal file
10
config/cloud_storage.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
'google_cloud' => [
|
||||||
|
'key_file' => env('GOOGLE_CLOUD_KEY_FILE') ? base_path(env('GOOGLE_CLOUD_KEY_FILE')) : null,
|
||||||
|
'storage_bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'),
|
||||||
|
],
|
||||||
|
|
||||||
|
];
|
@ -45,6 +45,12 @@ return [
|
|||||||
'ignore_exceptions' => false,
|
'ignore_exceptions' => false,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'backup' => [
|
||||||
|
'driver' => 'single',
|
||||||
|
'path' => storage_path('logs/backup.log'),
|
||||||
|
'level' => 'debug',
|
||||||
|
],
|
||||||
|
|
||||||
'single' => [
|
'single' => [
|
||||||
'driver' => 'single',
|
'driver' => 'single',
|
||||||
'path' => storage_path('logs/laravel.log'),
|
'path' => storage_path('logs/laravel.log'),
|
||||||
|
2
storage/backup/.gitignore
vendored
Normal file
2
storage/backup/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
Loading…
Reference in New Issue
Block a user