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
|
|
|
* Mongodump executable class. |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @subpackage Backup |
14
|
|
|
* @author Sebastian Feldmann <[email protected]> |
15
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
17
|
|
|
* @link http://phpbu.de/ |
18
|
|
|
* @since Class available since Release 2.1.0 |
19
|
|
|
*/ |
20
|
|
|
class Mongodump extends Abstraction implements Executable |
21
|
|
|
{ |
22
|
|
|
use OptionMasker; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Dump Directory |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $dumpDir; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Use IPv6 |
33
|
|
|
* --ipv6 |
34
|
|
|
* |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
private $useIPv6; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Host to connect to |
41
|
|
|
* --host <hostname:port> |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $host; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* User to connect with |
49
|
|
|
* --username <username> |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $user; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Password to authenticate with |
57
|
|
|
* --password <password> |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $password; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Database to use for authentication |
65
|
|
|
* --authenticationDatabase <dbname> |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $authenticationDatabase; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* List of databases to backup |
73
|
|
|
* --db <database> |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
private $databases = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* List of collections to backup |
81
|
|
|
* --collection <collection> |
82
|
|
|
* |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
private $collections = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* List of collections to ignore |
89
|
|
|
* --excludeCollections array of strings |
90
|
|
|
* |
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
private $excludeCollections = []; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* List of prefixes to exclude collections |
97
|
|
|
* --excludeCollectionWithPrefix array of strings |
98
|
|
|
* |
99
|
|
|
* @var array |
100
|
|
|
*/ |
101
|
|
|
private $excludeCollectionsWithPrefix = []; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Constructor. |
105
|
|
|
* |
106
|
|
|
* @param string $path |
107
|
|
|
*/ |
108
|
13 |
|
public function __construct(string $path = '') |
109
|
|
|
{ |
110
|
13 |
|
$this->setup('mongodump', $path); |
111
|
13 |
|
$this->setMaskCandidates(['password']); |
112
|
13 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set path to dump to. |
116
|
|
|
* |
117
|
|
|
* @param string $path |
118
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
119
|
|
|
*/ |
120
|
12 |
|
public function dumpToDirectory(string $path) : Mongodump |
121
|
|
|
{ |
122
|
12 |
|
$this->dumpDir = $path; |
123
|
12 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Use ipv6 to connect. |
128
|
|
|
* |
129
|
|
|
* @param boolean $bool |
130
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
131
|
|
|
*/ |
132
|
4 |
|
public function useIpv6(bool $bool) : Mongodump |
133
|
|
|
{ |
134
|
4 |
|
$this->useIPv6 = $bool; |
135
|
4 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set host to dump from. |
140
|
|
|
* |
141
|
|
|
* @param string $host |
142
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
143
|
|
|
*/ |
144
|
4 |
|
public function useHost(string $host) : Mongodump |
145
|
|
|
{ |
146
|
4 |
|
$this->host = $host; |
147
|
4 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set credentials. |
152
|
|
|
* |
153
|
|
|
* @param string $user |
154
|
|
|
* @param string $password |
155
|
|
|
* @param string $authDatabase |
156
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
157
|
|
|
*/ |
158
|
5 |
|
public function credentials(string $user = '', string $password = '', string $authDatabase = '') : Mongodump |
159
|
|
|
{ |
160
|
5 |
|
$this->user = $user; |
161
|
5 |
|
$this->password = $password; |
162
|
5 |
|
$this->authenticationDatabase = $authDatabase; |
163
|
5 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Dump only given databases. |
168
|
|
|
* |
169
|
|
|
* @param array $databases |
170
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
171
|
|
|
*/ |
172
|
4 |
|
public function dumpDatabases(array $databases) : Mongodump |
173
|
|
|
{ |
174
|
4 |
|
$this->databases = $databases; |
175
|
4 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Dump only given collections. |
180
|
|
|
* |
181
|
|
|
* @param array $collections |
182
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
183
|
|
|
*/ |
184
|
4 |
|
public function dumpCollections(array $collections) : Mongodump |
185
|
|
|
{ |
186
|
4 |
|
$this->collections = $collections; |
187
|
4 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Exclude collections. |
192
|
|
|
* |
193
|
|
|
* @param array $collections |
194
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
195
|
|
|
*/ |
196
|
4 |
|
public function excludeCollections(array $collections) : Mongodump |
197
|
|
|
{ |
198
|
4 |
|
$this->excludeCollections = $collections; |
199
|
4 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Exclude collections with given prefixes. |
204
|
|
|
* |
205
|
|
|
* @param array $prefixes |
206
|
|
|
* @return \phpbu\App\Cli\Executable\Mongodump |
207
|
|
|
*/ |
208
|
4 |
|
public function excludeCollectionsWithPrefix(array $prefixes) : Mongodump |
209
|
|
|
{ |
210
|
4 |
|
$this->excludeCollectionsWithPrefix = $prefixes; |
211
|
4 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Mongodump CommandLine generator. |
216
|
|
|
* |
217
|
|
|
* @return \SebastianFeldmann\Cli\CommandLine |
218
|
|
|
* @throws \phpbu\App\Exception |
219
|
|
|
*/ |
220
|
13 |
|
protected function createCommandLine() : CommandLine |
221
|
|
|
{ |
222
|
13 |
|
if (empty($this->dumpDir)) { |
223
|
1 |
|
throw new Exception('no directory to dump to'); |
224
|
|
|
} |
225
|
12 |
|
$process = new CommandLine(); |
226
|
12 |
|
$cmd = new Cmd($this->binary); |
227
|
12 |
|
$process->addCommand($cmd); |
228
|
|
|
|
229
|
12 |
|
$cmd->addOption('--out', $this->dumpDir, ' '); |
230
|
12 |
|
$cmd->addOptionIfNotEmpty('--ipv6', $this->useIPv6, false); |
231
|
12 |
|
$cmd->addOptionIfNotEmpty('--host', $this->host, true, ' '); |
232
|
12 |
|
$cmd->addOptionIfNotEmpty('--username', $this->user, true, ' '); |
233
|
12 |
|
$cmd->addOptionIfNotEmpty('--password', $this->password, true, ' '); |
234
|
12 |
|
$cmd->addOptionIfNotEmpty('--authenticationDatabase', $this->authenticationDatabase, true, ' '); |
235
|
|
|
|
236
|
12 |
|
if (count($this->databases)) { |
237
|
1 |
|
foreach ($this->databases as $db) { |
238
|
1 |
|
$cmd->addOption('--db', $db, ' '); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
12 |
|
if (count($this->collections)) { |
243
|
1 |
|
foreach ($this->collections as $col) { |
244
|
1 |
|
$cmd->addOption('--collection', $col, ' '); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
12 |
|
$cmd->addOptionIfNotEmpty('--excludeCollection', $this->excludeCollections); |
249
|
12 |
|
$cmd->addOptionIfNotEmpty('--excludeCollectionWithPrefix', $this->excludeCollectionsWithPrefix); |
250
|
|
|
|
251
|
12 |
|
return $process; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|