1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Result; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Util; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Amazon S3 Sync base class |
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.0.0 |
18
|
|
|
*/ |
19
|
|
|
abstract class AmazonS3 implements Simulator |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* AWS key |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $key; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* AWS secret |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $secret; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* AWS S3 bucket |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $bucket; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* TTL for all items in this bucket. |
44
|
|
|
* |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
protected $bucketTTL; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* AWS S3 region |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $region; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* AWS remote path / object key |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $path; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* AWS remote raw path / object key |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $pathRaw; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Unix timestamp of generating path from placeholder. |
72
|
|
|
* |
73
|
|
|
* @var int |
74
|
|
|
*/ |
75
|
|
|
protected $time; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* AWS acl |
79
|
|
|
* 'private' by default |
80
|
|
|
* |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
protected $acl; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Use multi part config |
87
|
|
|
* |
88
|
|
|
* @var boolean |
89
|
|
|
*/ |
90
|
|
|
protected $multiPartUpload; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Min multi part upload size |
94
|
|
|
* |
95
|
|
|
* @var int |
96
|
|
|
*/ |
97
|
|
|
protected $minMultiPartUploadSize = 5242880; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Max stream upload size |
101
|
|
|
* |
102
|
|
|
* @var int |
103
|
|
|
*/ |
104
|
|
|
protected $maxStreamUploadSize = 104857600; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Configure the sync. |
108
|
|
|
* |
109
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
110
|
|
|
* @param array $config |
111
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
112
|
|
|
*/ |
113
|
9 |
|
public function setup(array $config) |
114
|
|
|
{ |
115
|
9 |
|
if (!class_exists('\\Aws\\S3\\S3Client')) { |
116
|
|
|
throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// check for mandatory options |
120
|
9 |
|
$this->validateConfig($config); |
121
|
|
|
|
122
|
4 |
|
$this->time = time(); |
123
|
4 |
|
$this->key = $config['key']; |
124
|
4 |
|
$this->secret = $config['secret']; |
125
|
4 |
|
$this->bucket = $config['bucket']; |
126
|
4 |
|
$this->bucketTTL = Util\Arr::getValue($config, 'bucketTTL'); |
127
|
4 |
|
$this->region = $config['region']; |
128
|
4 |
|
$this->path = Util\Path::withTrailingSlash(Util\Path::replaceDatePlaceholders($config['path'], $this->time)); |
129
|
4 |
|
$this->pathRaw = $config['path']; |
130
|
4 |
|
$this->acl = Util\Arr::getValue($config, 'acl', 'private'); |
131
|
4 |
|
$this->multiPartUpload = Util\Str::toBoolean(Util\Arr::getValue($config, 'useMultiPartUpload'), false); |
132
|
4 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Make sure all mandatory keys are present in given config. |
136
|
|
|
* |
137
|
|
|
* @param array $config |
138
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
139
|
|
|
*/ |
140
|
9 |
|
protected function validateConfig(array $config) |
141
|
|
|
{ |
142
|
9 |
View Code Duplication |
foreach (['key', 'secret', 'bucket', 'region', 'path'] as $option) { |
143
|
9 |
|
if (!Util\Arr::isSetAndNotEmptyString($config, $option)) { |
144
|
9 |
|
throw new Exception('AWS S3 ' . $option . ' is mandatory'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
4 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Execute the sync |
151
|
|
|
* |
152
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
153
|
|
|
* @param \phpbu\App\Backup\Target $target |
154
|
|
|
* @param \phpbu\App\Result $result |
155
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
156
|
|
|
*/ |
157
|
|
|
abstract public function sync(Target $target, Result $result); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Simulate the sync execution. |
161
|
|
|
* |
162
|
|
|
* @param \phpbu\App\Backup\Target $target |
163
|
|
|
* @param \phpbu\App\Result $result |
164
|
|
|
*/ |
165
|
1 |
|
public function simulate(Target $target, Result $result) |
166
|
|
|
{ |
167
|
1 |
|
$result->debug( |
168
|
1 |
|
'sync backup to Amazon S3' . PHP_EOL |
169
|
1 |
|
. ' region: ' . $this->region . PHP_EOL |
170
|
1 |
|
. ' key: ' . $this->key . PHP_EOL |
171
|
1 |
|
. ' secret: ********' . PHP_EOL |
172
|
1 |
|
. ' location: ' . $this->bucket |
173
|
|
|
); |
174
|
1 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Should multi part upload be used. |
178
|
|
|
* |
179
|
|
|
* @param \phpbu\App\Backup\Target $target |
180
|
|
|
* @return bool |
181
|
|
|
* @throws \phpbu\App\Exception |
182
|
|
|
*/ |
183
|
|
|
protected function useMultiPartUpload(Target $target) |
184
|
|
|
{ |
185
|
|
|
// files bigger 5GB have to be uploaded via multi part |
186
|
|
|
// files uploaded with multi part upload has to be at least 5MB |
187
|
|
|
return ( |
188
|
|
|
$target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload |
189
|
|
|
) && $target->getSize() > $this->minMultiPartUploadSize; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|