generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ahoicloud/feat/optimize
Add ability to create a database backup
- Loading branch information
Showing
6 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace AHOI\SqliteOptimize\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
|
||
class SqliteBackupCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'sqlite:backup'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Backup SQLite database'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
$database = config('database.connections.sqlite.database'); | ||
$filename = 'backup-'.now()->timestamp.'.sql'; | ||
$this->info('Starting SQLite backup...'); | ||
$backupPath = database_path('backups'); | ||
|
||
// Create the backup directory if it doesn't exist | ||
if (File::isDirectory($backupPath)) { | ||
$this->info('Backup directory created at: '.$backupPath); | ||
File::makeDirectory($backupPath); | ||
} | ||
|
||
try { | ||
// Create a backup of the SQLite database | ||
$file_path = database_path('backups/'.$filename); | ||
File::copy($database, $file_path); | ||
|
||
$this->info('SQLite backup created successfully at: '.$file_path); | ||
|
||
return Command::SUCCESS; | ||
} catch (\Exception $e) { | ||
$this->error('Failed to create SQLite backup: '.$e->getMessage()); | ||
|
||
return Command::FAILURE; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters