Stream data of backup for less memory usage
This commit is contained in:
parent
0a2a06fb8a
commit
4af4717785
@ -27,13 +27,15 @@ class BackupDatabaseCommand extends Command
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('name', InputArgument::OPTIONAL, 'Database name');
|
||||
$this->addArgument('file', InputArgument::OPTIONAL, 'Backup file name');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$db = $this->getDatabaseName($input, $output);
|
||||
$fileName = $input->getArgument('file');
|
||||
|
||||
$backupName = $this->bs->backup($db);
|
||||
$backupName = $this->bs->backup($db, $fileName);
|
||||
$output->writeln(sprintf('Backup written to: %s', $backupName));
|
||||
|
||||
return Command::SUCCESS;
|
||||
|
@ -21,15 +21,22 @@ class BackupService
|
||||
$fileName = sprintf('%s_%s_backup.sql', $db, $date);
|
||||
}
|
||||
|
||||
$filesystem = new Filesystem();
|
||||
$process = new Process([
|
||||
'mysqldump',
|
||||
'-u', $this->credentials->getUser(),
|
||||
$db,
|
||||
]);
|
||||
$process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]);
|
||||
$process->run();
|
||||
$filesystem->dumpFile($fileName, $process->getOutput());
|
||||
$process->start();
|
||||
|
||||
$stream = fopen($fileName, 'w+');
|
||||
foreach ($process as $type => $data) {
|
||||
if ($process::OUT === $type) {
|
||||
fwrite($stream, $data);
|
||||
} else { // $process::ERR === $type
|
||||
echo "\nError: " . $data;
|
||||
}
|
||||
}
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user