|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup\Sync; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Cli; |
|
5
|
|
|
use phpbu\App\Backup\File\Remote; |
|
6
|
|
|
use phpbu\App\Backup\Rsync as RsyncTrait; |
|
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
|
13 |
|
public function setup(array $options) |
|
35
|
|
|
{ |
|
36
|
|
|
try { |
|
37
|
13 |
|
$this->setupRsync($options); |
|
38
|
1 |
|
} catch (\Exception $e) { |
|
39
|
1 |
|
throw new Exception($e->getMessage()); |
|
40
|
|
|
} |
|
41
|
12 |
|
} |
|
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
|
2 |
View Code Duplication |
public function sync(Target $target, Result $result) |
|
52
|
|
|
{ |
|
53
|
2 |
|
if ($this->args) { |
|
54
|
|
|
// pro mode define all arguments yourself |
|
55
|
|
|
// WARNING! no escaping is done by phpbu |
|
56
|
1 |
|
$result->debug('WARNING: phpbu uses your rsync args without escaping'); |
|
57
|
|
|
} |
|
58
|
2 |
|
$rsync = $this->execute($target); |
|
59
|
|
|
|
|
60
|
2 |
|
$result->debug($rsync->getCmd()); |
|
61
|
|
|
|
|
62
|
2 |
|
if (!$rsync->isSuccessful()) { |
|
63
|
1 |
|
throw new Exception('rsync failed: ' . $rsync->getStdErr()); |
|
64
|
|
|
} |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Simulate the sync execution. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
71
|
|
|
* @param \phpbu\App\Result $result |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function simulate(Target $target, Result $result) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$result->debug( |
|
76
|
1 |
|
'sync backup with rsync' . PHP_EOL |
|
77
|
1 |
|
. $this->getExecutable($target)->getCommandPrintable() |
|
78
|
|
|
); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Setup the Executable to run the 'rsync' command. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \phpbu\App\Backup\Target |
|
85
|
|
|
* @return \phpbu\App\Cli\Executable |
|
86
|
|
|
*/ |
|
87
|
9 |
|
protected function createExecutable(Target $target) : Executable |
|
88
|
|
|
{ |
|
89
|
9 |
|
$executable = new Executable\Rsync($this->pathToRsync); |
|
90
|
9 |
|
if (!empty($this->args)) { |
|
91
|
2 |
|
$executable->useArgs( |
|
92
|
2 |
|
Util\Str::replaceTargetPlaceholders( |
|
93
|
2 |
|
$this->args, |
|
94
|
2 |
|
$target->getPathname() |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} else { |
|
98
|
7 |
|
$executable->fromPath($this->getRsyncLocation($target)) |
|
99
|
7 |
|
->usePassword($this->password) |
|
100
|
7 |
|
->usePasswordFile($this->passwordFile) |
|
101
|
7 |
|
->toHost($this->host) |
|
102
|
7 |
|
->toPath($this->path) |
|
103
|
7 |
|
->toUser($this->user) |
|
104
|
7 |
|
->compressed(!$target->shouldBeCompressed()) |
|
105
|
7 |
|
->removeDeleted($this->delete) |
|
106
|
7 |
|
->exclude($this->excludes); |
|
107
|
|
|
} |
|
108
|
9 |
|
return $executable; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Execute the remote clean up if needed |
|
113
|
|
|
* |
|
114
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
115
|
|
|
* @param \phpbu\App\Result $result |
|
116
|
|
|
*/ |
|
117
|
|
|
public function cleanup(Target $target, Result $result) |
|
118
|
|
|
{ |
|
119
|
|
|
// TODO: Implement cleanup() method. |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|