1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\Dumper\Drivers; |
4
|
|
|
|
5
|
|
|
use CodexShaper\Dumper\Dumper; |
6
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
7
|
|
|
|
8
|
|
|
class MysqlDumper extends Dumper |
9
|
|
|
{ |
10
|
|
|
/*@var bool*/ |
11
|
|
|
protected $singleTransaction = false; |
12
|
|
|
/*@var bool*/ |
13
|
|
|
protected $skipLockTables = false; |
14
|
|
|
/*@var bool*/ |
15
|
|
|
protected $quick = false; |
16
|
|
|
/*@var bool*/ |
17
|
|
|
protected $skipComments = true; |
18
|
|
|
/*@var string*/ |
19
|
|
|
protected $defaultCharacterSet = ''; |
20
|
|
|
/*@var bool*/ |
21
|
|
|
protected $createTables = true; |
22
|
|
|
|
23
|
2 |
|
public function useSingleTransaction() |
24
|
|
|
{ |
25
|
2 |
|
$this->singleTransaction = true; |
26
|
2 |
|
return $this; |
27
|
|
|
} |
28
|
2 |
|
public function useSkipLockTables() |
29
|
|
|
{ |
30
|
2 |
|
$this->skipLockTables = true; |
31
|
2 |
|
return $this; |
32
|
|
|
} |
33
|
2 |
|
public function useQuick() |
34
|
|
|
{ |
35
|
2 |
|
$this->quick = true; |
36
|
2 |
|
return $this; |
37
|
|
|
} |
38
|
2 |
|
public function doNotUseSkipComments() |
39
|
|
|
{ |
40
|
2 |
|
$this->skipComments = false; |
41
|
2 |
|
return $this; |
42
|
|
|
} |
43
|
2 |
|
public function doNotCreateTables() |
44
|
|
|
{ |
45
|
2 |
|
$this->createTables = false; |
46
|
2 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
public function setDefaultCharacterSet(string $charecterSet) |
49
|
|
|
{ |
50
|
|
|
$this->defaultCharacterSet = $charecterSet; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function dump(string $destinationPath = "") |
55
|
|
|
{ |
56
|
|
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
57
|
|
|
$this->runCommand($destinationPath, "dump"); |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function restore(string $restorePath = "") |
62
|
|
|
{ |
63
|
|
|
$restorePath = !empty($restorePath) ? $restorePath : $this->restorePath; |
64
|
|
|
$this->runCommand($restorePath, 'restore'); |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
28 |
|
protected function prepareDumpCommand(string $credentialFile, string $destinationPath): string |
69
|
|
|
{ |
70
|
28 |
|
$dumpCommand = sprintf( |
71
|
28 |
|
'%s %s %s %s %s %s %s %s %s %s %s %s', |
72
|
28 |
|
$this->quoteCommand("{$this->commandBinaryPath}mysqldump"), |
73
|
28 |
|
$this->prepareAuthentication($credentialFile), |
74
|
28 |
|
$this->prepareSocket(), |
75
|
28 |
|
$this->prepareSkipComments(), |
76
|
28 |
|
$this->prepareCreateTables(), |
77
|
28 |
|
$this->prepareSingleTransaction(), |
78
|
28 |
|
$this->prepareSkipLockTables(), |
79
|
28 |
|
$this->prepareQuick(), |
80
|
28 |
|
$this->prepareDefaultCharSet(), |
81
|
28 |
|
$this->prepareDatabase(), |
82
|
28 |
|
$this->prepareIncludeTables(), |
83
|
28 |
|
$this->prepareIgnoreTables() |
84
|
|
|
); |
85
|
|
|
|
86
|
28 |
|
if ($this->isCompress) { |
87
|
2 |
|
$compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}"); |
88
|
2 |
|
return "{$dumpCommand} | {$compressCommand} > \"{$destinationPath}{$this->compressExtension}\""; |
89
|
|
|
} |
90
|
|
|
|
91
|
26 |
|
return "{$dumpCommand} > \"{$destinationPath}\""; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
protected function prepareRestoreCommand(string $credentialFile, string $filePath): string |
95
|
|
|
{ |
96
|
6 |
|
$restoreCommand = sprintf("%s %s %s", |
97
|
6 |
|
$this->quoteCommand("{$this->commandBinaryPath}mysql"), |
98
|
6 |
|
$this->prepareAuthentication($credentialFile), |
99
|
6 |
|
$this->prepareDatabase() |
100
|
|
|
); |
101
|
|
|
|
102
|
6 |
|
if ($this->isCompress) { |
103
|
|
|
|
104
|
2 |
|
$compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}"); |
105
|
|
|
|
106
|
2 |
|
return "{$compressCommand} < \"{$filePath}\" | {$restoreCommand}"; |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
return "{$restoreCommand} < \"{$filePath}\""; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function runCommand($filePath, $action) |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
|
116
|
|
|
$credentials = $this->getCredentials(); |
117
|
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'mysqlpass'); |
118
|
|
|
$handler = fopen($tempFile, 'r+'); |
119
|
|
|
if ($handler !== false) { |
120
|
|
|
fwrite($handler, $credentials); |
121
|
|
|
|
122
|
|
|
if ($action == 'dump') { |
123
|
|
|
$dumpCommand = $this->prepareDumpCommand($tempFile, $filePath); |
124
|
|
|
$this->command = $this->removeExtraSpaces($dumpCommand); |
125
|
|
|
} else if ($action == 'restore') { |
126
|
|
|
$dumpCommand = $this->prepareRestoreCommand($tempFile, $filePath); |
127
|
|
|
$this->command = $this->removeExtraSpaces($dumpCommand); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$process = $this->prepareProcessCommand(); |
131
|
|
|
|
132
|
|
|
if ($this->debug) { |
133
|
|
|
$process->mustRun(); |
134
|
|
|
} else { |
135
|
|
|
$process->run(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
fclose($handler); |
139
|
|
|
unlink($tempFile); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} catch (ProcessFailedException $e) { |
143
|
|
|
throw new \Exception($e->getMessage()); |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function getCredentials() |
149
|
|
|
{ |
150
|
|
|
$contents = [ |
151
|
|
|
'[client]', |
152
|
|
|
"user = '{$this->username}'", |
153
|
|
|
"password = '{$this->password}'", |
154
|
|
|
"host = '{$this->host}'", |
155
|
|
|
"port = '{$this->port}'", |
156
|
|
|
]; |
157
|
|
|
return implode(PHP_EOL, $contents); |
158
|
|
|
} |
159
|
|
|
|
160
|
28 |
|
public function prepareSingleTransaction() |
161
|
|
|
{ |
162
|
28 |
|
return $this->singleTransaction ? '--single-transaction' : ''; |
163
|
|
|
} |
164
|
|
|
|
165
|
28 |
|
public function prepareSkipLockTables() |
166
|
|
|
{ |
167
|
28 |
|
return $this->skipLockTables ? '--skip-lock-tables' : ''; |
168
|
|
|
} |
169
|
|
|
|
170
|
28 |
|
public function prepareQuick() |
171
|
|
|
{ |
172
|
28 |
|
return $this->quick ? '--quick' : ''; |
173
|
|
|
} |
174
|
|
|
|
175
|
28 |
|
public function prepareCreateTables() |
176
|
|
|
{ |
177
|
28 |
|
return !$this->createTables ? '--no-create-info' : ''; |
178
|
|
|
} |
179
|
|
|
|
180
|
28 |
|
public function prepareSkipComments() |
181
|
|
|
{ |
182
|
28 |
|
return $this->skipComments ? '--skip-comments' : ''; |
183
|
|
|
} |
184
|
|
|
|
185
|
28 |
|
public function prepareDefaultCharSet() |
186
|
|
|
{ |
187
|
28 |
|
return ($this->defaultCharacterSet !== '') ? "--default-character-set={$this->defaultCharacterSet}" : ''; |
188
|
|
|
} |
189
|
|
|
|
190
|
34 |
|
public function prepareAuthentication(string $credentialFile) |
191
|
|
|
{ |
192
|
34 |
|
return "--defaults-extra-file=\"{$credentialFile}\""; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|