init: scheduled backups
This commit is contained in:
		@@ -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 @@ class Kernel extends ConsoleKernel
 | 
			
		||||
            $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
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,7 @@ use App\Models\User;
 | 
			
		||||
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 @@ class Controller extends BaseController
 | 
			
		||||
        $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 @@ class Controller extends BaseController
 | 
			
		||||
                $invitation->delete();
 | 
			
		||||
                abort(401);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (\Throwable $th) {
 | 
			
		||||
        } catch (Throwable $th) {
 | 
			
		||||
            throw $th;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -158,7 +158,7 @@ class Controller extends BaseController
 | 
			
		||||
            }
 | 
			
		||||
            $invitation->delete();
 | 
			
		||||
            return redirect()->route('team.show');
 | 
			
		||||
        } catch (\Throwable $th) {
 | 
			
		||||
        } catch (Throwable $th) {
 | 
			
		||||
            throw $th;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										16
									
								
								app/Http/Livewire/Dev/ScheduledBackups.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								app/Http/Livewire/Dev/ScheduledBackups.php
									
									
									
									
									
										Normal 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();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								app/Jobs/BackupDatabaseJob.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								app/Jobs/BackupDatabaseJob.php
									
									
									
									
									
										Normal 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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								app/Models/ScheduledDatabaseBackup.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								app/Models/ScheduledDatabaseBackup.php
									
									
									
									
									
										Normal 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();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -64,6 +64,11 @@ class StandalonePostgresql extends BaseModel
 | 
			
		||||
        return $this->morphTo();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function scheduled_database_backups()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->morphMany(ScheduledDatabaseBackup::class, 'database');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function environment_variables(): HasMany
 | 
			
		||||
    {
 | 
			
		||||
        return $this->hasMany(EnvironmentVariable::class);
 | 
			
		||||
 
 | 
			
		||||
@@ -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 @@ class EmailChannel
 | 
			
		||||
        $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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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');
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										34
									
								
								database/seeders/ScheduledDatabaseBackupSeeder.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								database/seeders/ScheduledDatabaseBackupSeeder.php
									
									
									
									
									
										Normal 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,
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -24,5 +24,6 @@
 | 
			
		||||
    </div>
 | 
			
		||||
    @if (isDev())
 | 
			
		||||
        <livewire:dev.s3-test/>
 | 
			
		||||
        <livewire:dev.scheduled-backups/>
 | 
			
		||||
    @endif
 | 
			
		||||
</x-layout>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										13
									
								
								resources/views/livewire/dev/scheduled-backups.blade.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								resources/views/livewire/dev/scheduled-backups.blade.php
									
									
									
									
									
										Normal 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>
 | 
			
		||||
		Reference in New Issue
	
	Block a user