1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Cli\Executable; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Executable; |
5
|
|
|
use phpbu\App\Exception; |
6
|
|
|
use SebastianFeldmann\Cli\CommandLine; |
7
|
|
|
use SebastianFeldmann\Cli\Command\Executable as Cmd; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Arangodump source class. |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @subpackage Backup |
14
|
|
|
* @author Francis Chuang <[email protected]> |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
* @since Class available since Release 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
class Arangodump extends Abstraction implements Executable |
22
|
|
|
{ |
23
|
|
|
use OptionMasker; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Endpoint to connect to |
27
|
|
|
* --server.endpoint <endpoint> |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $endpoint; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Username to connect with |
35
|
|
|
* --server.username <username> |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $username; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Password to authenticate with |
43
|
|
|
* --server.password <password> |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $password; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The database to backup |
51
|
|
|
* --server.database <database> |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $database; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Whether the data should be dumped or not |
59
|
|
|
* --dump-data |
60
|
|
|
* |
61
|
|
|
* @var boolean |
62
|
|
|
*/ |
63
|
|
|
private $dumpData; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Include system collections |
67
|
|
|
* --include-system-collections |
68
|
|
|
* |
69
|
|
|
* @var boolean |
70
|
|
|
*/ |
71
|
|
|
private $includeSystemCollections; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Restrict the dump to these collections |
75
|
|
|
* --collection |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
private $collections = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Do not ask for the username and password when connecting to the server. |
83
|
|
|
* This does not control whether the server requires authentication. |
84
|
|
|
* -- disable-authentication |
85
|
|
|
* |
86
|
|
|
* @var boolean |
87
|
|
|
*/ |
88
|
|
|
private $disableAuthentication; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Directory to dump to. |
92
|
|
|
* |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
private $dumpDir; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Constructor. |
99
|
|
|
* |
100
|
|
|
* @param string $path |
101
|
|
|
*/ |
102
|
15 |
|
public function __construct(string $path = '') |
103
|
|
|
{ |
104
|
15 |
|
$this->setup('arangodump', $path); |
105
|
15 |
|
$this->setMaskCandidates(['password']); |
106
|
15 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set target dump directory. |
110
|
|
|
* |
111
|
|
|
* @param string $path |
112
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
113
|
|
|
*/ |
114
|
14 |
|
public function dumpTo(string $path) : Arangodump |
115
|
|
|
{ |
116
|
14 |
|
$this->dumpDir = $path; |
117
|
14 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set user credentials. |
122
|
|
|
* |
123
|
|
|
* @param string $username |
124
|
|
|
* @param string $password |
125
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
126
|
|
|
*/ |
127
|
7 |
|
public function credentials(string $username = '', string $password = '') : Arangodump |
128
|
|
|
{ |
129
|
7 |
|
$this->username = $username; |
130
|
7 |
|
$this->password = $password; |
131
|
7 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Endpoint to use. |
136
|
|
|
* |
137
|
|
|
* @param string $endpoint |
138
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
139
|
|
|
*/ |
140
|
6 |
|
public function useEndpoint(string $endpoint) : Arangodump |
141
|
|
|
{ |
142
|
6 |
|
$this->endpoint = $endpoint; |
143
|
6 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Database to dump. |
148
|
|
|
* |
149
|
|
|
* @param string $database |
150
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
151
|
|
|
*/ |
152
|
6 |
|
public function dumpDatabase(string $database) : Arangodump |
153
|
|
|
{ |
154
|
6 |
|
$this->database = $database; |
155
|
6 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Collections to dump. |
160
|
|
|
* |
161
|
|
|
* @param array $collections |
162
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
163
|
|
|
*/ |
164
|
6 |
|
public function dumpCollections(array $collections) : Arangodump |
165
|
|
|
{ |
166
|
6 |
|
$this->collections = $collections; |
167
|
6 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Disable authentication. |
172
|
|
|
* |
173
|
|
|
* @param boolean $bool |
174
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
175
|
|
|
*/ |
176
|
6 |
|
public function disableAuthentication(bool $bool) : Arangodump |
177
|
|
|
{ |
178
|
6 |
|
$this->disableAuthentication = $bool; |
179
|
6 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Dump system collections. |
184
|
|
|
* |
185
|
|
|
* @param boolean $bool |
186
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
187
|
|
|
*/ |
188
|
6 |
|
public function includeSystemCollections(bool $bool) : Arangodump |
189
|
|
|
{ |
190
|
6 |
|
$this->includeSystemCollections = $bool; |
191
|
6 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Dump data as well. |
196
|
|
|
* |
197
|
|
|
* @param boolean $bool |
198
|
|
|
* @return \phpbu\App\Cli\Executable\Arangodump |
199
|
|
|
*/ |
200
|
6 |
|
public function dumpData(bool $bool) : Arangodump |
201
|
|
|
{ |
202
|
6 |
|
$this->dumpData = $bool; |
203
|
6 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Arangodump CommandLine generator. |
208
|
|
|
* |
209
|
|
|
* @return \SebastianFeldmann\Cli\CommandLine |
210
|
|
|
* @throws \phpbu\App\Exception |
211
|
|
|
*/ |
212
|
15 |
|
public function createCommandLine() : CommandLine |
213
|
|
|
{ |
214
|
15 |
|
if (empty($this->dumpDir)) { |
215
|
1 |
|
throw new Exception('dump dir is mandatory'); |
216
|
|
|
} |
217
|
|
|
|
218
|
14 |
|
$process = new CommandLine(); |
219
|
14 |
|
$cmd = new Cmd($this->binary); |
220
|
14 |
|
$process->addCommand($cmd); |
221
|
|
|
|
222
|
14 |
|
$cmd->addOptionIfNotEmpty('--server.username', $this->username, true, ' '); |
223
|
14 |
|
$cmd->addOptionIfNotEmpty('--server.password', $this->password, true, ' '); |
224
|
14 |
|
$cmd->addOptionIfNotEmpty('--server.endpoint', $this->endpoint, true, ' '); |
225
|
14 |
|
$cmd->addOptionIfNotEmpty('--server.database', $this->database, true, ' '); |
226
|
|
|
|
227
|
14 |
|
$this->handleCollections($cmd); |
228
|
|
|
|
229
|
14 |
|
if ($this->disableAuthentication) { |
230
|
1 |
|
$cmd->addOption('--server.disable-authentication', 'true', ' '); |
231
|
|
|
} |
232
|
|
|
|
233
|
14 |
|
$this->handleDump($cmd); |
234
|
|
|
|
235
|
14 |
|
return $process; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Handle command collection settings. |
240
|
|
|
* |
241
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
242
|
|
|
*/ |
243
|
14 |
|
protected function handleCollections(Cmd $cmd) |
244
|
|
|
{ |
245
|
14 |
|
if (count($this->collections)) { |
246
|
2 |
|
foreach ($this->collections as $collection) { |
247
|
2 |
|
$cmd->addOption('--collection', $collection, ' '); |
248
|
|
|
} |
249
|
|
|
} |
250
|
14 |
|
if ($this->includeSystemCollections) { |
251
|
1 |
|
$cmd->addOption('--include-system-collections', 'true', ' '); |
252
|
|
|
} |
253
|
14 |
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Handle command data settings. |
257
|
|
|
* |
258
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
259
|
|
|
*/ |
260
|
14 |
|
protected function handleDump(Cmd $cmd) |
261
|
|
|
{ |
262
|
14 |
|
if ($this->dumpData) { |
263
|
1 |
|
$cmd->addOption('--dump-data', 'true', ' '); |
264
|
|
|
} |
265
|
14 |
|
$cmd->addOption('--output-directory', $this->dumpDir, ' '); |
266
|
14 |
|
} |
267
|
|
|
} |
268
|
|
|
|