Skip to content

Instantly share code, notes, and snippets.

@nicoaudy
Forked from hungthai1401/BackUpDatabase.php
Created February 22, 2021 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoaudy/eb3f2c7350e41c94ac66cc77c4a2285e to your computer and use it in GitHub Desktop.
Save nicoaudy/eb3f2c7350e41c94ac66cc77c4a2285e to your computer and use it in GitHub Desktop.
Laravel command backup, restore postgresql database
<?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.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment