implement backup config via api

This commit is contained in:
DanielHemmati
2025-04-24 16:48:08 +02:00
parent da487f609a
commit 5dff22d345
2 changed files with 59 additions and 5 deletions

View File

@@ -288,6 +288,19 @@ class DatabasesController extends Controller
'mysql_user' => ['type' => 'string', 'description' => 'MySQL user'], 'mysql_user' => ['type' => 'string', 'description' => 'MySQL user'],
'mysql_database' => ['type' => 'string', 'description' => 'MySQL database'], 'mysql_database' => ['type' => 'string', 'description' => 'MySQL database'],
'mysql_conf' => ['type' => 'string', 'description' => 'MySQL conf'], 'mysql_conf' => ['type' => 'string', 'description' => 'MySQL conf'],
// WIP
'save_s3' => ['type' => 'boolean', 'description' => 'Weather data is saved in s3 or not'],
's3_storage_id' => ['type' => 'integer', 'description' => 'S3 storage id'],
'enabled' => ['type' => 'boolean', 'description' => 'Weather the backup is enabled or not'],
'databases_to_backup' => ['type' => 'string', 'description' => 'Comma separated list of databases to backup'],
'dump_all' => ['type' => 'boolean', 'description' => 'Weather all databases are dumped or not'],
'frequency' => ['type' => 'string', 'description' => 'Frequency of the backup'],
'database_backup_retention_amount_locally' => ['type' => 'integer', 'description' => 'Retention amount of the backup locally'],
'database_backup_retention_days_locally' => ['type' => 'integer', 'description' => 'Retention days of the backup locally'],
'database_backup_retention_max_storage_locally' => ['type' => 'integer', 'description' => 'Max storage of the backup locally'],
'database_backup_retention_amount_s3' => ['type' => 'integer', 'description' => 'Retention amount of the backup in s3'],
'database_backup_retention_days_s3' => ['type' => 'integer', 'description' => 'Retention days of the backup in s3'],
'database_backup_retention_max_storage_s3' => ['type' => 'integer', 'description' => 'Max storage of the backup locally'],
], ],
), ),
) )
@@ -313,12 +326,14 @@ class DatabasesController extends Controller
)] )]
public function update_by_uuid(Request $request) public function update_by_uuid(Request $request)
{ {
$allowedBackupConfigsFields = ['save_s3', 'enabled', 'dump_all', 'frequency', 'databases_to_backup', 'database_backup_retention_amount_locally', 'database_backup_retention_days_locally', 'database_backup_retention_max_storage_locally', 'database_backup_retention_amount_s3', 'database_backup_retention_days_s3', 'database_backup_retention_max_storage_s3', 's3_storage_id'];
$allowedFields = ['name', 'description', 'image', 'public_port', 'is_public', 'instant_deploy', 'limits_memory', 'limits_memory_swap', 'limits_memory_swappiness', 'limits_memory_reservation', 'limits_cpus', 'limits_cpuset', 'limits_cpu_shares', 'postgres_user', 'postgres_password', 'postgres_db', 'postgres_initdb_args', 'postgres_host_auth_method', 'postgres_conf', 'clickhouse_admin_user', 'clickhouse_admin_password', 'dragonfly_password', 'redis_password', 'redis_conf', 'keydb_password', 'keydb_conf', 'mariadb_conf', 'mariadb_root_password', 'mariadb_user', 'mariadb_password', 'mariadb_database', 'mongo_conf', 'mongo_initdb_root_username', 'mongo_initdb_root_password', 'mongo_initdb_database', 'mysql_root_password', 'mysql_password', 'mysql_user', 'mysql_database', 'mysql_conf']; $allowedFields = ['name', 'description', 'image', 'public_port', 'is_public', 'instant_deploy', 'limits_memory', 'limits_memory_swap', 'limits_memory_swappiness', 'limits_memory_reservation', 'limits_cpus', 'limits_cpuset', 'limits_cpu_shares', 'postgres_user', 'postgres_password', 'postgres_db', 'postgres_initdb_args', 'postgres_host_auth_method', 'postgres_conf', 'clickhouse_admin_user', 'clickhouse_admin_password', 'dragonfly_password', 'redis_password', 'redis_conf', 'keydb_password', 'keydb_conf', 'mariadb_conf', 'mariadb_root_password', 'mariadb_user', 'mariadb_password', 'mariadb_database', 'mongo_conf', 'mongo_initdb_root_username', 'mongo_initdb_root_password', 'mongo_initdb_database', 'mysql_root_password', 'mysql_password', 'mysql_user', 'mysql_database', 'mysql_conf'];
$teamId = getTeamIdFromToken(); $teamId = getTeamIdFromToken();
if (is_null($teamId)) { if (is_null($teamId)) {
return invalidTokenResponse(); return invalidTokenResponse();
} }
// this check if the request is a valid json
$return = validateIncomingRequest($request); $return = validateIncomingRequest($request);
if ($return instanceof \Illuminate\Http\JsonResponse) { if ($return instanceof \Illuminate\Http\JsonResponse) {
return $return; return $return;
@@ -336,6 +351,18 @@ class DatabasesController extends Controller
'limits_cpus' => 'string', 'limits_cpus' => 'string',
'limits_cpuset' => 'string|nullable', 'limits_cpuset' => 'string|nullable',
'limits_cpu_shares' => 'numeric', 'limits_cpu_shares' => 'numeric',
'save_s3' => 'boolean',
'enabled' => 'boolean',
'dump_all' => 'boolean',
's3_storage_id' => 'integer|min:1|exists:s3_storages,id|nullable',
'databases_to_backup' => 'string',
'frequency' => 'string|in:every_minute,hourly,daily,weekly,monthly,yearly',
'database_backup_retention_amount_locally' => 'integer|min:0',
'database_backup_retention_days_locally' => 'integer|min:0',
'database_backup_retention_max_storage_locally' => 'integer|min:0',
'database_backup_retention_amount_s3' => 'integer|min:0',
'database_backup_retention_days_s3' => 'integer|min:0',
'database_backup_retention_max_storage_s3' => 'integer|min:0',
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
@@ -347,6 +374,7 @@ class DatabasesController extends Controller
$uuid = $request->uuid; $uuid = $request->uuid;
removeUnnecessaryFieldsFromRequest($request); removeUnnecessaryFieldsFromRequest($request);
$database = queryDatabaseByUuidWithinTeam($uuid, $teamId); $database = queryDatabaseByUuidWithinTeam($uuid, $teamId);
$backupConfig = ScheduledDatabaseBackup::where('database_id', $database->id)->first();
if (! $database) { if (! $database) {
return response()->json(['message' => 'Database not found.'], 404); return response()->json(['message' => 'Database not found.'], 404);
} }
@@ -545,7 +573,7 @@ class DatabasesController extends Controller
} }
break; break;
} }
$extraFields = array_diff(array_keys($request->all()), $allowedFields); $extraFields = array_diff(array_keys($request->all()), $allowedFields, $allowedBackupConfigsFields);
if ($validator->fails() || ! empty($extraFields)) { if ($validator->fails() || ! empty($extraFields)) {
$errors = $validator->errors(); $errors = $validator->errors();
if (! empty($extraFields)) { if (! empty($extraFields)) {
@@ -567,7 +595,36 @@ class DatabasesController extends Controller
$whatToDoWithDatabaseProxy = 'start'; $whatToDoWithDatabaseProxy = 'start';
} }
$database->update($request->all()); $backupPayload = $request->only($allowedBackupConfigsFields);
$databasePayload = $request->only($allowedFields);
if ($databasePayload) {
$database->update($databasePayload);
}
if ($backupPayload && ! $backupConfig) {
if ($database->type() === 'standalone-postgresql') {
$backupPayload['databases_to_backup'] = $database->postgres_db;
} elseif ($database->type() === 'standalone-mysql') {
$backupPayload['databases_to_backup'] = $database->mysql_database;
} elseif ($database->type() === 'standalone-mariadb') {
$backupPayload['databases_to_backup'] = $database->mariadb_database;
} elseif ($database->type() === 'standalone-mongodbs') {
$backupPayload['databases_to_backup'] = $database->mongo_initdb_database;
}
$backupConfig = ScheduledDatabaseBackup::create([
'database_id' => $database->id,
'database_type' => $database->getMorphClass(),
'team_id' => $teamId,
's3_storage_id' => $backupPayload['s3_storage_id'] ?? 1,
...$backupPayload,
]);
}
if ($backupPayload && $backupConfig) {
$backupConfig->update($backupPayload);
}
if ($whatToDoWithDatabaseProxy === 'start') { if ($whatToDoWithDatabaseProxy === 'start') {
StartDatabaseProxy::dispatch($database); StartDatabaseProxy::dispatch($database);

View File

@@ -23,9 +23,6 @@ Route::group([
}); });
Route::post('/feedback', [OtherController::class, 'feedback']); Route::post('/feedback', [OtherController::class, 'feedback']);
Route::get('/test', function () {
return response()->json(['message' => 'test']);
});
Route::group([ Route::group([
'middleware' => ['auth:sanctum', 'api.ability:write'], 'middleware' => ['auth:sanctum', 'api.ability:write'],