1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Cli; |
5
|
|
|
use phpbu\App\Backup\Rsync as RsyncTrait; |
6
|
|
|
use phpbu\App\Backup\Sync; |
7
|
|
|
use phpbu\App\Backup\Target; |
8
|
|
|
use phpbu\App\Cli\Executable; |
9
|
|
|
use phpbu\App\Result; |
10
|
|
|
use phpbu\App\Util; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Rsync |
14
|
|
|
* |
15
|
|
|
* @package phpbu |
16
|
|
|
* @subpackage Backup |
17
|
|
|
* @author Sebastian Feldmann <[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 1.1.0 |
22
|
|
|
*/ |
23
|
|
|
class Rsync extends Cli implements Simulator |
24
|
|
|
{ |
25
|
|
|
use RsyncTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Setup the rsync sync. |
29
|
|
|
* |
30
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
31
|
|
|
* @param array $options |
32
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
33
|
|
|
*/ |
34
|
|
|
public function setup(array $options) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$this->setupRsync($options); |
38
|
|
|
} catch (\Exception $e) { |
39
|
|
|
throw new Exception($e->getMessage()); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the sync. |
45
|
|
|
* |
46
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
47
|
|
|
* @param \phpbu\App\Backup\Target $target |
48
|
|
|
* @param \phpbu\App\Result $result |
49
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
50
|
|
|
*/ |
51
|
|
|
public function sync(Target $target, Result $result) |
52
|
|
|
{ |
53
|
|
|
if ($this->args) { |
54
|
|
|
// pro mode define all arguments yourself |
55
|
|
|
// WARNING! no escaping is done by phpbu |
56
|
|
|
$result->debug('WARNING: phpbu uses your rsync args without escaping'); |
57
|
|
|
} |
58
|
|
|
$rsync = $this->execute($target); |
59
|
|
|
|
60
|
|
|
$result->debug($rsync->getCmd()); |
61
|
|
|
|
62
|
|
|
if (!$rsync->wasSuccessful()) { |
63
|
|
|
throw new Exception('rsync failed: ' . $rsync->getStdErr()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Simulate the sync execution. |
69
|
|
|
* |
70
|
|
|
* @param \phpbu\App\Backup\Target $target |
71
|
|
|
* @param \phpbu\App\Result $result |
72
|
|
|
*/ |
73
|
|
|
public function simulate(Target $target, Result $result) |
74
|
|
|
{ |
75
|
|
|
$result->debug( |
76
|
|
|
'sync backup with rsync' . PHP_EOL |
77
|
|
|
. $this->getExecutable($target)->getCommandLine() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Configure the Executable to run the 'rsync' command. |
83
|
|
|
* |
84
|
|
|
* @param \phpbu\App\Cli\Executable\Rsync $exec |
85
|
|
|
* @param \phpbu\App\Backup\Target $target |
86
|
|
|
*/ |
87
|
9 |
View Code Duplication |
protected function configureExecutable(Executable\Rsync $exec, Target $target) |
|
|
|
|
88
|
|
|
{ |
89
|
9 |
|
$exec->fromPath($this->getRsyncLocation($target)) |
90
|
|
|
->toHost($this->host) |
91
|
9 |
|
->toPath($this->path) |
92
|
4 |
|
->toUser($this->user) |
93
|
4 |
|
->compressed(!$target->shouldBeCompressed()) |
94
|
5 |
|
->removeDeleted($this->delete) |
95
|
1 |
|
->exclude($this->excludes); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.