Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup PostgreSQL Global #1775

Closed
rlzdesenv opened this issue Mar 27, 2024 Discussed in #1774 · 0 comments
Closed

Backup PostgreSQL Global #1775

rlzdesenv opened this issue Mar 27, 2024 Discussed in #1774 · 0 comments

Comments

@rlzdesenv
Copy link

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>
@spatie spatie locked and limited conversation to collaborators Jul 5, 2024
@Nielsvanpach Nielsvanpach converted this issue into discussion #1812 Jul 5, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant