Created
November 2, 2018 09:47
-
-
Save hungthai1401/7746b6f2f3c5d3eb55b83dbfc873002e to your computer and use it in GitHub Desktop.
Laravel command backup, restore postgresql database
This file contains hidden or 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
<?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.'); | |
} | |
} | |
} |
This file contains hidden or 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
<?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.'); | |
} | |
} | |
} |
@fauzi442097
You can try another way:
use Symfony\Component\Process\Process;
Process::fromShellCommandline('YOUR_COMMAND_STRING');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It means, you're parsing string instead of array. You should parse array argument.