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