Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Repositories/Wings/DaemonBackupRepository.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Repositories\Wings;
4
5
use Webmozart\Assert\Assert;
6
use Pterodactyl\Models\Backup;
7
use Pterodactyl\Models\Server;
8
use Psr\Http\Message\ResponseInterface;
9
use GuzzleHttp\Exception\TransferException;
10
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
11
12
/**
13
* @method \Pterodactyl\Repositories\Wings\DaemonBackupRepository setNode(\Pterodactyl\Models\Node $node)
14
* @method \Pterodactyl\Repositories\Wings\DaemonBackupRepository setServer(\Pterodactyl\Models\Server $server)
15
*/
16
class DaemonBackupRepository extends DaemonRepository
17
{
18
protected ?string $adapter;
19
20
/**
21
* Sets the backup adapter for this execution instance.
22
*/
23
public function setBackupAdapter(string $adapter): self
24
{
25
$this->adapter = $adapter;
26
27
return $this;
28
}
29
30
/**
31
* Tells the remote Daemon to begin generating a backup for the server.
32
*
33
* @throws DaemonConnectionException
34
*/
35
public function backup(Backup $backup): ResponseInterface
36
{
37
Assert::isInstanceOf($this->server, Server::class);
38
39
try {
40
return $this->getHttpClient()->post(
41
sprintf('/api/servers/%s/backup', $this->server->uuid),
42
[
43
'json' => [
44
'adapter' => $this->adapter ?? config('backups.default'),
45
'uuid' => $backup->uuid,
46
'ignore' => implode("\n", $backup->ignored_files),
47
],
48
]
49
);
50
} catch (TransferException $exception) {
51
throw new DaemonConnectionException($exception);
52
}
53
}
54
55
/**
56
* Sends a request to Wings to begin restoring a backup for a server.
57
*
58
* @throws DaemonConnectionException
59
*/
60
public function restore(Backup $backup, ?string $url = null, bool $truncate = false): ResponseInterface
61
{
62
Assert::isInstanceOf($this->server, Server::class);
63
64
try {
65
return $this->getHttpClient()->post(
66
sprintf('/api/servers/%s/backup/%s/restore', $this->server->uuid, $backup->uuid),
67
[
68
'json' => [
69
'adapter' => $backup->disk,
70
'truncate_directory' => $truncate,
71
'download_url' => $url ?? '',
72
],
73
]
74
);
75
} catch (TransferException $exception) {
76
throw new DaemonConnectionException($exception);
77
}
78
}
79
80
/**
81
* Deletes a backup from the daemon.
82
*
83
* @throws DaemonConnectionException
84
*/
85
public function delete(Backup $backup): ResponseInterface
86
{
87
Assert::isInstanceOf($this->server, Server::class);
88
89
try {
90
return $this->getHttpClient()->delete(
91
sprintf('/api/servers/%s/backup/%s', $this->server->uuid, $backup->uuid)
92
);
93
} catch (TransferException $exception) {
94
throw new DaemonConnectionException($exception);
95
}
96
}
97
}
98
99