find('mysqldump'); $process = new Process([ $mysqldumpPath, '-h', $dbHost, '-u', $dbUser, '-p' . $dbPass, '--single-transaction', '--no-tablespaces', $dbDatabase, ]); $process->start(); $errors = ""; $dumpTempFile = tempnam(sys_get_temp_dir(), 'bsbackup'); $dumpTempFileResource = fopen($dumpTempFile, 'w'); foreach ($process as $type => $data) { if ($process::OUT === $type) { fwrite($dumpTempFileResource, $data); } else { // $process::ERR === $type $errors .= $data . "\n"; } } fclose($dumpTempFileResource); // Create a new ZIP file $zipTempFile = tempnam(sys_get_temp_dir(), 'bsbackup'); $zip = new ZipArchive(); $sep = DIRECTORY_SEPARATOR; $zip->open($zipTempFile, ZipArchive::CREATE); $zip->addFile($this->appDir . $sep . '.env', '.env'); $zip->addFile($dumpTempFile, 'db.sql'); $fileDirs = [ $this->appDir . $sep . 'public' . $sep . 'uploads' => 'public/uploads', $this->appDir . $sep . 'storage' . $sep . 'uploads' => 'storage/uploads', ]; foreach ($fileDirs as $fullFileDir => $relativeFileDir) { $dirIter = new RecursiveDirectoryIterator($fullFileDir); $fileIter = new \RecursiveIteratorIterator($dirIter); /** @var \SplFileInfo $file */ foreach ($fileIter as $file) { if (!$file->isDir()) { $zip->addFile($file->getPathname(), $relativeFileDir . '/' . $fileIter->getSubPathname()); } } } // Close off our zip and move it to the required location $zip->close(); rename($zipTempFile, $zipOutFile); // Delete our temporary DB dump file unlink($dumpTempFile); // Announce end and display errors echo "Finished"; if ($errors) { echo " with the following errors:\n" . $errors; } } }