|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Executable; |
|
5
|
|
|
use phpbu\App\Exception; |
|
6
|
|
|
use phpbu\App\Util; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Rsync trait. |
|
10
|
|
|
* |
|
11
|
|
|
* @package phpbu |
|
12
|
|
|
* @subpackage Backup |
|
13
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
14
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
15
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
16
|
|
|
* @link http://phpbu.de/ |
|
17
|
|
|
* @since Class available since Release 3.1.4 |
|
18
|
|
|
*/ |
|
19
|
|
|
trait Rsync |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Tar Executable |
|
23
|
|
|
* |
|
24
|
|
|
* @var \phpbu\App\Cli\Executable\Tar |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $executable; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Path to executable. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $pathToRsync; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Raw args |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $args; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Remote username |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $user; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Target host |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $host; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Target path |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $path; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Files to ignore, extracted from config string separated by ":" |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $excludes; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Should only the created backup be synced or the complete directory |
|
72
|
|
|
* |
|
73
|
|
|
* @var boolean |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $isDirSync; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Remove deleted files. |
|
79
|
|
|
* |
|
80
|
|
|
* @var bool |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $delete; |
|
83
|
|
|
|
|
84
|
|
|
protected function setupRsync(array $conf) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->pathToRsync = Util\Arr::getValue($conf, 'pathToRsync'); |
|
87
|
|
|
|
|
88
|
|
|
if (Util\Arr::isSetAndNotEmptyString($conf, 'args')) { |
|
89
|
|
|
$this->args = $conf['args']; |
|
90
|
|
|
} else { |
|
91
|
|
|
if (!Util\Arr::isSetAndNotEmptyString($conf, 'path')) { |
|
92
|
|
|
throw new Exception('option \'path\' is missing'); |
|
93
|
|
|
} |
|
94
|
|
|
$this->path = Util\Str::replaceDatePlaceholders($conf['path']); |
|
95
|
|
|
$this->user = Util\Arr::getValue($conf, 'user'); |
|
96
|
|
|
$this->host = Util\Arr::getValue($conf, 'host'); |
|
97
|
|
|
$this->excludes = Util\Str::toList(Util\Arr::getValue($conf, 'exclude', ''), ':'); |
|
98
|
|
|
$this->delete = Util\Str::toBoolean(Util\Arr::getValue($conf, 'delete', ''), false); |
|
99
|
|
|
$this->isDirSync = Util\Str::toBoolean(Util\Arr::getValue($conf, 'dirsync', ''), false); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Setup the Executable to run the 'rsync' command. |
|
105
|
|
|
* |
|
106
|
|
|
* @param \phpbu\App\Backup\Target |
|
107
|
|
|
* @return \phpbu\App\Cli\Executable |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getExecutable(Target $target) |
|
110
|
|
|
{ |
|
111
|
|
|
if (null == $this->executable) { |
|
112
|
|
|
$this->executable = new Executable\Rsync($this->pathToRsync); |
|
|
|
|
|
|
113
|
|
|
if (!empty($this->args)) { |
|
114
|
|
|
$this->executable->useArgs( |
|
115
|
|
|
Util\Str::replaceTargetPlaceholders( |
|
116
|
|
|
$this->args, |
|
117
|
|
|
$target->getPathnamePlain() |
|
118
|
|
|
) |
|
119
|
|
|
); |
|
120
|
|
|
} else { |
|
121
|
|
|
$this->configureExecutable($this->executable, $target); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
return $this->executable; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Configure the executable if no costim args are used. |
|
129
|
|
|
* |
|
130
|
|
|
* @param \phpbu\App\Cli\Executable\Rsync $exec |
|
131
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
132
|
|
|
*/ |
|
133
|
|
|
abstract protected function configureExecutable(Executable\Rsync $exec, Target $target); |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Return rsync location. |
|
137
|
|
|
* |
|
138
|
|
|
* @param \phpbu\App\Backup\Target |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function getRsyncLocation(Target $target) |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->isDirSync ? $target->getPath() : $target->getPathname(); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..