-
-
Save hungthai1401/7746b6f2f3c5d3eb55b83dbfc873002e to your computer and use it in GitHub Desktop.
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Symfony\Component\Process\Process; | |
use Symfony\Component\Process\Exception\ProcessFailedException; | |
class BackUpDatabase extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'db:backup'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'This command will backup the database'; | |
/** | |
* @var Process | |
*/ | |
protected $process; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->process = new Process(sprintf( | |
'PGPASSWORD="%s" pg_dump -U %s -h localhost %s >> %s', | |
config('database.connections.pgsql.password'), | |
config('database.connections.pgsql.username'), | |
config('database.connections.pgsql.database'), | |
storage_path(sprintf('app/backups/backup_%s.sql', now()->format('Ymd'))) | |
)); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
try { | |
$this->info('The backup has been started'); | |
$this->process->mustRun(); | |
$this->info('The backup has been proceed successfully.'); | |
} catch (ProcessFailedException $exception) { | |
logger()->error('Backup exception', compact('exception')); | |
$this->error('The backup process has been failed.'); | |
} | |
} | |
} |
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Symfony\Component\Process\Process; | |
use Symfony\Component\Process\Exception\ProcessFailedException; | |
class RestoreDatabase extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'db:restore {--path=}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'This command will restore the database'; | |
/** | |
* @var Process | |
*/ | |
protected $process; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$path = $this->option('path'); | |
$this->process = new Process(sprintf( | |
'psql -U %s %s < %s', | |
config('database.connections.pgsql.username'), | |
config('database.connections.pgsql.database'), | |
$path ?? storage_path('app/backups/backup.sql') | |
)); | |
try { | |
$this->call('migrate:fresh'); | |
$this->info('The restore has been started'); | |
$this->process->mustRun(); | |
$this->info('The restore has been proceed successfully.'); | |
} catch (ProcessFailedException $exception) { | |
logger()->error('restore exception', compact('exception')); | |
$this->error('The restore process has been failed.'); | |
} | |
} | |
} |
how can i calling this function in controller?
how can i calling this function in controller?
@fauzi442097
Use Artisan::call()
method inside your controller.
use Illuminate\Support\Facades\Artisan;
public function test()
{
Artisan::call('mail:send', [
'user' => $user, '--queue' => 'default'
]);
}
Read docs : https://laravel.com/docs/8.x/artisan#programmatically-executing-commands
ok. when i calling the command, i got this message
"Argument 1 passed to Symfony\Component\Process\Process::__construct() must be of the type array, string given, called in /Applications/XAMPP/xamppfiles/htdocs/lsp-dev-v2/app/Console/Commands/BackUpDatabase.php on line 43"
what should i do?
i'm using laravel 7 and php 7.
ok. when i calling the command, i got this message
"Argument 1 passed to Symfony\Component\Process\Process::__construct() must be of the type array, string given, called in /Applications/XAMPP/xamppfiles/htdocs/lsp-dev-v2/app/Console/Commands/BackUpDatabase.php on line 43"what should i do?
i'm using laravel 7 and php 7.
It means, you're parsing string instead of array. You should parse array argument.
@fauzi442097
You can try another way:
use Symfony\Component\Process\Process;
Process::fromShellCommandline('YOUR_COMMAND_STRING');
Thanks, mate.
Just don't forget to create new directory on
storage/app/backups
and set permission.