spatie / laravel-backup

A package to backup your Laravel app

Home Page:https://spatie.be/docs/laravel-backup

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Backup PostgreSQL Global

rlzdesenv opened this issue · comments

Discussed in #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

image

<?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;
    }

}

```</div>