You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I configured a PostgresGlobalBackupCommand backup to run before laravel-backup to get the database roles, but I would like the file to be next to the database backup.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class PostgresGlobalBackupCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'backup:postgres-global';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Criar um backup global do banco de dados PostgreSQL incluindo roles';
protected string $database = '';
protected string $username = '';
protected string $password = '';
protected string $host = 'localhost';
protected int $port = 5432;
protected string $socket = '';
protected int $timeout = 0;
protected string $dumpBinaryPath = '';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->host = config('database.connections.pgsql.host');
$this->port = config('database.connections.pgsql.port');
$this->username = config('database.connections.pgsql.username');
$this->password = config('database.connections.pgsql.password');
$this->database = config('database.connections.pgsql.database');
$file = storage_path('..\globals.sql');
$command = $this->getDumpCommand($file);
$envVars = [];
$envVars['PGPASSWORD'] = $this->password;
$process = Process::fromShellCommandline($command, null, $envVars, null, $this->timeout);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$this->info('O backup foi realizado com sucesso.');
return 0;
}
public function getDumpCommand(string $dumpFile): string
{
$quote = $this->determineQuote();
$command = [
"{$quote}{$this->dumpBinaryPath}pg_dumpall{$quote}",
"-U \"{$this->username}\"",
'-h '.($this->socket === '' ? $this->host : $this->socket),
"-p {$this->port}",
"-g",
"-w",
];
return $this->echoToFile(implode(' ', $command), $dumpFile);
}
protected function determineQuote(): string
{
return $this->isWindows() ? '"' : "'";
}
protected function isWindows(): bool
{
return str_starts_with(strtoupper(PHP_OS), 'WIN');
}
protected function echoToFile(string $command, string $dumpFile): string
{
$dumpFile = '"' . addcslashes($dumpFile, '\\"') . '"';
return $command . ' > ' . $dumpFile;
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I configured a PostgresGlobalBackupCommand backup to run before laravel-backup to get the database roles, but I would like the file to be next to the database backup.
Beta Was this translation helpful? Give feedback.
All reactions