spatie / laravel-backup

A package to backup your Laravel app
https://spatie.be/docs/laravel-backup
MIT License
5.64k stars 763 forks source link

Backup PostgreSQL Global #1775

Closed rlzdesenv closed 3 months ago

rlzdesenv commented 7 months ago

Discussed in https://github.com/spatie/laravel-backup/discussions/1774

Originally posted by **rlzdesenv** March 27, 2024 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. ![image](https://github.com/spatie/laravel-backup/assets/86244364/686058e5-7357-4992-bf2c-5f61c5fe8fd1) ![image](https://github.com/spatie/laravel-backup/assets/86244364/a1ef6116-2ad8-45ac-953d-75c63f14eca6) ``` 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; } } ```