1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of rsync-lib |
5
|
|
|
* |
6
|
|
|
* (c) Alberto Fernández <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AFM\Rsync; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Rsync wrapper. Many options are not implemented, |
16
|
|
|
* but you can use setOptionalParameters |
17
|
|
|
* |
18
|
|
|
* @author Alberto Fernández <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Rsync extends AbstractProtocol |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $executable = "/usr/bin/rsync"; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $archive = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $skipNewerFiles = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $followSymLinks = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
protected $dryRun = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $optionalParameters = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $verbose = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $deleteFromTarget = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
protected $deleteExcluded = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
protected $exclude = array(); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $excludeFrom = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
protected $recursive = true; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
protected $times = false; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var bool |
89
|
|
|
*/ |
90
|
|
|
protected $showOutput = true; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var bool |
94
|
|
|
*/ |
95
|
|
|
protected $compression = false; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var bool |
99
|
|
|
*/ |
100
|
|
|
protected $remoteOrigin = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var bool |
104
|
|
|
*/ |
105
|
|
|
protected $removeSource = false; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var bool |
109
|
|
|
*/ |
110
|
|
|
protected $info = false; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var bool |
114
|
|
|
*/ |
115
|
|
|
protected $compareDest = false; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var SSH |
119
|
|
|
*/ |
120
|
|
|
protected $ssh; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Injects and validates config |
124
|
|
|
* |
125
|
|
|
* @param array $options |
126
|
|
|
*/ |
127
|
|
|
public function __construct(Array $options = array()) |
128
|
|
|
{ |
129
|
|
|
$this->setOption($options, 'executable', 'setExecutable'); |
130
|
|
|
$this->setOption($options, 'archive', 'setArchive'); |
131
|
|
|
$this->setOption($options, 'update', 'setSkipNewerFiles'); |
132
|
|
|
$this->setOption($options, 'follow_symlinks', 'setFollowSymLinks'); |
133
|
|
|
$this->setOption($options, 'dry_run', 'setDryRun'); |
134
|
|
|
$this->setOption($options, 'option_parameters', 'setOptionalParameters'); |
135
|
|
|
$this->setOption($options, 'verbose', 'setVerbose'); |
136
|
|
|
$this->setOption($options, 'delete_from_target', 'setDeleteFromTarget'); |
137
|
|
|
$this->setOption($options, 'delete_excluded', 'setDeleteExcluded'); |
138
|
|
|
$this->setOption($options, 'exclude', 'setExclude'); |
139
|
|
|
$this->setOption($options, 'excludeFrom', 'setExcludeFrom'); |
140
|
|
|
$this->setOption($options, 'recursive', 'setRecursive'); |
141
|
|
|
$this->setOption($options, 'times', 'setTimes'); |
142
|
|
|
$this->setOption($options, 'show_output', 'setShowOutput'); |
143
|
|
|
$this->setOption($options, 'ssh', 'setSshOptions'); |
144
|
|
|
$this->setOption($options, 'compression', 'setCompression'); |
145
|
|
|
$this->setOption($options, 'remote_origin', 'setRemoteOrigin'); |
146
|
|
|
$this->setOption($options, 'remove_source', 'setRemoveSource'); |
147
|
|
|
$this->setOption($options, 'info', 'setInfo'); |
148
|
|
|
$this->setOption($options, 'compare_dest', 'setCompareDest'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $options |
153
|
|
|
*/ |
154
|
|
|
public function setSshOptions($options) |
155
|
|
|
{ |
156
|
|
|
if(is_null($this->ssh)) |
157
|
|
|
$this->ssh = new SSH($options); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sync $origin directory with $target one. |
162
|
|
|
* If SSH was configured, you must use absolute path |
163
|
|
|
* in the target directory |
164
|
|
|
* |
165
|
|
|
* @param $origin |
166
|
|
|
* @param $target |
167
|
|
|
* |
168
|
|
|
* @throws \InvalidArgumentException If the command failed |
169
|
|
|
*/ |
170
|
|
|
public function sync($origin, $target) |
171
|
|
|
{ |
172
|
|
|
$command = $this->getCommand($origin, $target); |
173
|
|
|
|
174
|
|
|
$command->execute($this->showOutput); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getExecutable() |
181
|
|
|
{ |
182
|
|
|
return $this->executable; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function getArchive() |
189
|
|
|
{ |
190
|
|
|
return $this->archive; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param $archive |
195
|
|
|
*/ |
196
|
|
|
public function setArchive($archive) |
197
|
|
|
{ |
198
|
|
|
$this->archive = $archive; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param $skipNewerFiles |
203
|
|
|
*/ |
204
|
|
|
public function setSkipNewerFiles($skipNewerFiles) |
205
|
|
|
{ |
206
|
|
|
$this->skipNewerFiles = $skipNewerFiles; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function getSkipNewerFiles() |
213
|
|
|
{ |
214
|
|
|
return $this->skipNewerFiles; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param $followSymLinks |
219
|
|
|
*/ |
220
|
|
|
public function setFollowSymLinks($followSymLinks) |
221
|
|
|
{ |
222
|
|
|
$this->followSymLinks = $followSymLinks; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function getFollowSymLinks() |
229
|
|
|
{ |
230
|
|
|
return $this->followSymLinks; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param $dryRun |
235
|
|
|
*/ |
236
|
|
|
public function setDryRun($dryRun) |
237
|
|
|
{ |
238
|
|
|
$this->dryRun = $dryRun; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
|
|
public function getDryRun() |
245
|
|
|
{ |
246
|
|
|
return $this->dryRun; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param $optionalParameters |
251
|
|
|
*/ |
252
|
|
|
public function setOptionalParameters($optionalParameters) |
253
|
|
|
{ |
254
|
|
|
$this->optionalParameters = $optionalParameters; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function getOptionalParameters() |
261
|
|
|
{ |
262
|
|
|
return $this->optionalParameters; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param $verbose |
267
|
|
|
*/ |
268
|
|
|
public function setVerbose($verbose) |
269
|
|
|
{ |
270
|
|
|
$this->verbose = $verbose; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
|
|
public function getVerbose() |
277
|
|
|
{ |
278
|
|
|
return $this->verbose; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param $deleteExcluded |
283
|
|
|
*/ |
284
|
|
|
public function setDeleteExcluded($deleteExcluded) |
285
|
|
|
{ |
286
|
|
|
$this->deleteExcluded = $deleteExcluded; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return bool |
291
|
|
|
*/ |
292
|
|
|
public function getDeleteExcluded() |
293
|
|
|
{ |
294
|
|
|
return $this->deleteExcluded; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param $deleteFromTarget |
299
|
|
|
*/ |
300
|
|
|
public function setDeleteFromTarget($deleteFromTarget) |
301
|
|
|
{ |
302
|
|
|
$this->deleteFromTarget = $deleteFromTarget; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return bool |
307
|
|
|
*/ |
308
|
|
|
public function getDeleteFromTarget() |
309
|
|
|
{ |
310
|
|
|
return $this->deleteFromTarget; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param $exclude |
315
|
|
|
*/ |
316
|
|
|
public function setExclude($exclude) |
317
|
|
|
{ |
318
|
|
|
$this->exclude = $exclude; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return array |
323
|
|
|
*/ |
324
|
|
|
public function getExclude() |
325
|
|
|
{ |
326
|
|
|
return $this->exclude; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param $exclude |
331
|
|
|
*/ |
332
|
|
|
public function setExcludeFrom($excludeFrom) |
333
|
|
|
{ |
334
|
|
|
$this->excludeFrom = $excludeFrom; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
public function getExcludeFrom() |
341
|
|
|
{ |
342
|
|
|
return $this->excludeFrom; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param $recursive |
347
|
|
|
*/ |
348
|
|
|
public function setRecursive($recursive) |
349
|
|
|
{ |
350
|
|
|
$this->recursive = $recursive; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @return bool |
355
|
|
|
*/ |
356
|
|
|
public function getRecursive() |
357
|
|
|
{ |
358
|
|
|
return $this->recursive; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param bool $times |
363
|
|
|
*/ |
364
|
|
|
public function setTimes($times) |
365
|
|
|
{ |
366
|
|
|
$this->times = $times; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @return bool |
371
|
|
|
*/ |
372
|
|
|
public function getTimes() |
373
|
|
|
{ |
374
|
|
|
return $this->times; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param $showOutput |
379
|
|
|
*/ |
380
|
|
|
public function setShowOutput($showOutput) |
381
|
|
|
{ |
382
|
|
|
$this->showOutput = $showOutput; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @return bool |
387
|
|
|
*/ |
388
|
|
|
public function getShowOutput() |
389
|
|
|
{ |
390
|
|
|
return $this->showOutput; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param $compression |
395
|
|
|
*/ |
396
|
|
|
public function setCompression($compression) |
397
|
|
|
{ |
398
|
|
|
$this->compression = $compression; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @return bool |
403
|
|
|
*/ |
404
|
|
|
public function getCompression() |
405
|
|
|
{ |
406
|
|
|
return $this->compression; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @param $remoteOrigin |
411
|
|
|
*/ |
412
|
|
|
public function setRemoteOrigin($remoteOrigin) |
413
|
|
|
{ |
414
|
|
|
$this->remoteOrigin = (bool) $remoteOrigin; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @return bool |
419
|
|
|
*/ |
420
|
|
|
public function getRemoteOrigin() |
421
|
|
|
{ |
422
|
|
|
return $this->remoteOrigin; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* @param $removeSource |
427
|
|
|
*/ |
428
|
|
|
public function setRemoveSource($removeSource) |
429
|
|
|
{ |
430
|
|
|
$this->removeSource = (bool) $removeSource; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @return bool |
435
|
|
|
*/ |
436
|
|
|
public function getRemoveSource() |
437
|
|
|
{ |
438
|
|
|
return $this->removeSource; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @param $info |
443
|
|
|
*/ |
444
|
|
|
public function setInfo($info) |
445
|
|
|
{ |
446
|
|
|
$this->info = $info; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* @return bool |
451
|
|
|
*/ |
452
|
|
|
public function getInfo() |
453
|
|
|
{ |
454
|
|
|
return $this->info; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* @param $dest |
459
|
|
|
*/ |
460
|
|
|
public function setCompareDest($dest) |
461
|
|
|
{ |
462
|
|
|
$this->compareDest = $dest; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @return string |
467
|
|
|
*/ |
468
|
|
|
public function getCompareDest() |
469
|
|
|
{ |
470
|
|
|
return $this->compareDest; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Gets command generated for this current |
475
|
|
|
* rsync configuration. You can use it to test |
476
|
|
|
* or execute it later without using the sync method |
477
|
|
|
* |
478
|
|
|
* @param $origin |
479
|
|
|
* @param $target |
480
|
|
|
* |
481
|
|
|
* @return Command |
482
|
|
|
*/ |
483
|
|
|
public function getCommand($origin, $target) |
484
|
|
|
{ |
485
|
|
|
$command = new Command($this->executable); |
486
|
|
|
|
487
|
|
|
if($this->skipNewerFiles) |
488
|
|
|
$command->addOption("u"); |
489
|
|
|
|
490
|
|
|
if($this->followSymLinks) |
491
|
|
|
$command->addOption("L"); |
492
|
|
|
|
493
|
|
|
if($this->dryRun) |
494
|
|
|
$command->addOption("n"); |
495
|
|
|
|
496
|
|
|
if($this->verbose) |
497
|
|
|
$command->addOption("v"); |
498
|
|
|
|
499
|
|
|
if($this->compression) |
500
|
|
|
$command->addOption("z"); |
501
|
|
|
|
502
|
|
|
// add any optional options we've specified |
503
|
|
|
$extra_options = $this->getOptionalParameters(); |
504
|
|
|
if(!empty($extra_options)) |
505
|
|
|
{ |
506
|
|
|
// if the extra options were given as a flat string, then convert it to an array |
507
|
|
|
if (is_string($extra_options)) |
508
|
|
|
$extra_options = str_split($extra_options); |
509
|
|
|
|
510
|
|
|
// add each extra option we've defined. |
511
|
|
|
if (is_array($extra_options)) |
512
|
|
|
{ |
513
|
|
|
foreach($extra_options as $option) |
514
|
|
|
{ |
515
|
|
|
$command->addOption($option); |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
if($this->times) |
521
|
|
|
$command->addArgument('times'); |
522
|
|
|
|
523
|
|
|
if($this->deleteFromTarget) |
524
|
|
|
$command->addArgument('delete'); |
525
|
|
|
|
526
|
|
|
if($this->removeSource) |
527
|
|
|
$command->addArgument('remove-source-files'); |
528
|
|
|
|
529
|
|
|
if($this->deleteExcluded) |
530
|
|
|
$command->addArgument('delete-excluded'); |
531
|
|
|
|
532
|
|
|
if($this->info) |
533
|
|
|
$command->addArgument('info', $this->info); |
534
|
|
|
|
535
|
|
|
if ($this->compareDest) |
536
|
|
|
$command->addArgument('compare-dest', $this->compareDest); |
537
|
|
|
|
538
|
|
|
if(!empty($this->exclude)) |
539
|
|
|
{ |
540
|
|
|
foreach($this->exclude as $excluded) |
541
|
|
|
{ |
542
|
|
|
$command->addArgument('exclude', $excluded); |
543
|
|
|
} |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
if(!empty($this->excludeFrom)) |
547
|
|
|
{ |
548
|
|
|
$command->addArgument('exclude-from', $this->excludeFrom); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
if($this->archive) |
552
|
|
|
$command->addOption("a"); |
553
|
|
|
|
554
|
|
|
if(!$this->archive && $this->recursive) |
555
|
|
|
$command->addOption("r"); |
556
|
|
|
|
557
|
|
|
if(!is_null($this->ssh)) |
558
|
|
|
{ |
559
|
|
|
$ssh = $this->ssh->getConnectionOptions(); |
560
|
|
|
$command->addArgument("rsh", $ssh); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
if(is_null($this->ssh)) |
564
|
|
|
{ |
565
|
|
|
$command->addParameter($origin); |
566
|
|
|
$command->addParameter($target); |
567
|
|
|
} |
568
|
|
|
elseif($this->remoteOrigin) |
569
|
|
|
{ |
570
|
|
|
$command->addParameter($this->ssh->getHostConnection() . ":" .$origin); |
571
|
|
|
$command->addParameter($target); |
572
|
|
|
} |
573
|
|
|
else |
574
|
|
|
{ |
575
|
|
|
$command->addParameter($origin); |
576
|
|
|
$command->addParameter($this->ssh->getHostConnection() . ":" .$target); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
return $command; |
580
|
|
|
} |
581
|
|
|
} |
582
|
|
|
|