@@ -64,6 +64,8 @@ public function handle()
6464 $ databaseOnly = $ this ->option ('database-only ' );
6565 $ filesOnly = $ this ->option ('files-only ' );
6666
67+ $ tempDir = null ; // <-- Tambahan variabel tempDir global handle
68+
6769 try {
6870 // Find the backup file
6971 $ backupFile = $ this ->findBackupFile ($ disk , $ backup );
@@ -81,34 +83,34 @@ public function handle()
8183 // Check if this backup contains database dumps
8284 $ hasDatabase = $ this ->backupContainsDatabase ($ disk , $ backupFile );
8385
86+ // Extract backup ONCE jika restore database atau files
87+ if ((! $ filesOnly && $ hasDatabase ) || ! $ databaseOnly ) {
88+ $ tempDir = $ this ->extractBackup ($ disk , $ backupFile );
89+ if (! $ tempDir ) {
90+ $ this ->error ('❌ Failed to extract backup! ' );
91+
92+ return 1 ;
93+ }
94+ }
95+
8496 // Restore database first (if backup contains database and not files-only)
8597 if (! $ filesOnly && $ hasDatabase ) {
8698 $ this ->info ('🗄️ Restoring database... ' );
87- if (! $ this ->restoreDatabase ($ disk , $ backupFile )) {
99+ if (! $ this ->restoreDatabase ($ disk , $ backupFile, $ tempDir )) {
88100 $ success = false ;
89101 }
90102 } elseif (! $ filesOnly && ! $ hasDatabase ) {
91103 $ this ->info ('ℹ️ No database found in backup (files-only backup) ' );
92104 }
93105
94106 // Restore files (if not database-only)
95- if (! $ databaseOnly && $ success ) {
107+ if (! $ databaseOnly && $ success && $ tempDir ) {
96108 $ this ->info ('📁 Restoring files... ' );
97-
98- // Extract backup to temporary location
99- $ tempDir = $ this ->extractBackup ($ disk , $ backupFile );
100- if (! $ tempDir ) {
101- $ this ->error ('❌ Failed to extract backup! ' );
102-
103- return 1 ;
104- }
105-
106109 if (! $ this ->restoreFiles ($ tempDir )) {
107110 $ success = false ;
108111 }
109112
110113 $ this ->cleanup ($ tempDir );
111-
112114 }
113115
114116 if ($ success ) {
@@ -133,6 +135,9 @@ public function handle()
133135
134136 } catch (Exception $ e ) {
135137 $ this ->error ('❌ Restore failed with error: ' .$ e ->getMessage ());
138+ if ($ tempDir ) {
139+ $ this ->cleanup ($ tempDir );
140+ }
136141
137142 return 1 ;
138143 }
@@ -372,47 +377,38 @@ private function restoreFiles($tempDir)
372377 $ failed = 0 ;
373378
374379 try {
375- // Look for the storage directory in the backup
376- $ storagePath = $ this -> findStoragePathInBackup ( $ tempDir );
380+ // Ambil mapping folder source => target dari config
381+ $ folders = config ( ' backup.backup.source.files.include ' ); // array: 'folder' => targetPath
377382
378- if (! $ storagePath ) {
379- $ this ->error ('❌ Storage directory not found in backup ' );
380- $ this ->warn ('💡 Available directories in backup: ' );
381- $ this ->listBackupContents ($ tempDir , 2 );
383+ if (empty ($ folders )) {
384+ $ this ->error ('❌ No folders configured to restore ' );
382385
383386 return false ;
384387 }
385388
386- $ this ->info ('📁 Found storage directory: ' .basename ($ storagePath ));
387-
388- // Restore storage directory to the current storage path
389- $ targetStoragePath = storage_path ();
389+ // Cari semua folder di backup
390+ $ backupPaths = $ this ->findBackupPaths ($ tempDir );
390391
391- $ this ->info ('🔄 Restoring storage from: ' .basename ($ storagePath ));
392- $ this ->info ('🔄 Restoring storage to: ' .$ targetStoragePath );
392+ foreach ($ folders as $ folder => $ targetPath ) {
393+ // Cek apakah folder ada di backup
394+ $ sourcePath = $ backupPaths [$ folder ] ?? null ;
395+ if (! $ sourcePath ) {
396+ $ this ->warn ("⚠️ Folder {$ folder } not found in backup " );
397+ $ failed ++;
393398
394- if ($ this ->restoreDirectory ($ storagePath , $ targetStoragePath )) {
395- $ this ->info ('✅ Restored storage directory ' );
396- $ restored ++;
397- } else {
398- $ this ->error ('❌ Failed to restore storage directory ' );
399- $ failed ++;
400- }
399+ continue ;
400+ }
401401
402- // Also try to restore public directories if they exist
403- $ publicPaths = $ this ->findPublicPathsInBackup ($ tempDir );
404- foreach ($ publicPaths as $ publicPath ) {
405- $ this ->info ('🔄 Restoring public directory: ' .basename ($ publicPath ));
406- if ($ this ->restorePublicDirectory ($ publicPath )) {
407- $ this ->info ('✅ Restored public directory: ' .basename ($ publicPath ));
402+ $ this ->info ("🔄 Restoring {$ folder } to {$ targetPath }" );
403+ if ($ this ->restoreDirectory ($ sourcePath , $ targetPath )) {
404+ $ this ->info ("✅ Restored {$ folder }" );
408405 $ restored ++;
409406 } else {
410- $ this ->warn (' ⚠️ Failed to restore public directory: ' . basename ( $ publicPath ) );
407+ $ this ->warn (" ⚠️ Failed to restore { $ folder }" );
411408 $ failed ++;
412409 }
413410 }
414411
415- // Fix permissions after restoration
416412 if ($ restored > 0 ) {
417413 $ this ->info ('🔧 Fixing file permissions... ' );
418414 $ this ->fixPermissions ();
@@ -429,41 +425,40 @@ private function restoreFiles($tempDir)
429425 }
430426 }
431427
432- private function findStoragePathInBackup ($ tempDir )
428+ private function findBackupPaths ($ tempDir )
433429 {
434- // Look for storage directory in common locations
435- $ possiblePaths = [
436- $ tempDir .'/storage/app/public ' ,
437- ];
438-
439- foreach ($ possiblePaths as $ path ) {
440- if (File::exists ($ path ) && File::isDirectory ($ path )) {
441- $ this ->info ('✅ Found storage directory: ' .basename ($ path ));
430+ $ paths = [];
442431
443- return $ path ;
444- }
445- }
432+ // Ambil daftar folder/file dari config
433+ $ includes = config ('backup.backup.source.files.include ' , []);
446434
447- // If not found in common locations, search recursively
448- $ this ->info ('🔍 Searching for storage directory in backup... ' );
449- $ storagePath = $ this ->findDirectoryRecursively ($ tempDir , 'storage ' );
435+ foreach ($ includes as $ relative ) {
436+ $ possiblePath = $ tempDir .'/ ' .ltrim ($ relative , '/ ' );
450437
451- if ($ storagePath ) {
452- $ this ->info ('✅ Found storage directory recursively: ' .basename ($ storagePath ));
453- } else {
454- $ this ->warn ('⚠️ Storage directory not found in common locations ' );
438+ if (File::exists ($ possiblePath ) && File::isDirectory ($ possiblePath )) {
439+ $ this ->info ('✅ Found directory: ' .basename ($ possiblePath ));
440+ $ paths [] = $ possiblePath ;
441+ } else {
442+ // Kalau tidak ada di lokasi umum, cari rekursif
443+ $ found = $ this ->findDirectoryRecursively ($ tempDir , basename ($ relative ));
444+ if ($ found ) {
445+ $ this ->info ('✅ Found directory recursively: ' .basename ($ found ));
446+ $ paths [] = $ found ;
447+ } else {
448+ $ this ->warn ("⚠️ Directory not found: {$ relative }" );
449+ }
450+ }
455451 }
456452
457- return $ storagePath ;
453+ return $ paths ;
458454 }
459455
460456 private function findDirectoryRecursively ($ dir , $ targetDir )
461457 {
462458 $ items = File::directories ($ dir );
463459
464460 foreach ($ items as $ item ) {
465- $ basename = basename ($ item );
466- if ($ basename === $ targetDir ) {
461+ if (basename ($ item ) === $ targetDir ) {
467462 return $ item ;
468463 }
469464
@@ -476,71 +471,6 @@ private function findDirectoryRecursively($dir, $targetDir)
476471 return null ;
477472 }
478473
479- private function findPublicPathsInBackup ($ tempDir )
480- {
481- $ publicPaths = [];
482-
483- // Look for public directories in common locations
484- $ possiblePaths = [
485- $ tempDir .'/public ' ,
486- ];
487-
488- foreach ($ possiblePaths as $ path ) {
489- if (File::exists ($ path ) && File::isDirectory ($ path ) && ! str_contains ($ path , '/storage/ ' )) {
490- $ publicPaths [] = $ path ;
491- }
492- }
493-
494- // If not found in common locations, search recursively
495- if (empty ($ publicPaths )) {
496- $ this ->info ('🔍 Searching for public directories in backup... ' );
497- $ publicPath = $ this ->findDirectoryRecursively ($ tempDir , 'public ' );
498- if ($ publicPath && ! str_contains ($ publicPath , '/storage/ ' )) {
499- $ publicPaths [] = $ publicPath ;
500- }
501- }
502-
503- return $ publicPaths ;
504- }
505-
506- private function restorePublicDirectory ($ publicPath )
507- {
508- try {
509- $ targetPublicPath = public_path ();
510-
511- // Check if this is a ShynDorca TP project (has uploads/download directories)
512- $ hasUploads = File::exists ($ publicPath .'/uploads ' );
513- $ hasDownload = File::exists ($ publicPath .'/download ' );
514-
515- if ($ hasUploads || $ hasDownload ) {
516- $ this ->info ('📁 Detected ShynDorca TP public directory ' );
517-
518- // Restore specific directories that are backed up
519- if ($ hasUploads ) {
520- $ this ->info ('🔄 Restoring uploads directory... ' );
521- $ this ->copyDirectoryContents ($ publicPath .'/uploads ' , $ targetPublicPath .'/uploads ' );
522- }
523-
524- if ($ hasDownload ) {
525- $ this ->info ('🔄 Restoring download directory... ' );
526- $ this ->copyDirectoryContents ($ publicPath .'/download ' , $ targetPublicPath .'/download ' );
527- }
528-
529- return true ;
530- } else {
531- // For other projects, restore the entire public directory
532- $ this ->info ('🔄 Restoring entire public directory... ' );
533-
534- return $ this ->restoreDirectory ($ publicPath , $ targetPublicPath );
535- }
536-
537- } catch (Exception $ e ) {
538- $ this ->error ('❌ Failed to restore public directory: ' .$ e ->getMessage ());
539-
540- return false ;
541- }
542- }
543-
544474 private function restoreDirectory ($ source , $ destination )
545475 {
546476 try {
@@ -842,7 +772,7 @@ private function importDatabaseDump($dumpFile, $connection)
842772 {
843773 try {
844774 if (! File::exists ($ dumpFile )) {
845- echo "❌ Dump file tidak ditemukan : $ dumpFile \n" ;
775+ echo "❌ Dump file not found : $ dumpFile \n" ;
846776
847777 return false ;
848778 }
@@ -903,26 +833,26 @@ private function importDatabaseDump($dumpFile, $connection)
903833 break ;
904834
905835 default :
906- echo "❌ Driver [ $ connection] tidak didukung untuk restore. \n" ;
836+ echo "❌ Driver [ $ connection] is not supported for restore. \n" ;
907837
908838 return false ;
909839 }
910840
911- echo "▶ Menjalankan perintah : $ command \n" ;
841+ echo "▶ Running command : $ command \n" ;
912842 exec ($ command , $ output , $ resultCode );
913843
914844 if ($ resultCode === 0 ) {
915- echo "✅ Database berhasil direstore dari $ dumpFile \n" ;
845+ echo "✅ Database successfully restored from $ dumpFile \n" ;
916846
917847 return true ;
918848 }
919849
920- echo "❌ Restore gagal . Output: \n" .implode ("\n" , $ output )."\n" ;
850+ echo "❌ Restore failed . Output: \n" .implode ("\n" , $ output )."\n" ;
921851
922852 return false ;
923853
924854 } catch (\Throwable $ e ) {
925- echo '⚠️ Terjadi error : ' .$ e ->getMessage ()."\n" ;
855+ echo '⚠️ Error occurred : ' .$ e ->getMessage ()."\n" ;
926856
927857 return false ;
928858 }
0 commit comments