diff --git a/web/app/Console/Commands/RunUploadBackupsToRemoteServers.php b/web/app/Console/Commands/RunUploadBackupsToRemoteServers.php new file mode 100644 index 0000000..596cf3f --- /dev/null +++ b/web/app/Console/Commands/RunUploadBackupsToRemoteServers.php @@ -0,0 +1,55 @@ +count() > 0) { + foreach ($getRemoteBackupServers as $remoteBackupServer) { + $remoteBackupServer->healthCheck(); + if ($remoteBackupServer->status == 'offline') { + $this->info('Skipping ' . $remoteBackupServer->name . ' because it is offline.'); + continue; + } + + $this->info('Uploading backups to ' . $remoteBackupServer->name . '...'); + + $findBackups = Backup::where('status', 'completed')->get(); + if ($findBackups->count() > 0) { + foreach ($findBackups as $backup) { + $uploadStatus = $remoteBackupServer->uploadFile($backup->filepath); + } + } else { + $this->info('No backups found to upload.'); + } + + } + } else { + $this->info('No remote backup servers found.'); + } + } +} diff --git a/web/app/Models/RemoteBackupServer.php b/web/app/Models/RemoteBackupServer.php index 3a825ee..adef308 100644 --- a/web/app/Models/RemoteBackupServer.php +++ b/web/app/Models/RemoteBackupServer.php @@ -5,6 +5,7 @@ namespace App\Models; use Doctrine\DBAL\DriverManager; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Symfony\Component\Process\Process; class RemoteBackupServer extends Model { @@ -30,9 +31,6 @@ class RemoteBackupServer extends Model $model->healthCheck(); }); - static::updated(function ($model) { - $model->healthCheck(); - }); } public function healthCheck() @@ -48,19 +46,19 @@ class RemoteBackupServer extends Model $path = trim($this->path); $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, 'ftp://'.$hostname.':'.$port.'/'.$path.'/'); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_URL, 'ftp://'.$hostname.':'.$port.'/'); curl_setopt($curl, CURLOPT_TIMEOUT, 30); - curl_setopt($curl, CURLOPT_USERPWD, $username.':'.$password); + curl_setopt($curl, CURLOPT_FTPLISTONLY, 1); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); $curlResponse = curl_exec($curl); - curl_close($curl); - if ($curlResponse) { - $this->status = 'online'; + if ($curlResponse === false) { + $this->status = 'offline'; $this->save(); return; } else { - $this->status = 'offline'; + $this->status = 'online'; $this->save(); return; } @@ -74,4 +72,38 @@ class RemoteBackupServer extends Model } + + public function uploadFile($filepath) + { + $username = trim($this->username); + $password = trim($this->password); + $hostname = trim($this->hostname); + $port = trim($this->port); + + if ($this->type == 'ftp') { + + $path = trim($this->path); + if ($path == '/') { + $path = ''; + } + + $uploadCurlCommand = "curl -T $filepath ftp://$username:$password@$hostname:$port "." $path"; + $uploadCurlCommand = trim($uploadCurlCommand); + + $uploadCurlProcess = Process::fromShellCommandline($uploadCurlCommand); + $uploadCurlProcess->run(); + + if (!$uploadCurlProcess->isSuccessful()) { + return [ + 'status' => 'error', + 'message' => 'Failed to upload backup to remote server.' + ]; + } + + return [ + 'status' => 'success', + 'message' => 'Backup uploaded successfully.' + ]; + } + } } diff --git a/web/tests/Unit/DockerTest.php b/web/tests/Unit/DockerTest.php new file mode 100644 index 0000000..71d40b6 --- /dev/null +++ b/web/tests/Unit/DockerTest.php @@ -0,0 +1,15 @@ +