forked from sebastianfeldmann/phpbu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElasticdump.php
More file actions
153 lines (136 loc) · 3.73 KB
/
Copy pathElasticdump.php
File metadata and controls
153 lines (136 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace phpbu\App\Backup\Source;
use phpbu\App\Backup\Target;
use phpbu\App\Cli\Executable;
use phpbu\App\Exception;
use phpbu\App\Result;
use phpbu\App\Util;
/**
* Elasticdump source class
*
* @package phpbu
* @subpackage Backup
* @author Francis Chuang <francis.chuang@gmail.com>
* @author Sebastian Feldmann <sebastian@phpbu.de>
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://phpbu.de/
* @since Class available since Release 2.0.1
*/
class Elasticdump extends SimulatorExecutable implements Simulator
{
/**
* Path to elasticdump binary
*
* @var string
*/
private $pathToElasticdump;
/**
* Host to connect to
*
* @var string
*/
private $host;
/**
* User to connect with
*
* @var string
*/
private $user;
/**
* Password to authenticate with
*
* @var string
*/
private $password;
/**
* Specific index to backup
*
* @var string
*/
private $index;
/**
* Whether to backup the mapping or data
* --type
*
* @var string
*/
private $type;
/**
* Number of documents to dump.
* --limit
*
* @var string
*/
private $limit;
/**
* Setup
*
* @see \phpbu\App\Backup\Source
* @param array $conf
*/
public function setup(array $conf = [])
{
$this->setupSourceData($conf);
$this->pathToElasticdump = Util\Arr::getValue($conf, 'pathToElasticdump', '');
$this->host = Util\Arr::getValue($conf, 'host', 'http://localhost:9200');
$this->user = Util\Arr::getValue($conf, 'user', '');
$this->password = Util\Arr::getValue($conf, 'password', '');
}
/**
* Get index and type
*
* @param array $conf
*/
protected function setupSourceData(array $conf)
{
$this->index = Util\Arr::getValue($conf, 'index', '');
$this->type = Util\Arr::getValue($conf, 'type', '');
$this->limit = Util\Arr::getValue($conf, 'limit', '');
}
/**
* Execute the backup
*
* @see \phpbu\App\Backup\Source
* @param \phpbu\App\Backup\Target $target
* @param \phpbu\App\Result $result
* @return \phpbu\App\Backup\Source\Status
* @throws \phpbu\App\Exception
*/
public function backup(Target $target, Result $result) : Status
{
$elasticdump = $this->execute($target);
$result->debug($elasticdump->getCmdPrintable());
if (!$elasticdump->isSuccessful()) {
throw new Exception('elasticdump failed: ' . $elasticdump->getStdErr());
}
return $this->createStatus($target);
}
/**
* Create the Executable to run the elasticdump command
*
* @param \phpbu\App\Backup\Target $target
* @return \phpbu\App\Cli\Executable
*/
protected function createExecutable(Target $target) : Executable
{
$executable = new Executable\Elasticdump($this->pathToElasticdump);
$executable->useHost($this->host)
->credentials($this->user, $this->password)
->dumpIndex($this->index)
->dumpType($this->type)
->limit($this->limit)
->dumpTo($target->getPathnamePlain());
return $executable;
}
/**
* Create backup status
*
* @param \phpbu\App\Backup\Target $target
* @return \phpbu\App\Backup\Source\Status
*/
protected function createStatus(Target $target) : Status
{
return Status::create()->uncompressedFile($target->getPathnamePlain());
}
}