perf: 优化数据库备份Command逻辑

This commit is contained in:
xboard 2023-12-13 09:28:01 +08:00
parent f812e5f239
commit b8009142ed

View File

@ -25,10 +25,10 @@ class BackupDatabase extends Command
} }
} }
// 数据库备份逻辑(用你自己的逻辑替换) // 数据库备份逻辑
$databaseBackupPath = storage_path('backup/' . now()->format('Y-m-d_H-i-s') . '_database_backup.sql');
try{ try{
if (config('database.default') === 'mysql'){ if (config('database.default') === 'mysql'){
$databaseBackupPath = storage_path('backup/' . now()->format('Y-m-d_H-i-s') . '_' . config('database.connections.mysql.database') . '_database_backup.sql');
$this->info("1开始备份Mysql"); $this->info("1开始备份Mysql");
\Spatie\DbDumper\Databases\MySql::create() \Spatie\DbDumper\Databases\MySql::create()
->setHost(config('database.connections.mysql.host')) ->setHost(config('database.connections.mysql.host'))
@ -39,11 +39,15 @@ class BackupDatabase extends Command
->dumpToFile($databaseBackupPath); ->dumpToFile($databaseBackupPath);
$this->info("2Mysql备份完成"); $this->info("2Mysql备份完成");
}elseif(config('database.default') === 'sqlite'){ }elseif(config('database.default') === 'sqlite'){
$databaseBackupPath = storage_path('backup/' . now()->format('Y-m-d_H-i-s') . '_sqlite' . '_database_backup.sql');
$this->info("1开始备份Sqlite"); $this->info("1开始备份Sqlite");
\Spatie\DbDumper\Databases\Sqlite::create() \Spatie\DbDumper\Databases\Sqlite::create()
->setDbName(config('database.connections.sqlite.database')) ->setDbName(config('database.connections.sqlite.database'))
->dumpToFile($databaseBackupPath); ->dumpToFile($databaseBackupPath);
$this->info("2Sqlite备份完成"); $this->info("2Sqlite备份完成");
}else{
$this->error('备份失败你的数据库不是sqlite或者mysql');
return;
} }
$this->info('3开始压缩备份文件'); $this->info('3开始压缩备份文件');
// 使用 gzip 压缩备份文件 // 使用 gzip 压缩备份文件