Skip to content

Instantly share code, notes, and snippets.

@hungthai1401
Created November 2, 2018 09:47
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save hungthai1401/7746b6f2f3c5d3eb55b83dbfc873002e to your computer and use it in GitHub Desktop.
Save hungthai1401/7746b6f2f3c5d3eb55b83dbfc873002e 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.');
}
}
}
@fendis0709
Copy link

Thanks, mate.
Just don't forget to create new directory on storage/app/backups and set permission.

@fauzi442097
Copy link

how can i calling this function in controller?

@fendis0709
Copy link

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

@fauzi442097
Copy link

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.

@fendis0709
Copy link

fendis0709 commented May 5, 2021

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
Copy link

after i parsing string to array with character space, i got error like this
image

am i wrong when parsing string based on white space character?

@hungthai1401
Copy link
Author

@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