@@ -224,25 +224,35 @@ else {
224224 note (sprintf ('Using the latest backup ID %s for DB %s. ' , $ backup_id , $ db_name ));
225225
226226 task ('Discovering backup URL. ' );
227- $ backup_url_response = request_get (sprintf ('https://cloud.acquia.com/api/environments/%s/databases/%s/backups/%s/actions/download ' , $ env_id , $ db_name , $ backup_id ), dl_acquia_api_headers ($ token ));
227+ // The Acquia API responds with a 302 redirect to the final S3 download
228+ // URL. Capture the redirect URL without following it to avoid sending the
229+ // Authorization header to S3 - the auth header is required only to query
230+ // the Acquia API.
231+ $ backup_url_response = request (sprintf ('https://cloud.acquia.com/api/environments/%s/databases/%s/backups/%s/actions/download ' , $ env_id , $ db_name , $ backup_id ), ['method ' => 'GET ' , 'headers ' => dl_acquia_api_headers ($ token ), 'follow_redirects ' => FALSE ]);
228232
229233 if (!$ backup_url_response ['ok ' ]) {
230- fail ('Failed to retrieve backup URL for backup ID \'%s \'. ' , $ backup_id );
234+ fail (sprintf (
235+ 'Unable to discover backup URL for backup ID \'%s \' (status: %s, error: %s). ' ,
236+ $ backup_id ,
237+ (string ) ($ backup_url_response ['status ' ] ?? 'unknown ' ),
238+ (string ) ($ backup_url_response ['error ' ] ?? 'none ' )
239+ ));
231240 }
232241
233- $ backup_url_data = json_decode ((string ) $ backup_url_response ['body ' ], TRUE );
234- $ backup_url = $ backup_url_data ['url ' ] ?? '' ;
242+ $ backup_url = (string ) ($ backup_url_response ['info ' ]['redirect_url ' ] ?? '' );
235243
236244 if ($ backup_url === '' ) {
237245 fail (sprintf ('Unable to discover backup URL for backup ID \'%s \'. ' , $ backup_id ));
238246 }
239247
240248 task (sprintf ('Downloading DB dump into file %s. ' , $ file_name_compressed ));
241- $ dl_response = request ($ backup_url , ['method ' => 'GET ' , 'headers ' => dl_acquia_api_headers ( $ token ), ' save_to ' => $ file_name_compressed , 'timeout ' => 600 ]);
249+ $ dl_response = request ($ backup_url , ['method ' => 'GET ' , 'save_to ' => $ file_name_compressed , 'timeout ' => 600 ]);
242250 if (!$ dl_response ['ok ' ]) {
243251 fail ('Unable to download database %s. ' , $ db_name );
244252 }
245253
254+ // Check if the downloaded file exists and has content. Leave the file in
255+ // place on failure so it can be inspected.
246256 if (!file_exists ($ file_name_compressed ) || filesize ($ file_name_compressed ) === 0 ) {
247257 fail (sprintf ('Downloaded file is empty or missing: %s ' , $ file_name_compressed ));
248258 }
@@ -253,15 +263,15 @@ else {
253263
254264 task (sprintf ('Expanding DB file %s into %s. ' , $ file_name_compressed , $ file_name ));
255265
266+ // Test the gzip file first to ensure it's valid. Leave the file in place
267+ // on failure so it can be inspected.
256268 $ header = file_get_contents ($ file_name_compressed , FALSE , NULL , 0 , 2 );
257269 if ($ header === FALSE || $ header !== "\x1f\x8b" ) {
258- @unlink ($ file_name_compressed );
259270 fail (sprintf ('Downloaded file is not a valid gzip archive: %s ' , $ file_name_compressed ));
260271 }
261272
262273 $ gz_in = @fopen ('compress.zlib:// ' . $ file_name_compressed , 'rb ' );
263274 if ($ gz_in === FALSE ) {
264- @unlink ($ file_name_compressed );
265275 // @codeCoverageIgnoreStart
266276 fail (sprintf ('Unable to read compressed file %s. ' , $ file_name_compressed ));
267277 // @codeCoverageIgnoreEnd
@@ -270,7 +280,6 @@ else {
270280 $ out = @fopen ($ file_name , 'wb ' );
271281 if ($ out === FALSE ) {
272282 fclose ($ gz_in );
273- @unlink ($ file_name_compressed );
274283 // @codeCoverageIgnoreStart
275284 fail (sprintf ('Unable to write decompressed file %s. ' , $ file_name ));
276285 // @codeCoverageIgnoreEnd
@@ -280,17 +289,17 @@ else {
280289 fclose ($ gz_in );
281290 fclose ($ out );
282291
292+ // Check decompression result and file validity. Leave both files in place
293+ // on failure so they can be inspected.
283294 if ($ bytes === FALSE || $ bytes === 0 ) {
284- @unlink ($ file_name_compressed );
285- @unlink ($ file_name );
286295 fail (sprintf ('Downloaded file is not a valid gzip archive: %s ' , $ file_name_compressed ));
287296 }
288297
289- @unlink ($ file_name_compressed );
290-
291298 if (!file_exists ($ file_name ) || filesize ($ file_name ) === 0 ) {
292299 fail (sprintf ('Unable to process DB dump file "%s". ' , $ file_name ));
293300 }
301+
302+ @unlink ($ file_name_compressed );
294303}
295304
296305task (sprintf ('Renaming file "%s" to "%s/%s". ' , $ file_name , $ db_dir , $ db_file ));
0 commit comments