+ $zipOutFile = $this->buildZipFilePath($suggestedOutPath);
+ $dumpTempFile = $this->createDatabaseDump();
+
+ // Create a new ZIP file
+ $zipTempFile = tempnam(sys_get_temp_dir(), 'bsbackup');
+ $zip = new ZipArchive();
+ $zip->open($zipTempFile, ZipArchive::CREATE);
+ $zip->addFile($this->appDir . DIRECTORY_SEPARATOR . '.env', '.env');
+
+ if ($handleDatabase) {
+ $zip->addFile($dumpTempFile, 'db.sql');
+ }
+
+ if ($handleUploads) {
+ $this->addUploadFoldersToZip($zip);
+ }
+
+ // 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";
+ }
+
+ /**
+ * Build a full zip path from the given suggestion, which may be empty,
+ * a path to a folder, or a path to a file in relative or absolute form.
+ */
+ protected function buildZipFilePath(string $suggestedOutPath): string
+ {
+ $zipDir = getcwd() ?: $this->appDir;
+ $zipName = "bookstack-backup-" . date('Y-m-d-His') . '.zip';
+
+ if ($suggestedOutPath) {
+ if (is_dir($suggestedOutPath)) {
+ $zipDir = realpath($suggestedOutPath);
+ } else if (is_dir(dirname($suggestedOutPath))) {
+ $zipDir = realpath(dirname($suggestedOutPath));
+ $zipName = basename($suggestedOutPath);
+ } else {
+ // TODO - Handle not found output
+ }
+ }
+
+ $fullPath = $zipDir . DIRECTORY_SEPARATOR . $zipName;