2.x - backups

This commit is contained in:
Saeed Vaziry
2024-09-29 17:54:11 +02:00
parent e4fed24498
commit 2e9620409b
35 changed files with 1093 additions and 122 deletions

View File

@ -7,7 +7,6 @@
use App\Models\Backup;
use App\Models\Server;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
@ -17,17 +16,15 @@ class CreateBackup
* @throws AuthorizationException
* @throws ValidationException
*/
public function create($type, Server $server, array $input): Backup
public function create(Server $server, array $input): Backup
{
$this->validate($type, $server, $input);
$backup = new Backup([
'type' => $type,
'type' => 'database',
'server_id' => $server->id,
'database_id' => $input['backup_database'] ?? null,
'storage_id' => $input['backup_storage'],
'interval' => $input['backup_interval'] == 'custom' ? $input['backup_custom'] : $input['backup_interval'],
'keep_backups' => $input['backup_keep'],
'database_id' => $input['database'] ?? null,
'storage_id' => $input['storage'],
'interval' => $input['interval'] == 'custom' ? $input['custom_interval'] : $input['interval'],
'keep_backups' => $input['keep'],
'status' => BackupStatus::RUNNING,
]);
$backup->save();
@ -37,45 +34,35 @@ public function create($type, Server $server, array $input): Backup
return $backup;
}
/**
* @throws ValidationException
*/
private function validate($type, Server $server, array $input): void
public static function rules(Server $server, array $input): array
{
$rules = [
'backup_storage' => [
'storage' => [
'required',
Rule::exists('storage_providers', 'id'),
],
'backup_keep' => [
'keep' => [
'required',
'numeric',
'min:1',
],
'backup_interval' => [
'interval' => [
'required',
Rule::in([
'0 * * * *',
'0 0 * * *',
'0 0 * * 0',
'0 0 1 * *',
'custom',
]),
Rule::in(array_keys(config('core.cronjob_intervals'))),
],
];
if ($input['backup_interval'] == 'custom') {
$rules['backup_custom'] = [
'required',
];
}
if ($type === 'database') {
$rules['backup_database'] = [
'database' => [
'required',
Rule::exists('databases', 'id')
->where('server_id', $server->id)
->where('status', DatabaseStatus::READY),
],
];
if ($input['interval'] == 'custom') {
$rules['custom_interval'] = [
'required',
];
}
Validator::make($input, $rules)->validate();
return $rules;
}
}