2024-04-29 10:44:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-04-29 11:15:17 +00:00
|
|
|
use Doctrine\DBAL\DriverManager;
|
2024-04-29 10:44:02 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-04-29 16:27:27 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2024-04-29 10:44:02 +00:00
|
|
|
|
|
|
|
class RemoteBackupServer extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'type',
|
|
|
|
'hostname',
|
|
|
|
'port',
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'path',
|
|
|
|
'ssh_private_key',
|
|
|
|
'ssh_private_key_password',
|
|
|
|
];
|
2024-04-29 11:15:17 +00:00
|
|
|
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::created(function ($model) {
|
|
|
|
$model->healthCheck();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function healthCheck()
|
|
|
|
{
|
|
|
|
if ($this->type == 'ftp') {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$username = trim($this->username);
|
|
|
|
$password = trim($this->password);
|
|
|
|
$hostname = trim($this->hostname);
|
|
|
|
$port = trim($this->port);
|
|
|
|
$path = trim($this->path);
|
|
|
|
|
|
|
|
$curl = curl_init();
|
2024-04-29 16:27:27 +00:00
|
|
|
curl_setopt($curl, CURLOPT_URL, 'ftp://'.$hostname.':'.$port.'/');
|
2024-04-29 11:15:17 +00:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
|
2024-04-29 16:27:27 +00:00
|
|
|
curl_setopt($curl, CURLOPT_FTPLISTONLY, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
|
2024-04-29 11:15:17 +00:00
|
|
|
$curlResponse = curl_exec($curl);
|
|
|
|
|
2024-04-29 16:27:27 +00:00
|
|
|
if ($curlResponse === false) {
|
|
|
|
$this->status = 'offline';
|
2024-04-29 11:15:17 +00:00
|
|
|
$this->save();
|
|
|
|
return;
|
|
|
|
} else {
|
2024-04-29 16:27:27 +00:00
|
|
|
$this->status = 'online';
|
2024-04-29 11:15:17 +00:00
|
|
|
$this->save();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->status = 'offline';
|
|
|
|
$this->save();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-04-29 16:27:27 +00:00
|
|
|
|
2024-04-29 16:34:15 +00:00
|
|
|
public function uploadFile($filepath, $directory)
|
2024-04-29 16:27:27 +00:00
|
|
|
{
|
|
|
|
$username = trim($this->username);
|
|
|
|
$password = trim($this->password);
|
|
|
|
$hostname = trim($this->hostname);
|
|
|
|
$port = trim($this->port);
|
|
|
|
|
|
|
|
if ($this->type == 'ftp') {
|
|
|
|
|
2024-04-29 16:34:15 +00:00
|
|
|
$directory = trim($directory);
|
2024-04-29 16:27:27 +00:00
|
|
|
|
2024-04-29 16:34:15 +00:00
|
|
|
$uploadCurlCommand = "curl -T $filepath ftp://$hostname:$port/$directory/ -u '$username:$password' --ftp-create-dirs";
|
2024-04-29 16:27:27 +00:00
|
|
|
$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.'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2024-04-29 10:44:02 +00:00
|
|
|
}
|