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
|
|
|
* RedisCli 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.12 |
19
|
|
|
*/ |
20
|
|
|
class RedisCli extends Abstraction implements Executable |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* List of implemented redis commands |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $availableCommands = ['BGSAVE' => true, 'LASTSAVE' => true]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Redis command to execute |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $command; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Host to connect to |
38
|
|
|
* -h |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $host; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Port to connect to |
46
|
|
|
* -p |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $port; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Password to connect |
55
|
|
|
* -a |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $password; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
* |
64
|
|
|
* @param string $path |
65
|
|
|
*/ |
66
|
|
|
public function __construct($path = null) |
67
|
|
|
{ |
68
|
|
|
$this->cmd = 'redis-cli'; |
69
|
|
|
parent::__construct($path); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the redis-cli command to execute. |
74
|
|
|
* |
75
|
|
|
* @param string $command |
76
|
|
|
* @return \phpbu\App\Cli\Executable\RedisCli |
77
|
|
|
* @throws \phpbu\App\Exception |
78
|
|
|
*/ |
79
|
|
|
public function runCommand($command) |
80
|
|
|
{ |
81
|
|
|
if(!isset($this->availableCommands[$command])) { |
82
|
|
|
throw new Exception('Unknown redis-cli command'); |
83
|
|
|
} |
84
|
|
|
$this->command = $command; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Execute redis-cli BGSAVE command. |
90
|
|
|
* |
91
|
|
|
* @return \phpbu\App\Cli\Executable\RedisCli |
92
|
|
|
* @throws \phpbu\App\Exception |
93
|
|
|
*/ |
94
|
|
|
public function backup() |
95
|
|
|
{ |
96
|
|
|
return $this->runCommand('BGSAVE'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Execute redis-cli LASTSAVE command. |
101
|
|
|
* |
102
|
|
|
* @return \phpbu\App\Cli\Executable\RedisCli |
103
|
|
|
* @throws \phpbu\App\Exception |
104
|
|
|
*/ |
105
|
|
|
public function lastBackupTime() |
106
|
|
|
{ |
107
|
|
|
return $this->runCommand('LASTSAVE'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Host to connect to. |
112
|
|
|
* |
113
|
|
|
* @param string $host |
114
|
|
|
* @return \phpbu\App\Cli\Executable\RedisCli |
115
|
|
|
*/ |
116
|
|
|
public function useHost($host) |
117
|
|
|
{ |
118
|
|
|
$this->host = $host; |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Port to connect to. |
124
|
|
|
* |
125
|
|
|
* @param integer $port |
126
|
|
|
* @return \phpbu\App\Cli\Executable\RedisCli |
127
|
|
|
*/ |
128
|
|
|
public function usePort($port) |
129
|
|
|
{ |
130
|
|
|
$this->port = $port; |
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Password to authenticate. |
136
|
|
|
* |
137
|
|
|
* @param string $password |
138
|
|
|
* @return \phpbu\App\Cli\Executable\RedisCli |
139
|
|
|
*/ |
140
|
|
|
public function usePassword($password) |
141
|
|
|
{ |
142
|
|
|
$this->password = $password; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* RedisCli Process generator. |
148
|
|
|
* |
149
|
|
|
* @return \phpbu\App\Cli\Process |
150
|
|
|
* @throws \phpbu\App\Exception |
151
|
|
|
*/ |
152
|
|
|
protected function createProcess() |
153
|
|
|
{ |
154
|
|
|
if (empty($this->command)) { |
155
|
|
|
throw new Exception('Choose command to execute'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$process = new Process(); |
159
|
|
|
$cmd = new Cmd($this->binary); |
160
|
|
|
$process->addCommand($cmd); |
161
|
|
|
|
162
|
|
|
// no std error unless it is activated |
163
|
|
|
if (!$this->showStdErr) { |
164
|
|
|
$cmd->silence(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->setOptions($cmd); |
168
|
|
|
$cmd->addOption($this->command); |
169
|
|
|
|
170
|
|
|
return $process; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the openssl command line options |
175
|
|
|
* |
176
|
|
|
* @param \phpbu\App\Cli\Cmd $cmd |
177
|
|
|
*/ |
178
|
|
|
protected function setOptions(Cmd $cmd) |
179
|
|
|
{ |
180
|
|
|
$cmd->addOptionIfNotEmpty('-h', $this->host, true, ' '); |
181
|
|
|
$cmd->addOptionIfNotEmpty('-p', $this->port, true, ' '); |
182
|
|
|
$cmd->addOptionIfNotEmpty('-a', $this->password, true, ' '); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.