From 4aa7a32e2756bb783bf94d3741f9a1558018d828 Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Fri, 26 Apr 2024 14:49:00 +0300 Subject: [PATCH] Update HostingSubscriptionBackup.php --- web/app/Models/HostingSubscriptionBackup.php | 109 ++++++++++++------- 1 file changed, 67 insertions(+), 42 deletions(-) diff --git a/web/app/Models/HostingSubscriptionBackup.php b/web/app/Models/HostingSubscriptionBackup.php index c35bfdc7..8441abbc 100644 --- a/web/app/Models/HostingSubscriptionBackup.php +++ b/web/app/Models/HostingSubscriptionBackup.php @@ -154,61 +154,86 @@ public function startBackup() $backupTempPath = $backupPath.'/temp'; shell_exec('mkdir -p ' . $backupTempPath); - if ($this->backup_type == 'full') { - $backupFileName = Str::slug($findHostingSubscription->system_username .'-'. date('Ymd-His')) . '.tar.gz'; - $backupFilePath = $backupPath.'/'.$backupFileName; + $backupFileName = Str::slug($findHostingSubscription->system_username .'-'. date('Ymd-His')) . '.tar.gz'; + $backupFilePath = $backupPath.'/'.$backupFileName; - $backupLogFileName = 'backup.log'; - $backupLogFilePath = $backupPath.'/'.$backupLogFileName; + $backupLogFileName = 'backup.log'; + $backupLogFilePath = $backupPath.'/'.$backupLogFileName; - $backupTargetPath = $findMainDomain->domain_root . '/backups'; - $backupTargetFilePath = $backupTargetPath.'/'.$backupFileName; + $backupTargetPath = $findMainDomain->domain_root . '/backups'; + $backupTargetFilePath = $backupTargetPath.'/'.$backupFileName; - $backupTempScript = '/tmp/backup-script-'.$this->id.'.sh'; - $shellFileContent = ''; - $shellFileContent .= 'mkdir -p '. $backupTargetPath.PHP_EOL; - $shellFileContent .= 'echo "Backup up domain: '.$findHostingSubscription->domain . PHP_EOL; - $shellFileContent .= 'echo "Backup filename: '.$backupFileName. PHP_EOL; - $shellFileContent .= 'cp -r /home/'.$findHostingSubscription->system_username.' '.$backupTempPath.PHP_EOL; + $backupTempScript = '/tmp/backup-script-'.$this->id.'.sh'; + $shellFileContent = ''; + $shellFileContent .= 'mkdir -p '. $backupTargetPath.PHP_EOL; + $shellFileContent .= 'echo "Backup up domain: '.$findHostingSubscription->domain . PHP_EOL; + $shellFileContent .= 'echo "Backup filename: '.$backupFileName. PHP_EOL; - $shellFileContent .= 'cd '.$backupTempPath .' && tar -czvf '.$backupFilePath.' ./* '. PHP_EOL; + if ($this->backup_type == 'full') { + $shellFileContent .= 'cp -r /home/' . $findHostingSubscription->system_username . ' ' . $backupTempPath . PHP_EOL; + } - $shellFileContent .= 'rm -rf '.$backupTempPath.PHP_EOL; - $shellFileContent .= 'echo "Backup complete"' . PHP_EOL; - $shellFileContent .= 'touch ' . $backupTargetPath. '/backup-'.$this->id.'.done' . PHP_EOL; - $shellFileContent .= 'rm -rf ' . $backupTempScript . PHP_EOL; + if ($this->backup_type == 'full' || $this->backup_type == 'database') { + $getDatabases = Database::where('hosting_subscription_id', $findHostingSubscription->id) + ->where('is_remote_database_server', 0) + ->get(); + if ($getDatabases->count() > 0) { + foreach ($getDatabases as $database) { + $findDatabaseUser = DatabaseUser::where('database_id', $database->id) + ->first(); + if (!$findDatabaseUser) { + continue; + } + $databaseName = $database->database_name_prefix . '_' . $database->database_name; + $databaseUser = $findDatabaseUser->username_prefix . $findDatabaseUser->username; + $databaseUserPassword = $findDatabaseUser->password; + + $shellFileContent .= 'echo "Backup up database: ' . $databaseName . PHP_EOL; + $shellFileContent .= 'echo "Backup up database user: ' . $databaseUser . PHP_EOL; + $databaseBackupPath = $backupTempPath . '/' . $databaseName . '.sql'; + $shellFileContent .= 'mysqldump -u ' . $databaseUser . ' -p' . $databaseUserPassword . ' ' . $databaseName . ' > ' . $databaseBackupPath . PHP_EOL; + + } + } + } - $shellFileContent .= 'mv '.$backupFilePath.' '. $backupTargetFilePath.PHP_EOL; + $shellFileContent .= 'cd '.$backupTempPath .' && tar -czvf '.$backupFilePath.' ./* '. PHP_EOL; - file_put_contents($backupTempScript, $shellFileContent); + $shellFileContent .= 'rm -rf '.$backupTempPath.PHP_EOL; + $shellFileContent .= 'echo "Backup complete"' . PHP_EOL; + $shellFileContent .= 'touch ' . $backupTargetPath. '/backup-'.$this->id.'.done' . PHP_EOL; + $shellFileContent .= 'rm -rf ' . $backupTempScript . PHP_EOL; - $processId = shell_exec('bash '.$backupTempScript.' >> ' . $backupLogFilePath . ' & echo $!'); - $processId = intval($processId); + $shellFileContent .= 'mv '.$backupFilePath.' '. $backupTargetFilePath.PHP_EOL; - if ($processId > 0 && is_numeric($processId)) { + file_put_contents($backupTempScript, $shellFileContent); - $this->path = $findMainDomain->domain_root . '/backups'; - $this->filepath = $backupTargetFilePath; - $this->status = 'processing'; - $this->queued = true; - $this->queued_at = now(); - $this->process_id = $processId; - $this->save(); + $processId = shell_exec('bash '.$backupTempScript.' >> ' . $backupLogFilePath . ' & echo $!'); + $processId = intval($processId); - return [ - 'status' => 'processing', - 'message' => 'Backup started' - ]; - } else { - $this->status = 'failed'; - $this->save(); - return [ - 'status' => 'failed', - 'message' => 'Backup failed to start' - ]; - } + if ($processId > 0 && is_numeric($processId)) { + + $this->path = $findMainDomain->domain_root . '/backups'; + $this->filepath = $backupTargetFilePath; + $this->status = 'processing'; + $this->queued = true; + $this->queued_at = now(); + $this->process_id = $processId; + $this->save(); + return [ + 'status' => 'processing', + 'message' => 'Backup started' + ]; + } else { + $this->status = 'failed'; + $this->save(); + return [ + 'status' => 'failed', + 'message' => 'Backup failed to start' + ]; } + } }