1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Cli\Executable; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Executable; |
5
|
|
|
use phpbu\App\Cli\Process; |
6
|
|
|
use phpbu\App\Exception; |
7
|
|
|
use SebastianFeldmann\Cli\CommandLine; |
8
|
|
|
use SebastianFeldmann\Cli\Command\Executable as Cmd; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Rsync executable class. |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
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 Rsync extends Abstraction implements Executable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Source |
25
|
|
|
* |
26
|
|
|
* @var \phpbu\App\Cli\Executable\Rsync\Location |
27
|
|
|
*/ |
28
|
|
|
private $source; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Target |
32
|
|
|
* |
33
|
|
|
* @var \phpbu\App\Cli\Executable\Rsync\Location |
34
|
|
|
*/ |
35
|
|
|
private $target; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Raw args |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $args; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Files to ignore, extracted from config string separated by ":" |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $excludes = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Remove deleted files remotely as well |
53
|
|
|
* |
54
|
|
|
* @var boolean |
55
|
|
|
*/ |
56
|
|
|
protected $delete; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Compress data. |
60
|
|
|
* |
61
|
|
|
* @var boolean |
62
|
|
|
*/ |
63
|
|
|
protected $compressed = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Constructor. |
67
|
|
|
* |
68
|
|
|
* @param string $path |
69
|
|
|
*/ |
70
|
|
|
public function __construct(string $path = '') |
71
|
|
|
{ |
72
|
|
|
$this->setup('rsync', $path); |
73
|
|
|
$this->source = new Rsync\Location(); |
74
|
|
|
$this->target = new Rsync\Location(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set custom args |
79
|
|
|
* |
80
|
|
|
* @param string $args |
81
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
82
|
|
|
*/ |
83
|
15 |
|
public function useArgs(string $args) : Rsync |
84
|
|
|
{ |
85
|
15 |
|
$this->args = $args; |
86
|
15 |
|
return $this; |
87
|
15 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set source user. |
91
|
|
|
* |
92
|
|
|
* @param string $user |
93
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
94
|
|
|
*/ |
95
|
2 |
|
public function fromUser(string $user) : Rsync |
96
|
|
|
{ |
97
|
2 |
|
$this->source->setUser($user); |
98
|
2 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set source host. |
103
|
|
|
* |
104
|
|
|
* @param string $host |
105
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
106
|
|
|
*/ |
107
|
8 |
|
public function fromHost(string $host) : Rsync |
108
|
|
|
{ |
109
|
8 |
|
$this->source->setHost($host); |
110
|
8 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set source path. |
115
|
|
|
* |
116
|
|
|
* @param string $path |
117
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
118
|
|
|
*/ |
119
|
4 |
|
public function fromPath(string $path) : Rsync |
120
|
|
|
{ |
121
|
4 |
|
$this->source->setPath($path); |
122
|
4 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Use compression. |
127
|
|
|
* |
128
|
|
|
* @param bool $bool |
129
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
130
|
|
|
*/ |
131
|
5 |
|
public function compressed(bool $bool) : Rsync |
132
|
|
|
{ |
133
|
5 |
|
$this->compressed = $bool; |
134
|
5 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sync to host. |
139
|
|
|
* |
140
|
|
|
* @param string $host |
141
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
142
|
|
|
*/ |
143
|
10 |
|
public function toHost(string $host) : Rsync |
144
|
|
|
{ |
145
|
10 |
|
$this->target->setHost($host); |
146
|
10 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set path to sync to. |
151
|
|
|
* |
152
|
|
|
* @param string $path |
153
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
154
|
|
|
*/ |
155
|
5 |
|
public function toPath(string $path) : Rsync |
156
|
|
|
{ |
157
|
5 |
|
$this->target->setPath($path); |
158
|
5 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set user to connect as. |
163
|
|
|
* |
164
|
|
|
* @param string $user |
165
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
166
|
|
|
*/ |
167
|
4 |
|
public function toUser(string $user) : Rsync |
168
|
|
|
{ |
169
|
4 |
|
$this->target->setUser($user); |
170
|
4 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Exclude files. |
175
|
|
|
* |
176
|
|
|
* @param array $excludes |
177
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
178
|
|
|
*/ |
179
|
4 |
|
public function exclude(array $excludes) : Rsync |
180
|
|
|
{ |
181
|
4 |
|
$this->excludes = $excludes; |
182
|
4 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Use --delete option. |
187
|
|
|
* |
188
|
|
|
* @param bool $bool |
189
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
190
|
|
|
*/ |
191
|
11 |
|
public function removeDeleted(bool $bool) : Rsync |
192
|
|
|
{ |
193
|
11 |
|
$this->delete = $bool; |
194
|
11 |
|
return $this; |
195
|
11 |
|
} |
196
|
|
|
|
197
|
11 |
|
/** |
198
|
2 |
|
* Rsync CommandLine generator. |
199
|
2 |
|
* |
200
|
9 |
|
* @return \SebastianFeldmann\Cli\CommandLine |
201
|
1 |
|
* @throws \phpbu\App\Exception |
202
|
|
|
*/ |
203
|
8 |
|
protected function createCommandLine() : CommandLine |
204
|
1 |
|
{ |
205
|
|
|
$process = new CommandLine(); |
206
|
|
|
$cmd = new Cmd($this->binary); |
207
|
7 |
|
$process->addCommand($cmd); |
208
|
|
|
|
209
|
|
|
if (!empty($this->args)) { |
210
|
7 |
|
$cmd->addOption($this->args); |
211
|
7 |
|
} else { |
212
|
|
|
if (!$this->source->isValid()) { |
213
|
7 |
|
throw new Exception('source path is missing'); |
214
|
2 |
|
} |
215
|
2 |
|
if (!$this->target->isValid()) { |
216
|
2 |
|
throw new Exception('target path is missing'); |
217
|
2 |
|
} |
218
|
|
|
|
219
|
7 |
|
// use archive mode, verbose and compress if not already done |
220
|
7 |
|
$options = '-av' . ($this->compressed ? 'z' : ''); |
221
|
|
|
$cmd->addOption($options); |
222
|
|
|
$this->configureExcludes($cmd, $this->excludes); |
223
|
|
|
$cmd->addOptionIfNotEmpty('--delete', $this->delete, false); |
224
|
7 |
|
$cmd->addArgument($this->source->toString()); |
225
|
|
|
$cmd->addArgument($this->target->toString()); |
226
|
7 |
|
} |
227
|
|
|
|
228
|
7 |
|
return $process; |
229
|
|
|
} |
230
|
|
|
|
231
|
9 |
|
/** |
232
|
|
|
* Configure excludes. |
233
|
|
|
* |
234
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
235
|
|
|
* @param array $excludes |
236
|
|
|
*/ |
237
|
|
|
protected function configureExcludes(Cmd $cmd, array $excludes) |
238
|
|
|
{ |
239
|
11 |
|
foreach ($excludes as $ex) { |
240
|
|
|
$cmd->addOption('--exclude', $ex); |
241
|
11 |
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|