From f187040b7e2eeffa05de813c984a0b807254e71c Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 24 Oct 2023 10:42:28 +0200 Subject: [PATCH] fix: mongodb backup --- app/Jobs/DatabaseBackupJob.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/Jobs/DatabaseBackupJob.php b/app/Jobs/DatabaseBackupJob.php index 84aad1b6c..d73474018 100644 --- a/app/Jobs/DatabaseBackupJob.php +++ b/app/Jobs/DatabaseBackupJob.php @@ -175,13 +175,20 @@ private function backup_standalone_mongodb(string $databaseWithCollections): voi $commands[] = "mkdir -p " . $this->backup_dir; $commands[] = "docker exec $this->container_name mongodump --authenticationDatabase=admin --uri=$url --gzip --archive > $this->backup_location"; } else { - $collectionsToExclude = str($databaseWithCollections)->after(':')->explode(','); - $databaseName = str($databaseWithCollections)->before(':'); + if (str($databaseWithCollections)->contains(':')) { + $databaseName = str($databaseWithCollections)->before(':'); + $collectionsToExclude = str($databaseWithCollections)->after(':')->explode(','); + } else { + $databaseName = $databaseWithCollections; + $collectionsToExclude = collect(); + } $commands[] = "mkdir -p " . $this->backup_dir; - $commands[] = "docker exec $this->container_name mongodump --authenticationDatabase=admin --uri=$url --db $databaseName --gzip --excludeCollection " . $collectionsToExclude->implode(' --excludeCollection ') . " --archive > $this->backup_location"; + if ($collectionsToExclude->count() === 0) { + $commands[] = "docker exec $this->container_name mongodump --authenticationDatabase=admin --uri=$url --db $databaseName --gzip --archive > $this->backup_location"; + } else { + $commands[] = "docker exec $this->container_name mongodump --authenticationDatabase=admin --uri=$url --db $databaseName --gzip --excludeCollection " . $collectionsToExclude->implode(' --excludeCollection ') . " --archive > $this->backup_location"; + } } - - ray($commands); $this->backup_output = instant_remote_process($commands, $this->server); $this->backup_output = trim($this->backup_output); if ($this->backup_output === '') {