mirror of
https://github.com/cedar2025/Xboard.git
synced 2025-02-02 07:28:13 -05:00
fix: 修复自动备份command 的问题,增加备份后自动压缩为.gz节省空间
This commit is contained in:
parent
0303a74a33
commit
265842e7bf
@ -4,7 +4,7 @@ COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr
|
|||||||
|
|
||||||
RUN install-php-extensions pcntl
|
RUN install-php-extensions pcntl
|
||||||
|
|
||||||
RUN apk --no-cache add shadow supervisor nginx sqlite nginx-mod-http-brotli
|
RUN apk --no-cache add shadow supervisor nginx sqlite nginx-mod-http-brotli mysql-client
|
||||||
|
|
||||||
#复制项目文件以及配置文件
|
#复制项目文件以及配置文件
|
||||||
WORKDIR /www
|
WORKDIR /www
|
||||||
|
@ -4,6 +4,7 @@ namespace App\Console\Commands;
|
|||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Google\Cloud\Storage\StorageClient;
|
use Google\Cloud\Storage\StorageClient;
|
||||||
|
use Symfony\Component\Process\Process;
|
||||||
|
|
||||||
class BackupDatabase extends Command
|
class BackupDatabase extends Command
|
||||||
{
|
{
|
||||||
@ -30,6 +31,8 @@ class BackupDatabase extends Command
|
|||||||
if (config('database.default') === 'mysql'){
|
if (config('database.default') === 'mysql'){
|
||||||
$this->info("1️⃣:开始备份Mysql");
|
$this->info("1️⃣:开始备份Mysql");
|
||||||
\Spatie\DbDumper\Databases\MySql::create()
|
\Spatie\DbDumper\Databases\MySql::create()
|
||||||
|
->setHost(config('database.connections.mysql.host'))
|
||||||
|
->setPort(config('database.connections.mysql.port'))
|
||||||
->setDbName(config('database.connections.mysql.database'))
|
->setDbName(config('database.connections.mysql.database'))
|
||||||
->setUserName(config('database.connections.mysql.username'))
|
->setUserName(config('database.connections.mysql.username'))
|
||||||
->setPassword(config('database.connections.mysql.password'))
|
->setPassword(config('database.connections.mysql.password'))
|
||||||
@ -42,31 +45,50 @@ class BackupDatabase extends Command
|
|||||||
->dumpToFile($databaseBackupPath);
|
->dumpToFile($databaseBackupPath);
|
||||||
$this->info("2️⃣:Sqlite备份完成");
|
$this->info("2️⃣:Sqlite备份完成");
|
||||||
}
|
}
|
||||||
|
$this->info('3️⃣:开始压缩备份文件');
|
||||||
|
// 使用 gzip 压缩备份文件
|
||||||
|
$compressedBackupPath = $databaseBackupPath . '.gz';
|
||||||
|
$gzipCommand = new Process(["gzip", "-c", $databaseBackupPath]);
|
||||||
|
$gzipCommand->run();
|
||||||
|
|
||||||
|
// 检查压缩是否成功
|
||||||
|
if ($gzipCommand->isSuccessful()) {
|
||||||
|
// 压缩成功,你可以删除原始备份文件
|
||||||
|
file_put_contents($compressedBackupPath, $gzipCommand->getOutput());
|
||||||
|
$this->info('4️⃣:文件压缩成功');
|
||||||
|
unlink($databaseBackupPath);
|
||||||
|
} else {
|
||||||
|
// 压缩失败,处理错误
|
||||||
|
echo $gzipCommand->getErrorOutput();
|
||||||
|
$this->error('😔:文件压缩失败');
|
||||||
|
unlink($databaseBackupPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!$isUpload){
|
if (!$isUpload){
|
||||||
$this->info("🎉:数据库成功备份到:$databaseBackupPath");
|
$this->info("🎉:数据库成功备份到:$compressedBackupPath");
|
||||||
}else{
|
}else{
|
||||||
// 传到云盘
|
// 传到云盘
|
||||||
$this->info("3️⃣:开始将备份上传到Google Cloud");
|
$this->info("5️⃣:开始将备份上传到Google Cloud");
|
||||||
// Google Cloud Storage 配置
|
// Google Cloud Storage 配置
|
||||||
$storage = new StorageClient([
|
$storage = new StorageClient([
|
||||||
'keyFilePath' => config('cloud_storage.google_cloud.key_file'),
|
'keyFilePath' => config('cloud_storage.google_cloud.key_file'),
|
||||||
]);
|
]);
|
||||||
$bucket = $storage->bucket(config('cloud_storage.google_cloud.storage_bucket'));
|
$bucket = $storage->bucket(config('cloud_storage.google_cloud.storage_bucket'));
|
||||||
$objectName = 'backup/' . now()->format('Y-m-d_H-i-s') . '_database_backup.sql';
|
$objectName = 'backup/' . now()->format('Y-m-d_H-i-s') . '_database_backup.sql.gz';
|
||||||
// 上传文件
|
// 上传文件
|
||||||
$bucket->upload(fopen($databaseBackupPath, 'r'), [
|
$bucket->upload(fopen($compressedBackupPath, 'r'), [
|
||||||
'name' => $objectName,
|
'name' => $objectName,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 输出文件链接
|
// 输出文件链接
|
||||||
\Log::channel('backup')->info("🎉:数据库备份已上传到 Google Cloud Storage: $objectName");
|
\Log::channel('backup')->info("🎉:数据库备份已上传到 Google Cloud Storage: $objectName");
|
||||||
$this->info("🎉:数据库备份已上传到 Google Cloud Storage: $objectName");
|
$this->info("🎉:数据库备份已上传到 Google Cloud Storage: $objectName");
|
||||||
\File::delete($databaseBackupPath);
|
\File::delete($compressedBackupPath);
|
||||||
}
|
}
|
||||||
}catch(\Exception $e){
|
}catch(\Exception $e){
|
||||||
\Log::channel('backup')->error("😔:数据库备份失败 \n" . $e);
|
\Log::channel('backup')->error("😔:数据库备份失败 \n" . $e);
|
||||||
$this->error("😔:数据库备份失败\n" . $e);
|
$this->error("😔:数据库备份失败\n" . $e);
|
||||||
\File::delete($databaseBackupPath);
|
\File::delete($compressedBackupPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user