From 951d92b1433dd06a4d948431a99500b396c85e19 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 26 Dec 2020 11:59:21 -0700 Subject: [PATCH] Store S3 upload_id in the database for backups --- .../Backups/BackupRemoteUploadController.php | 7 +++- .../Remote/Backups/BackupStatusController.php | 2 +- app/Models/Backup.php | 5 ++- ..._add_upload_id_column_to_backups_table.php | 32 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2020_12_26_184914_add_upload_id_column_to_backups_table.php diff --git a/app/Http/Controllers/Api/Remote/Backups/BackupRemoteUploadController.php b/app/Http/Controllers/Api/Remote/Backups/BackupRemoteUploadController.php index 6d50ad64d..f8a758c1e 100644 --- a/app/Http/Controllers/Api/Remote/Backups/BackupRemoteUploadController.php +++ b/app/Http/Controllers/Api/Remote/Backups/BackupRemoteUploadController.php @@ -47,6 +47,7 @@ class BackupRemoteUploadController extends Controller * @return \Illuminate\Http\JsonResponse * * @throws \Exception + * @throws \Throwable * @throws \Illuminate\Database\Eloquent\ModelNotFoundException */ public function __invoke(Request $request, string $backup) @@ -101,8 +102,12 @@ class BackupRemoteUploadController extends Controller )->getUri()->__toString(); } - return new JsonResponse([ + // Set the upload_id on the backup in the database. + $backup->forceFill([ 'upload_id' => $params['UploadId'], + ])->saveOrFail(); + + return new JsonResponse([ 'parts' => $parts, 'part_size' => self::PART_SIZE, ]); diff --git a/app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php b/app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php index ba98b56f9..18901dba6 100644 --- a/app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php +++ b/app/Http/Controllers/Api/Remote/Backups/BackupStatusController.php @@ -78,7 +78,7 @@ class BackupStatusController extends Controller $params = [ 'Bucket' => $adapter->getBucket(), 'Key' => sprintf('%s/%s.tar.gz', $backup->server->uuid, $backup->uuid), - 'UploadId' => $request->input('upload_id'), + 'UploadId' => $backup->upload_id, ]; // If the backup was not successful, send an AbortMultipartUpload request. diff --git a/app/Models/Backup.php b/app/Models/Backup.php index 729342397..5f784ba64 100644 --- a/app/Models/Backup.php +++ b/app/Models/Backup.php @@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @property string $disk * @property string|null $checksum * @property int $bytes + * @property string|null $upload_id * @property \Carbon\CarbonImmutable|null $completed_at * @property \Carbon\CarbonImmutable $created_at * @property \Carbon\CarbonImmutable $updated_at @@ -46,8 +47,8 @@ class Backup extends Model protected $casts = [ 'id' => 'int', 'is_successful' => 'bool', - 'bytes' => 'int', 'ignored_files' => 'array', + 'bytes' => 'int', ]; /** @@ -64,6 +65,7 @@ class Backup extends Model 'is_successful' => true, 'checksum' => null, 'bytes' => 0, + 'upload_id' => null, ]; /** @@ -78,6 +80,7 @@ class Backup extends Model 'disk' => 'required|string', 'checksum' => 'nullable|string', 'bytes' => 'numeric', + 'upload_id' => 'nullable|uuid', ]; /** diff --git a/database/migrations/2020_12_26_184914_add_upload_id_column_to_backups_table.php b/database/migrations/2020_12_26_184914_add_upload_id_column_to_backups_table.php new file mode 100644 index 000000000..989670f50 --- /dev/null +++ b/database/migrations/2020_12_26_184914_add_upload_id_column_to_backups_table.php @@ -0,0 +1,32 @@ +char('upload_id', 36)->nullable()->after('bytes'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('backups', function (Blueprint $table) { + $table->dropColumn('upload_id'); + }); + } +}