init: scheduled backups

This commit is contained in:
Andras Bacsai 2023-08-08 17:28:36 +02:00
parent b4b1c671bd
commit 46909dca85
11 changed files with 159 additions and 4 deletions

View File

@ -2,12 +2,14 @@
namespace App\Console;
use App\Jobs\BackupDatabaseJob;
use App\Jobs\CheckResaleLicenseJob;
use App\Jobs\CheckResaleLicenseKeys;
use App\Jobs\DockerCleanupJob;
use App\Jobs\InstanceApplicationsStatusJob;
use App\Jobs\InstanceAutoUpdateJob;
use App\Jobs\ProxyCheckJob;
use App\Models\ScheduledDatabaseBackup;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -15,10 +17,12 @@ class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
// $schedule->call(fn() => $this->check_scheduled_backups($schedule))->everyTenSeconds();
if (isDev()) {
$schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new InstanceApplicationsStatusJob)->everyMinute();
$schedule->job(new ProxyCheckJob)->everyFiveMinutes();
// $schedule->job(new CheckResaleLicenseJob)->hourly();
// $schedule->job(new DockerCleanupJob)->everyOddHour();
// $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
@ -30,6 +34,24 @@ protected function schedule(Schedule $schedule): void
$schedule->job(new DockerCleanupJob)->everyTenMinutes();
$schedule->job(new InstanceAutoUpdateJob)->everyTenMinutes();
}
$this->check_scheduled_backups($schedule);
}
private function check_scheduled_backups($schedule)
{
ray('check_scheduled_backups');
$scheduled_backups = ScheduledDatabaseBackup::all();
if ($scheduled_backups->isEmpty()) {
ray('no scheduled backups');
return;
}
foreach ($scheduled_backups as $scheduled_backup) {
if (!$scheduled_backup->enabled) continue;
$schedule->job(new BackupDatabaseJob(
backup: $scheduled_backup
))->cron($scheduled_backup->frequency);
}
}
protected function commands(): void

View File

@ -11,6 +11,7 @@
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Throwable;
class Controller extends BaseController
{
@ -43,7 +44,6 @@ public function dashboard()
$s3s = S3Storage::ownedByCurrentTeam()->get();
$resources = 0;
foreach ($projects as $project) {
ray($project->postgresqls);
$resources += $project->applications->count();
$resources += $project->postgresqls->count();
}
@ -140,7 +140,7 @@ public function acceptInvitation()
$invitation->delete();
abort(401);
}
} catch (\Throwable $th) {
} catch (Throwable $th) {
throw $th;
}
}
@ -158,7 +158,7 @@ public function revokeInvitation()
}
$invitation->delete();
return redirect()->route('team.show');
} catch (\Throwable $th) {
} catch (Throwable $th) {
throw $th;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace App\Http\Livewire\Dev;
use App\Models\ScheduledDatabaseBackup;
use Livewire\Component;
class ScheduledBackups extends Component
{
public $scheduledDatabaseBackup;
public function mount()
{
$this->scheduledDatabaseBackup = ScheduledDatabaseBackup::all();
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class BackupDatabaseJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(public $backup)
{
}
public function handle()
{
ray('BackupDatabaseJob');
ray($this->backup);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ScheduledDatabaseBackup extends Model
{
protected $guarded = [];
public function database()
{
return $this->morphTo();
}
}

View File

@ -64,6 +64,11 @@ public function destination()
return $this->morphTo();
}
public function scheduled_database_backups()
{
return $this->morphMany(ScheduledDatabaseBackup::class, 'database');
}
public function environment_variables(): HasMany
{
return $this->hasMany(EnvironmentVariable::class);

View File

@ -2,6 +2,7 @@
namespace App\Notifications\Channels;
use Exception;
use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Mail;
@ -15,7 +16,7 @@ public function send(SendsEmail $notifiable, Notification $notification): void
$recepients = $notifiable->getRecepients($notification);
if (count($recepients) === 0) {
throw new \Exception('No email recipients found');
throw new Exception('No email recipients found');
}
$mailMessage = $notification->toMail($notifiable);

View File

@ -0,0 +1,24 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('scheduled_database_backups', function (Blueprint $table) {
$table->id();
$table->boolean('enabled')->default(true);
$table->string('frequency');
$table->morphs('database');
$table->foreignId('team_id');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('scheduled_database_backups');
}
};

View File

@ -0,0 +1,34 @@
<?php
namespace Database\Seeders;
use App\Models\ScheduledDatabaseBackup;
use Illuminate\Database\Seeder;
class ScheduledDatabaseBackupSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
ScheduledDatabaseBackup::create([
'frequency' => '* * * * *',
'database_id' => 1,
'database_type' => 'App\Models\StandalonePostgresql',
'team_id' => 0,
]);
ScheduledDatabaseBackup::create([
'frequency' => '*/2 * * * *',
'database_id' => 1,
'database_type' => 'App\Models\StandalonePostgresql',
'team_id' => 0,
]);
ScheduledDatabaseBackup::create([
'frequency' => '*/3 * * * *',
'database_id' => 1,
'database_type' => 'App\Models\StandalonePostgresql',
'team_id' => 0,
]);
}
}

View File

@ -24,5 +24,6 @@
</div>
@if (isDev())
<livewire:dev.s3-test/>
<livewire:dev.scheduled-backups/>
@endif
</x-layout>

View File

@ -0,0 +1,13 @@
<div>
<h2>Scheduled Databse Backups</h2>
@foreach($scheduledDatabaseBackup as $backup)
<div>
{{$backup->id}}
{{$backup->database->id}}
{{$backup->frequency}}
{{$backup->database->type()}}
{{$backup->created_at}}
{{$backup->updated_at}}
</div>
@endforeach
</div>