1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EDouna\LaravelDBBackup\Commands; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
|
8
|
|
|
class Restore extends BaseCommand |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'db:restore'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Retrieve a list of available back-ups of the current database driver and provides a choice list.'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Execute the console command. |
26
|
|
|
* |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
public function handle(): int |
30
|
|
|
{ |
31
|
|
|
Log::info('Starting restore procedure.'); |
32
|
|
|
$this->line('Starting restore procedure.'); |
33
|
|
|
$startTime = microtime(true); |
34
|
|
|
|
35
|
|
|
$this->database->buildDatabaseClass(); |
36
|
|
|
|
37
|
|
|
if (false === $this->storage->initializeStorageFolder()) { |
38
|
|
|
$this->error('Error in the back-up directory. Please see the error log for further details.'); |
39
|
|
|
|
40
|
|
|
return 1; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$files = $this->storage->getMostRecentBackups($this->database->getRealDatabase()->getDatabaseIdentifier()); |
44
|
|
|
if (null === $files) { |
45
|
|
|
$this->error(sprintf('No back-up files found for driver %s. No need to continue the restore procedure.', $this->database->getRealDatabase()->getDatabaseIdentifier())); |
46
|
|
|
|
47
|
|
|
return 1; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$selectionArray = []; |
51
|
|
|
foreach ($files as $k => $f) { |
52
|
|
|
$createdAt = Carbon::createFromTimestamp($f->getCTime())->toDateTimeString(); |
53
|
|
|
$selectionArray[$k] = sprintf('%s | Filename: %s | Created at: %s', $k, $f->getFilename(), $createdAt); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$restoreSelection = $this->choice('Please select the desired back-up file. Use the number to continue. Default selection:', $selectionArray, 1, null, false); |
57
|
|
|
|
58
|
|
|
$restoreSelection = explode(' ', $restoreSelection); |
|
|
|
|
59
|
|
|
$restoreSelection = $restoreSelection[0]; |
60
|
|
|
|
61
|
|
|
$this->comment('Restoring back-up. Depending on the file size this might take a few moments...'); |
62
|
|
|
|
63
|
|
|
$decompressedFile = $this->storage->decompressBackupFile($files[$restoreSelection], $this->database); |
64
|
|
|
if (false === $decompressedFile) { |
|
|
|
|
65
|
|
|
Log::error('Error in decompressing the archive. Please see the log files for further details.'); |
66
|
|
|
$this->error('Error in decompressing the archive. Please see the log files for further details.'); |
67
|
|
|
|
68
|
|
|
return 1; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (false === $this->database->getRealDatabase()->restore($decompressedFile)) { |
72
|
|
|
Log::error('Error in restoring the database archive. Please see the log files for further details.'); |
73
|
|
|
$this->error('Error in restoring the database archive. Please see the log files for further details.'); |
74
|
|
|
|
75
|
|
|
return 1; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (false === $this->storage->clearTmpFile($decompressedFile)) { |
79
|
|
|
Log::error('Error in cleaning the temp back-up file. Please see the log files for further details.'); |
80
|
|
|
$this->error('Error in cleaning the temp back-up file. Please see the log files for further details.'); |
81
|
|
|
|
82
|
|
|
return 1; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$endTime = round(microtime(true) - $startTime, 2); |
86
|
|
|
$this->comment(sprintf('Finished restoring the database in %s second(s).', $endTime)); |
87
|
|
|
Log::info(sprintf('Finished restoring the database in %s second(s).', $endTime)); |
88
|
|
|
|
89
|
|
|
return 0; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|