Repository navigation
feat(backups): download local backup archives from the files page #1283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mstfkhazaal
wants to merge
9
commits into
vitodeploy:4.x
Choose a base branch
from
fnx-software:feat/download-local-backup-files
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43eb994
feat(backups): introduce streaming download support and coverage
mstfkhazaal 4056f10
feat(backups): introduce streaming download support and coverage
mstfkhazaal af7ecb8
feat(backups): enable secure local backup downloads
mstfkhazaal da58cca
feat(backups): enable secure local backup downloads
mstfkhazaal e8b2e29
feat(backups): enable secure local backup downloads
mstfkhazaal 0a4e6aa
feat(backups): enable secure local backup downloads
mstfkhazaal f4c7277
feat(backups): enable secure local backup downloads
mstfkhazaal b83ec09
fix(backups): accept successful SFTP callback return values
mstfkhazaal ced8d6b
fix(backups): avoid assuming backup relation is present in realtime rows
mstfkhazaal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| namespace App\Actions\Backup; | ||
|
|
||
| use App\Enums\BackupFileStatus; | ||
| use App\Models\BackupFile; | ||
| use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
|
||
| class DownloadBackupFile | ||
| { | ||
| public function run(BackupFile $backupFile): StreamedResponse | ||
| { | ||
| abort_unless($backupFile->isLocal(), 404, 'Downloading from this storage provider is not supported.'); | ||
|
|
||
| abort_unless(in_array($backupFile->status, [ | ||
| BackupFileStatus::CREATED, | ||
| BackupFileStatus::RESTORED, | ||
| BackupFileStatus::RESTORE_FAILED, | ||
| ], true), 409, 'This backup file is not available for download.'); | ||
|
|
||
| $path = $backupFile->path(); | ||
| $ssh = $backupFile->backup->server->ssh(); | ||
|
|
||
| abort_unless($ssh->isRegularFile($path), 404, 'The backup file does not exist on the server.'); | ||
|
|
||
| return response()->streamDownload( | ||
| static function () use ($ssh, $path): void { | ||
| $ssh->streamDownload($path, static function (string $chunk): void { | ||
| echo $chunk; | ||
| flush(); | ||
| }); | ||
|
mstfkhazaal marked this conversation as resolved.
Resolved
|
||
| }, | ||
| basename($path), | ||
| [ | ||
| 'Content-Type' => 'application/octet-stream', | ||
| 'Cache-Control' => 'private, no-store, no-cache, must-revalidate', | ||
| 'X-Content-Type-Options' => 'nosniff', | ||
| 'X-Accel-Buffering' => 'no', | ||
| ], | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| <?php | ||
|
|
||
| use App\Enums\BackupFileStatus; | ||
| use App\Enums\BackupType; | ||
| use App\Facades\SSH; | ||
| use App\Models\Backup; | ||
| use App\Models\BackupFile; | ||
| use App\Models\Database; | ||
| use App\Models\Server; | ||
| use App\Models\StorageProvider; | ||
| use App\Models\User; | ||
| use App\StorageProviders\Local; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
|
||
| uses(RefreshDatabase::class); | ||
|
|
||
| beforeEach(function () { | ||
| $storage = StorageProvider::factory()->create([ | ||
| 'user_id' => $this->user->id, | ||
| 'provider' => Local::id(), | ||
| 'credentials' => ['path' => '/home/vito/backups'], | ||
| ]); | ||
|
|
||
| $this->backup = Backup::factory()->create([ | ||
| 'type' => BackupType::FILE, | ||
| 'server_id' => $this->server->id, | ||
| 'storage_id' => $storage->id, | ||
| 'path' => '/home/vito/example.com', | ||
| ]); | ||
|
|
||
| $this->file = BackupFile::factory()->create([ | ||
| 'backup_id' => $this->backup->id, | ||
| 'name' => 'example-20261028065718', | ||
| 'status' => BackupFileStatus::CREATED, | ||
| ]); | ||
| }); | ||
|
|
||
| function backupDownloadUrl(Server $server, Backup $backup, BackupFile $file): string | ||
| { | ||
| return route('backup-files.download', [ | ||
| 'server' => $server, | ||
| 'backup' => $backup, | ||
| 'backupFile' => $file, | ||
| ]); | ||
| } | ||
|
|
||
| test('downloads a local file backup as a streamed attachment', function () { | ||
| SSH::fake('test archive bytes'); | ||
|
|
||
| $response = $this->actingAs($this->user) | ||
| ->get(backupDownloadUrl($this->server, $this->backup, $this->file)) | ||
| ->assertOk() | ||
| ->assertHeader('content-type', 'application/octet-stream') | ||
| ->assertHeader('x-content-type-options', 'nosniff'); | ||
|
|
||
| expect($response->headers->get('content-disposition')) | ||
| ->toContain('attachment') | ||
| ->toContain('example-20261028065718.tar.gz'); | ||
| expect($response->streamedContent())->toBe('test archive bytes'); | ||
|
|
||
| SSH::assertExecutedContains('sftp:stream /home/vito/backups/example.com/example-20261028065718.tar.gz'); | ||
| }); | ||
|
|
||
| test('downloads a local database backup with sql.gz extension', function () { | ||
| SSH::fake('sql gzip bytes'); | ||
|
|
||
| $database = Database::factory()->create(['server_id' => $this->server->id, 'name' => 'test_db']); | ||
| $this->backup->update([ | ||
| 'type' => BackupType::DATABASE, | ||
| 'database_id' => $database->id, | ||
| 'path' => null, | ||
| ]); | ||
|
|
||
| $response = $this->actingAs($this->user) | ||
| ->get(backupDownloadUrl($this->server, $this->backup, $this->file)) | ||
| ->assertOk(); | ||
|
|
||
| expect($response->headers->get('content-disposition'))->toContain('.sql.gz'); | ||
| expect($response->streamedContent())->toBe('sql gzip bytes'); | ||
| SSH::assertExecutedContains('sftp:stream /home/vito/backups/test_db/example-20261028065718.sql.gz'); | ||
| }); | ||
|
|
||
| test('download fails when the remote backup archive is missing', function () { | ||
| SSH::fake()->remoteFileWillBeMissing(); | ||
|
|
||
| $this->actingAs($this->user) | ||
| ->get(backupDownloadUrl($this->server, $this->backup, $this->file)) | ||
| ->assertNotFound(); | ||
| }); | ||
|
|
||
| test('download rejects unavailable backup files', function (BackupFileStatus $status) { | ||
| SSH::fake(); | ||
| $this->file->update(['status' => $status]); | ||
|
|
||
| $this->actingAs($this->user) | ||
| ->get(backupDownloadUrl($this->server, $this->backup, $this->file)) | ||
| ->assertStatus(409); | ||
| })->with([ | ||
| BackupFileStatus::CREATING, | ||
| BackupFileStatus::FAILED, | ||
| BackupFileStatus::RESTORING, | ||
| BackupFileStatus::DELETING, | ||
| BackupFileStatus::DELETE_FAILED, | ||
| ]); | ||
|
|
||
| test('download is unavailable for unsupported storage providers', function () { | ||
| SSH::fake(); | ||
|
|
||
| $storage = StorageProvider::factory()->dropbox()->create(['user_id' => $this->user->id]); | ||
| $this->backup->update(['storage_id' => $storage->id]); | ||
|
|
||
| $this->actingAs($this->user) | ||
| ->get(backupDownloadUrl($this->server, $this->backup, $this->file)) | ||
| ->assertNotFound(); | ||
| }); | ||
|
|
||
| test('download checks the server and backup file relationship', function () { | ||
| SSH::fake(); | ||
|
|
||
| $otherBackup = Backup::factory()->create([ | ||
| 'server_id' => $this->server->id, | ||
| 'storage_id' => $this->backup->storage_id, | ||
| 'type' => BackupType::FILE, | ||
| 'path' => '/home/vito/other.com', | ||
| ]); | ||
|
|
||
| $this->actingAs($this->user) | ||
| ->get(backupDownloadUrl($this->server, $otherBackup, $this->file)) | ||
| ->assertNotFound(); | ||
| }); | ||
|
|
||
| test('download enforces project membership', function () { | ||
| SSH::fake(); | ||
|
|
||
| $otherUser = User::factory()->create(); | ||
| $otherUser->ensureHasDefaultProject(); | ||
|
|
||
| $this->actingAs($otherUser) | ||
| ->get(backupDownloadUrl($this->server, $this->backup, $this->file)) | ||
| ->assertForbidden(); | ||
| }); | ||
|
|
||
| test('guest cannot download a backup file', function () { | ||
| SSH::fake(); | ||
|
|
||
| $this->get(backupDownloadUrl($this->server, $this->backup, $this->file)) | ||
| ->assertRedirect(); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.