Skip to content
Advertisement

Symfony Command stores an empty SQL-File for Database Backup

i have an issue by making backup from database (Postgresql 11) with Symfony(6.01) Command. I use also for that the Process Bundle of Symfony.

Problem is, if the Controller Action calls the command to backup the database, it saves finally an empty SQL-File. There is also no errors in dev.log or Postgreql logfile.

But when i print out the command to the browser and execute this in the terminal the SQL-File is filled correctly and not empty.

Any Idea what could be the problem. Thanks in advise. PS: I can not use shell script. Didn’t got it better here with CodeFormatting of the command class.

<?php

namespace AppXXXXXXYYYYYYBundleCommand;

use SymfonyComponentConsoleAttributeAsCommand;

use SymfonyComponentConsoleCommandCommand;

use SymfonyComponentConsoleInputInputArgument;

use SymfonyComponentConsoleInputInputInterface;

use SymfonyComponentConsoleOutputOutputInterface;

use SymfonyComponentHttpKernelKernelInterface;

use SymfonyComponentProcessProcess;

#[AsCommand( name: ‘xxxxx:dump-database’, description: ‘Creates a xxxxx database dump.’, aliases: [‘xxxxx:dump-database’], hidden: false, )]

class DatabaseDumpCommand extends Command {

/**
 * @var string
 */
private string $rootDir;
/**
 * @var string
 */
private string $databaseHost;
/**
 * @var string
 */
private string $databaseName;
/**
 * @var string
 */
private string $databaseUser;
/**
 * @var string
 */
private string $databasePW;

/** * @param string $databaseHost

 * @param string $databaseName

 * @param string $databaseUser

 * @param string $databasePW

 * @param KernelInterface $kernel

 */
public function __construct(
    string $databaseHost,
    string $databaseName,
    string $databaseUser,
    string $databasePW,
    KernelInterface $kernel)
{
    $this->databaseName = $databaseName;
    $this->databaseUser = $databaseUser;
    $this->databaseHost = $databaseHost;
    $this->databasePW = $databasePW;
    $this->rootDir = $kernel->getProjectDir();

    parent::__construct();
}


protected function configure(): void
{
    parent::configure();

    $this
        ->addArgument('file-name',  InputArgument::OPTIONAL, 'Name of dump file')
        ->addArgument('time',  InputArgument::OPTIONAL, 'created at')
    ;
}

public function execute(InputInterface $input, OutputInterface $output): int
{
    set_time_limit(0);

    $time = $input->getArgument('time');
    if (!$time) {
        $time = date('Y-m-dH:i:s');
    }

    $fileName = $input->getArgument('file-name');
    if(!$fileName) {
        $fileName = "xxxxxDbDump";
    }

    $fileName .= "$time.sql";

    $backupPath =  $this->rootDir."/DUMPS/";

    $backupPathTemp = '/tmp/';

    $path = "/usr/bin/pg_dump";

    $sqlQuery = "export PGPASSWORD='$this->databasePW';$path --host=$this->databaseHost --port=5432 --username=$this->databaseUser --create --clean
    --verbose $this->databaseName > $backupPathTemp$fileName;cp /tmp/$fileName $backupPath$fileName";

    //echo $sqlQuery;

    $cmd = method_exists(Process::class, 'fromShellCommandline') ? Process::fromShellCommandline($sqlQuery, null, null, null, null) : new Process($sqlQuery, null, null, null, null);


    /** @var Process $cmd */
    $cmd->run();
    if (!$cmd->isSuccessful()) {
        return false;
    }
    return true;
}

}

Advertisement

Answer

This has worked after the $sqlQuery was written in the CommandClass by this way:

$dumpQuery = 'export PGPASSWORD="' . $this->databasePW . '"; ' .
        $path . ' ' .
        '--host=' . $this->databaseHost . ' ' .
        '--username=' . $this->databaseUser . ' ' .
        '--port='.$this->databasePort . ' ' .
        '--no-password ' .
        '--create ' .
        '--clean ' .
        '--verbose ' .
        $this->databaseName . ' ' . '> ' . $backupPathTemp.$fileName .';' .
        'cp ' . $backupPathTemp.$fileName . ' ' . $backupPath.$fileName;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement