This commit is contained in:
Bozhidar 2024-04-29 19:27:27 +03:00
parent bae4213184
commit e68b198d74
3 changed files with 112 additions and 10 deletions

View file

@ -0,0 +1,55 @@
<?php
namespace App\Console\Commands;
use App\Models\Backup;
use App\Models\RemoteBackupServer;
use Illuminate\Console\Command;
class RunUploadBackupsToRemoteServers extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'phyre:run-upload-backups-to-remote-servers';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$getRemoteBackupServers = RemoteBackupServer::all();
if ($getRemoteBackupServers->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.');
}
}
}

View file

@ -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.'
];
}
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace tests\Unit;
use Tests\Feature\Api\ActionTestCase;
class DockerTest extends ActionTestCase
{
public function testDocker()
{
}
}