1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace phpbu\App\Backup\Source; |
4
|
|
|
|
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Cli\Executable; |
7
|
|
|
use phpbu\App\Exception; |
8
|
|
|
use phpbu\App\Result; |
9
|
|
|
use phpbu\App\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Ldapdump source class. |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @author Julian Marié <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
* @since Class available since Release 2.1.12 |
22
|
|
|
*/ |
23
|
|
|
class Ldapdump extends SimulatorExecutable implements Simulator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Executable to handle ldapDb command. |
27
|
|
|
* |
28
|
|
|
* @var \phpbu\App\Cli\Executable\Ldapdump |
29
|
|
|
*/ |
30
|
|
|
protected $executable; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Path to executable. |
34
|
|
|
* |
35
|
|
|
* @var string $pathToLdapdump |
36
|
|
|
*/ |
37
|
|
|
private $pathToLdapdump; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Host to connect to |
41
|
|
|
* -h <hostname> |
42
|
|
|
* |
43
|
|
|
* @var string $host |
44
|
|
|
*/ |
45
|
|
|
private $host; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Port to connect to |
49
|
|
|
* -p <port> |
50
|
|
|
* |
51
|
|
|
* @var string $port |
52
|
|
|
*/ |
53
|
|
|
private $port; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Basename |
57
|
|
|
* -b <basename> |
58
|
|
|
* |
59
|
|
|
* @var string $searchBase |
60
|
|
|
*/ |
61
|
|
|
private $searchBase; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* BindDn to connect with |
65
|
|
|
* -D <DN> |
66
|
|
|
* |
67
|
|
|
* @var string $bindDn |
68
|
|
|
*/ |
69
|
|
|
private $bindDn; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Password to authenticate with |
73
|
|
|
* -w <password> |
74
|
|
|
* |
75
|
|
|
* @var string $password |
76
|
|
|
*/ |
77
|
|
|
private $password; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Filter |
81
|
|
|
* <filter> |
82
|
|
|
* |
83
|
|
|
* @var string $filter |
84
|
|
|
*/ |
85
|
|
|
private $filter; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Attributes |
89
|
|
|
* <attrs> |
90
|
|
|
* |
91
|
|
|
* @var array $attrs |
92
|
|
|
*/ |
93
|
|
|
private $attrs; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Setup. |
97
|
|
|
* |
98
|
|
|
* @see \phpbu\App\Backup\Source |
99
|
|
|
* @param array $conf |
100
|
|
|
* @throws \phpbu\App\Exception |
101
|
|
|
*/ |
102
|
|
|
public function setup(array $conf = []) |
103
|
|
|
{ |
104
|
|
|
$this->setupSourceData($conf); |
105
|
|
|
|
106
|
|
|
$this->pathToLdapdump = Util\Arr::getValue($conf, 'pathToLdapdump', ''); |
107
|
|
|
$this->host = Util\Arr::getValue($conf, 'host', 'localhost'); |
108
|
|
|
$this->port = Util\Arr::getValue($conf, 'port', '389'); |
109
|
|
|
$this->searchBase = Util\Arr::getValue($conf, 'searchBase', 'ou=Users,dc=fr'); |
110
|
|
|
$this->bindDn = Util\Arr::getValue($conf, 'bindDn', 'cn=admin,dc=fr'); |
111
|
|
|
$this->password = Util\Arr::getValue($conf, 'password', ''); |
112
|
|
|
$this->filter = Util\Arr::getValue($conf, 'filter', ''); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get attributes to backup |
117
|
|
|
* |
118
|
|
|
* @param array $conf |
119
|
|
|
*/ |
120
|
|
|
protected function setupSourceData(array $conf) |
121
|
|
|
{ |
122
|
|
|
$this->attrs = Util\Str::toList(Util\Arr::getValue($conf, 'attrs', '')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Execute the backup. |
127
|
|
|
* |
128
|
|
|
* @see \phpbu\App\Backup\Source |
129
|
|
|
* @param \phpbu\App\Backup\Target $target |
130
|
|
|
* @param \phpbu\App\Result $result |
131
|
|
|
* @return \phpbu\App\Backup\Source\Status |
132
|
|
|
* @throws \phpbu\App\Exception |
133
|
|
|
*/ |
134
|
|
|
public function backup(Target $target, Result $result) : Status |
135
|
|
|
{ |
136
|
|
|
$ldapdb = $this->execute($target); |
137
|
|
|
|
138
|
|
|
$result->debug($this->getExecutable($target)->getCommandPrintable()); |
139
|
|
|
|
140
|
|
|
if (!$ldapdb->isSuccessful()) { |
141
|
|
|
throw new Exception('ldapdb dump failed:' . $ldapdb->getStdErr()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->createStatus($target); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Create the Executable to run the ldapdump command. |
149
|
|
|
* |
150
|
|
|
* @param \phpbu\App\Backup\Target $target |
151
|
|
|
* @return \phpbu\App\Cli\Executable |
152
|
|
|
*/ |
153
|
|
|
protected function createExecutable(Target $target) : Executable |
154
|
|
|
{ |
155
|
|
|
$executable = new Executable\Ldapdump($this->pathToLdapdump); |
156
|
|
|
$executable |
157
|
|
|
->credentials($this->bindDn, $this->password) |
158
|
|
|
->useHost($this->host) |
159
|
|
|
->usePort($this->port) |
|
|
|
|
160
|
|
|
->useSearchBase($this->searchBase) |
161
|
|
|
->useFilter($this->filter) |
162
|
|
|
->useAttributes($this->attrs) |
163
|
|
|
->dumpTo($this->getDumpTarget($target)); |
164
|
|
|
; |
165
|
|
|
// if compression is active and commands can be piped |
166
|
|
|
if ($this->isHandlingCompression($target)) { |
167
|
|
|
$executable->compressOutput($target->getCompression()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $executable; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Create backup status. |
175
|
|
|
* |
176
|
|
|
* @param \phpbu\App\Backup\Target $target |
177
|
|
|
* @return \phpbu\App\Backup\Source\Status |
178
|
|
|
*/ |
179
|
|
|
protected function createStatus(Target $target) : Status |
180
|
|
|
{ |
181
|
|
|
// if compression is active and commands can be piped |
182
|
|
|
// compression is handled via pipe |
183
|
|
|
if ($this->isHandlingCompression($target)) { |
184
|
|
|
return Status::create(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// default create uncompressed dump file |
188
|
|
|
return Status::create()->uncompressedFile($this->getDumpTarget($target)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Can compression be handled via pipe operator. |
193
|
|
|
* |
194
|
|
|
* @param \phpbu\App\Backup\Target $target |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
private function isHandlingCompression(Target $target) : bool |
198
|
|
|
{ |
199
|
|
|
return $target->shouldBeCompressed() && Util\Cli::canPipe() && $target->getCompression()->isPipeable(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return dump target path. |
204
|
|
|
* |
205
|
|
|
* @param \phpbu\App\Backup\Target $target |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
private function getDumpTarget(Target $target) : string |
209
|
|
|
{ |
210
|
|
|
return $target->getPathnamePlain(); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|