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
|
|
|
* Elasticdump 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.1.0 |
20
|
|
|
*/ |
21
|
|
|
class Elasticdump extends Abstraction implements Executable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Host to connect to |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $host; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* User to connect with |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $user; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Password to authenticate with |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $password; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Specific index to backup |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $index; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Whether to backup the mapping or data |
53
|
|
|
* --type |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $type; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* File to dump to. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $dumpPathname; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Constructor. |
68
|
|
|
* |
69
|
|
|
* @param string $path |
70
|
|
|
*/ |
71
|
12 |
|
public function __construct($path = null) |
72
|
|
|
{ |
73
|
12 |
|
$this->cmd = 'elasticdump'; |
74
|
12 |
|
parent::__construct($path); |
75
|
12 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set host to get data from. |
79
|
|
|
* |
80
|
|
|
* @param string $host |
81
|
|
|
* @return \phpbu\App\Cli\Executable\Elasticdump |
82
|
|
|
*/ |
83
|
11 |
|
public function useHost($host) |
84
|
|
|
{ |
85
|
11 |
|
$this->host = $host; |
86
|
11 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set index to dump. |
91
|
|
|
* |
92
|
|
|
* @param string $index |
93
|
|
|
* @return \phpbu\App\Cli\Executable\Elasticdump |
94
|
|
|
*/ |
95
|
4 |
|
public function dumpIndex($index) |
96
|
|
|
{ |
97
|
4 |
|
$this->index = $index; |
98
|
4 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set dump type. |
103
|
|
|
* |
104
|
|
|
* @param string $type |
105
|
|
|
* @return \phpbu\App\Cli\Executable\Elasticdump |
106
|
|
|
*/ |
107
|
4 |
|
public function dumpType($type) |
108
|
|
|
{ |
109
|
4 |
|
$this->type = $type; |
110
|
4 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set file to dump to. |
115
|
|
|
* |
116
|
|
|
* @param string $pathname |
117
|
|
|
* @return \phpbu\App\Cli\Executable\Elasticdump |
118
|
|
|
*/ |
119
|
10 |
|
public function dumpTo($pathname) |
120
|
|
|
{ |
121
|
10 |
|
$this->dumpPathname = $pathname; |
122
|
10 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set elastic credentials. |
127
|
|
|
* |
128
|
|
|
* @param string $user |
129
|
|
|
* @param string $password |
130
|
|
|
* @return \phpbu\App\Cli\Executable\Elasticdump |
131
|
|
|
*/ |
132
|
5 |
|
public function credentials($user = null, $password = null) |
133
|
|
|
{ |
134
|
5 |
|
$this->user = $user; |
135
|
5 |
|
$this->password = $password; |
136
|
5 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Subclass Process generator. |
141
|
|
|
* |
142
|
|
|
* @return \phpbu\App\Cli\Process |
143
|
|
|
* @throws \phpbu\App\Exception |
144
|
|
|
*/ |
145
|
12 |
|
protected function createProcess() |
146
|
|
|
{ |
147
|
12 |
|
if (empty($this->host)) { |
148
|
1 |
|
throw new Exception('host is mandatory'); |
149
|
|
|
} |
150
|
11 |
|
if (empty($this->dumpPathname)) { |
151
|
1 |
|
throw new Exception('no file to dump to'); |
152
|
|
|
} |
153
|
|
|
|
154
|
10 |
|
$process = new Process(); |
155
|
10 |
|
$cmd = new Cmd($this->binary); |
156
|
10 |
|
$process->addCommand($cmd); |
157
|
|
|
|
158
|
|
|
$cmd->addOption('--input', $this->generateNodeUrl($this->host, $this->user, $this->password, $this->index)); |
159
|
10 |
|
$cmd->addOptionIfNotEmpty('--type', $this->type); |
160
|
8 |
|
$cmd->addOption('--output', $this->dumpPathname); |
161
|
|
|
|
162
|
8 |
|
return $process; |
163
|
|
|
} |
164
|
10 |
|
|
165
|
10 |
|
/** |
166
|
10 |
|
* Create a elastic node url. |
167
|
|
|
* |
168
|
10 |
|
* @param string $host |
169
|
|
|
* @param string $user |
170
|
|
|
* @param string $password |
171
|
|
|
* @param string $index |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
private function generateNodeUrl($host, $user = null, $password = null, $index = null) |
175
|
|
|
{ |
176
|
|
|
$parsed = parse_url($host); |
177
|
|
|
|
178
|
|
|
if (!isset($parsed['scheme'])) { |
179
|
|
|
$parsed = parse_url('http://' . $host); |
180
|
10 |
|
} |
181
|
|
|
|
182
|
10 |
|
$url = ''; |
183
|
|
|
|
184
|
10 |
|
if (isset($parsed['scheme'])) { |
185
|
7 |
|
$url .= $parsed['scheme'] . '://'; |
186
|
7 |
|
} |
187
|
|
|
|
188
|
10 |
|
if (!empty($user)) { |
189
|
|
|
$url .= $user; |
190
|
10 |
|
} |
191
|
10 |
|
|
192
|
10 |
|
if (!empty($password)) { |
193
|
|
|
$url .= ':' . $password; |
194
|
10 |
|
} |
195
|
3 |
|
|
196
|
3 |
|
if (!empty($user) || !empty($password)) { |
197
|
|
|
$url .= '@'; |
198
|
10 |
|
} |
199
|
1 |
|
|
200
|
1 |
|
$url .= $parsed['host']; |
201
|
|
|
|
202
|
10 |
|
if (isset($parsed['port'])) { |
203
|
3 |
|
$url .= ':' . $parsed['port']; |
204
|
3 |
|
} |
205
|
|
|
|
206
|
10 |
|
if (!empty($parsed['path'])) { |
207
|
|
|
$url .= $parsed['path']; |
208
|
10 |
|
} |
209
|
10 |
|
|
210
|
10 |
|
if (substr($url, -1) != '/') { |
211
|
|
|
$url .= '/'; |
212
|
10 |
|
} |
213
|
1 |
|
|
214
|
1 |
|
if ($index !== null) { |
215
|
|
|
$url .= $index; |
216
|
10 |
|
} |
217
|
10 |
|
|
218
|
10 |
|
return $url; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|