Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

implement backup config via api

DanielHemmati5dff22d34551
diff --git a/app/Http/Controllers/Api/DatabasesController.php b/app/Http/Controllers/Api/DatabasesController.php
index de8daa43ef..ab0191581e 100644
--- a/app/Http/Controllers/Api/DatabasesController.php
+++ b/app/Http/Controllers/Api/DatabasesController.php
@@ -288,6 +288,19 @@ class DatabasesController extends Controller
'mysql_user' => ['type' => 'string', 'description' => 'MySQL user'],
'mysql_database' => ['type' => 'string', 'description' => 'MySQL database'],
'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)
{
+ $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'];
$teamId = getTeamIdFromToken();
if (is_null($teamId)) {
return invalidTokenResponse();
}
+ // this check if the request is a valid json
$return = validateIncomingRequest($request);
if ($return instanceof \Illuminate\Http\JsonResponse) {
return $return;
@@ -336,6 +351,18 @@ class DatabasesController extends Controller
'limits_cpus' => 'string',
'limits_cpuset' => 'string|nullable',
'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()) {
@@ -347,6 +374,7 @@ class DatabasesController extends Controller
$uuid = $request->uuid;
removeUnnecessaryFieldsFromRequest($request);
$database = queryDatabaseByUuidWithinTeam($uuid, $teamId);
+ $backupConfig = ScheduledDatabaseBackup::where('database_id', $database->id)->first();
if (! $database) {
return response()->json(['message' => 'Database not found.'], 404);
}
@@ -545,7 +573,7 @@ class DatabasesController extends Controller
}
break;
}
- $extraFields = array_diff(array_keys($request->all()), $allowedFields);
+ $extraFields = array_diff(array_keys($request->all()), $allowedFields, $allowedBackupConfigsFields);
if ($validator->fails() || ! empty($extraFields)) {
$errors = $validator->errors();
if (! empty($extraFields)) {
@@ -567,7 +595,36 @@ class DatabasesController extends Controller
$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') {
StartDatabaseProxy::dispatch($database);
diff --git a/routes/api.php b/routes/api.php
index 409dd393f0..326399f30e 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -23,9 +23,6 @@ Route::group([
});
Route::post('/feedback', [OtherController::class, 'feedback']);
-Route::get('/test', function () {
- return response()->json(['message' => 'test']);
-});
Route::group([
'middleware' => ['auth:sanctum', 'api.ability:write'],