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