1 | <?php |
||
2 | |||
3 | namespace CodexShaper\DBM\Commands; |
||
4 | |||
5 | use CodexShaper\Dumper\Contracts\Dumper; |
||
6 | use Illuminate\Console\Command; |
||
7 | use Illuminate\Filesystem\Filesystem; |
||
8 | use Illuminate\Support\Facades\Storage; |
||
9 | use Symfony\Component\Console\Input\InputOption; |
||
10 | |||
11 | class DatabaseRestore extends Command |
||
12 | { |
||
13 | /** |
||
14 | * The name and signature of the console command. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $signature = 'dbm:restore |
||
19 | {--p|path=} |
||
20 | {--f|file=}'; |
||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = 'Restore Database'; |
||
27 | |||
28 | /** |
||
29 | * Get Option. |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | protected function getOptions() |
||
34 | { |
||
35 | return [ |
||
36 | ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production', null], |
||
37 | ]; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get the composer command for the environment. |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | protected function findComposer() |
||
46 | { |
||
47 | if (file_exists(getcwd().'/composer.phar')) { |
||
48 | return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar'; |
||
49 | } |
||
50 | |||
51 | return 'composer'; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Execute the console command. |
||
56 | * |
||
57 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | public function handle(Filesystem $filesystem, Dumper $dumper) |
||
62 | { |
||
63 | $this->info('Restoring Database'); |
||
64 | |||
65 | $driver = dbm_driver(); |
||
66 | $hostname = config('database.connections.'.$driver.'.host', '127.0.0.1'); |
||
67 | $port = config('database.connections.'.$driver.'.port', '3306'); |
||
68 | $database = config('database.connections.'.$driver.'.database', 'dbm'); |
||
69 | $username = config('database.connections.'.$driver.'.username', 'root'); |
||
70 | $password = config('database.connections.'.$driver.'.password', ''); |
||
71 | |||
72 | $directory = 'backups'.DIRECTORY_SEPARATOR.$driver; |
||
73 | |||
74 | if ($this->option('path') != null) { |
||
75 | $path = $this->option('path'); |
||
76 | } elseif ($this->option('file') != null) { |
||
77 | $path = $directory.DIRECTORY_SEPARATOR.$this->option('file'); |
||
78 | } else { |
||
79 | $files = array_reverse(Storage::files($directory)); |
||
80 | $path = $files[0]; |
||
81 | } |
||
82 | |||
83 | $filePath = storage_path('app').DIRECTORY_SEPARATOR.$path; |
||
84 | $isCompress = config('dbm.backup.compress', false); |
||
85 | $compressBinaryPath = config('dbm.backup.compress_binary_path', ''); |
||
86 | $compressCommand = config('dbm.backup.uncompress_command', 'gunzip'); |
||
87 | $compressExtension = config('dbm.backup.compress_extension', '.gz'); |
||
88 | $dumpBinaryPath = config('dbm.backup.'.$driver.'.binary_path', ''); |
||
89 | |||
90 | $dumper->setHost($hostname) |
||
91 | ->setPort($port) |
||
92 | ->setDbName($database) |
||
93 | ->setUserName($username) |
||
94 | ->setPassword($password); |
||
95 | |||
96 | try { |
||
97 | switch ($driver) { |
||
98 | case 'mongodb': |
||
99 | $dsn = config('dbm.backup.mongodb.dsn', ''); |
||
100 | if (! empty($dsn)) { |
||
101 | $dumper->setUri($dsn); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
102 | } |
||
103 | break; |
||
104 | } |
||
105 | |||
106 | if ($isCompress) { |
||
107 | $dumper->useCompress($compressCommand, $compressExtension, $compressBinaryPath); |
||
108 | } |
||
109 | $dumper->setCommandBinaryPath($dumpBinaryPath) |
||
110 | ->setRestorePath($filePath) |
||
111 | ->restore(); |
||
112 | |||
113 | $this->info('Restored Completed'); |
||
114 | } catch (\Exception $e) { |
||
115 | throw new \Exception($e->getMessage(), 1); |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 |