Symfony 进程组件 mysqldump gzip

问题描述 投票:0回答:1

我试图弄清楚如何使用 Symfony 进程组件将带有 gzip 压缩的管道添加到 mysqldump 命令中。我拥有的,但似乎不起作用的是:

$date = Carbon::now();

$fileName = $date->format('Y-m-d_H-i-s').'.sql';

$command = [
    $this->mysqldumpPath,
    '--add-drop-table',
    '--skip-comments',
    '--default-character-set=utf8mb4',
    $this->ignoreTables(),
    '--user='.$this->dbConfig['username'],
    '--password='.$this->dbConfig['password'],
    $this->dbConfig['database'],
];

if ($this->compress) {
    $command[] = '| gzip -9';
    $fileName = $fileName.'.gz';
}

$file = $this->backupPath($fileName);

$process = new Process(array_merge(
    $command,
    ['--result-file='.$file]
));

$process->run();

我遇到的错误是

mysqldump:找不到表:“| gzip -9”

ignoreTables()
方法

private function ignoreTables(): ?string
{
    if (empty($this->ignoreTables)) {
        return null;
    }

    return collect($this->ignoreTables)->map(
        fn(string $table) => sprintf(
            '--ignore-table=%s.%s',
            $this->dbConfig['database'],
            $table
        )
    )->implode(' ');
}

知道如何将管道 gzip 标志传递给命令吗?

php symfony
1个回答
4
投票

您仍然可以将命令构建为字符串并使用

Process::fromShellCommandline
在 shell 中运行它。

不过,我认为你的命令不会像写的那样工作,所以我正在进行我认为必要的更改(你也可以清理它,而不是使用数组来构建命令)。

主要:

$date = Carbon::now();
$fileName = $date->format('Y-m-d_H-i-s').'.sql';
$file = $this->backupPath($fileName);

$command = [
    $this->mysqldumpPath,
    '--add-drop-table',
    '--skip-comments',
    '--default-character-set=utf8mb4',
    '--user="${:USER}"',
    '--password="${:PASSWORD}"',
    '"${:DATABASE}"',
];
$command = array_merge($command, $this->getIgnoredTablesArguments());

if ($this->compress) {
    $command[] = ' | gzip -9 > "${:OUTPUT_FILE}"';
    $file .= '.gz';
} else {
    $command[] = '--result-file="${:OUTPUT_FILE}"';
}

$process = Process::fromShellCommandline(implode(' ', $command));

$parameters = [
    'USER' => $this->dbConfig['username'],
    'PASSWORD' => $this->dbConfig['password'],
    'DATABASE' => $this->dbConfig['database'],
    'OUTPUT_FILE' => $file,
];
$parameters = array_merge($parameters, $this->getIgnoredTablesParameters());

$process->run(null, $parameters);

辅助方法:

private function ignoreTables(): array
{
    return collect($this->ignoreTables)->map(
        fn(string $table) => sprintf(
            '%s.%s',
            $this->dbConfig['database'],
            $table
        )
    );
}

private function getIgnoredTablesArguments(): array
{
    $arguments = [];
    foreach ($this->ignoreTables as $k => $v) {
        $argName = sprintf('${:IGNORE_%s}', $k);
        $arguments[] = sprintf('--ignore-table="%s"', $argName);
    }
    return $arguments;
}

private function getIgnoredTablesParameters(): array
{
    $envs = [];
    foreach ($this->ignoreTables() as $k => $v) {
        $argName = sprintf('IGNORE_%s', $k);
        $envs = array_merge($envs, [$argName => $v]);
    }
    return $envs;
}
© www.soinside.com 2019 - 2024. All rights reserved.