This commit is contained in:
Bozhidar 2024-05-02 17:51:26 +03:00
parent e1d7d50a1f
commit 98efa1b42c
6 changed files with 100 additions and 39 deletions

72
web/app/BackupStorage.php Normal file
View file

@ -0,0 +1,72 @@
<?php
namespace App;
use App\Models\Domain;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
class BackupStorage
{
public static function getDomainPath($domainId)
{
$findDomain = Domain::where('id', $domainId)->first();
if ($findDomain) {
return $findDomain->domain_root . '/backups';
}
return false;
}
public static function getDomainInstance($domainId)
{
$domainPath = self::getDomainPath($domainId);
if ($domainPath) {
$storageBuild = Storage::build([
'driver' => 'local',
'root' => $domainPath,
]);
$storageBuild->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
return URL::temporarySignedRoute(
'backup.download',
$expiration,
array_merge($options, ['path' => $path])
);
});
return $storageBuild;
}
return false;
}
public static function getPath()
{
$rootPath = storage_path('app');
$customBackupPath = setting('general.backup_path');
if (!empty($customBackupPath)) {
$rootPath = $customBackupPath;
}
return $rootPath;
}
public static function getInstance()
{
$rootPath = self::getPath();
$storageBuild = Storage::build([
'driver' => 'local',
'throw' => false,
'root' => $rootPath,
]);
$storageBuild->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
return URL::temporarySignedRoute(
'backup.download',
$expiration,
array_merge($options, ['path' => $path])
);
});
return $storageBuild;
}
}

View file

@ -10,7 +10,9 @@ use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Illuminate\Support\Facades\Storage;
use Outerweb\FilamentSettings\Filament\Pages\Settings as BaseSettings;
use Symfony\Component\Console\Input\Input;
class Settings extends BaseSettings
{
@ -29,6 +31,7 @@ class Settings extends BaseSettings
public function schema(): array|Closure
{
return [
Tabs::make('Settings')
->schema([
@ -57,6 +60,12 @@ class Settings extends BaseSettings
Textarea::make('general.domain_suspend_page_html'),
Textarea::make('general.domain_created_page_html'),
]),
Tabs\Tab::make('Backups')
->schema([
TextInput::make('general.backup_path')
->default(Storage::path('backups'))
]),
]),
];
}

View file

@ -2,12 +2,14 @@
namespace App\Models;
use App\BackupStorage;
use App\Filament\Enums\BackupStatus;
use App\Helpers;
use App\ShellApi;
use Dotenv\Dotenv;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Jackiedo\DotenvEditor\Facades\DotenvEditor;
@ -179,20 +181,11 @@ class Backup extends Model
];
}
$storagePath = Storage::path('backups');
if (! is_dir($storagePath)) {
mkdir($storagePath);
}
$storagePath = BackupStorage::getPath();
$backupPath = $storagePath.'/'.$this->id;
if (!is_dir(dirname($backupPath))) {
mkdir(dirname($backupPath));
}
if (! is_dir($backupPath)) {
mkdir($backupPath);
}
$backupTempPath = $backupPath.'/temp';
if (! is_dir($backupTempPath)) {
mkdir($backupTempPath);
shell_exec('mkdir -p '.$backupTempPath);
}
$backupFilename = 'phyre-backup-'.date('Ymd-His').'.zip';
@ -272,11 +265,16 @@ class Backup extends Model
if ($processId > 0 && is_numeric($processId)) {
$this->path = $backupPath;
$this->filepath = $backupFilename;
$this->root_path = $storagePath;
$this->temp_path = $backupTempPath;
$this->file_path = $backupFilePath;
$this->file_name = $backupFilename;
$this->status = 'processing';
$this->queued = true;
$this->queued_at = now();
$this->process_id = $processId;
$this->save();
return [

View file

@ -2,6 +2,7 @@
namespace App\Providers;
use App\BackupStorage;
use App\Events\ModelDomainCreated;
use App\Events\ModelDomainDeleting;
use App\Events\ModelHostingSubscriptionCreated;
@ -16,7 +17,9 @@ use App\Models\HostingSubscription;
use App\Policies\CustomerPolicy;
use BladeUI\Icons\Factory;
use Filament\Facades\Filament;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;
@ -32,15 +35,6 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
// This allows us to generate a temporary url for backups downloading
Storage::disk('backups')->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
return URL::temporarySignedRoute(
'backup.download',
$expiration,
array_merge($options, ['path' => $path])
);
});
// Register Phyre Icons set
$this->callAfterResolving(Factory::class, function (Factory $factory) {
$factory->add('phyre', [
@ -72,16 +66,5 @@ class AppServiceProvider extends ServiceProvider
Gate::define('delete-customer', [CustomerPolicy::class, 'delete']);
if (is_file(storage_path('installed'))) {
$getDomains = Domain::all();
if ($getDomains->count() > 0) {
foreach ($getDomains as $domain) {
$this->app['config']["filesystems.disks.backups_" . Str::slug($domain->domain)] = [
'driver' => 'local',
'root' => $domain->domain_root . '/backups',
];
}
}
}
}
}

View file

@ -44,12 +44,6 @@ return [
'throw' => false,
],
'backups' => [
'driver' => 'local',
'root' => storage_path('app/backups'),
'throw' => false,
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),

View file

@ -16,8 +16,13 @@ return new class extends Migration
$table->string('backup_type')->nullable();
$table->string('status')->nullable();
$table->string('path')->nullable();
$table->string('filepath')->nullable();
$table->string('root_path')->nullable();
$table->string('temp_path')->nullable();
$table->string('file_path')->nullable();
$table->string('file_name')->nullable();
$table->string('size')->nullable();
$table->string('disk')->nullable();
$table->string('process_id')->nullable();