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
|
|
|
* Unix timestamp of generating path from placeholder. |
65
|
|
|
* |
66
|
|
|
* @var int |
67
|
|
|
*/ |
68
|
|
|
protected $time; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* AWS remote raw path / object key |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $pathRaw; |
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
|
11 |
|
public function setup(array $config) |
114
|
|
|
{ |
115
|
11 |
|
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
|
11 |
|
$this->validateConfig($config, ['key', 'secret', 'bucket', 'region', 'path']); |
121
|
|
|
|
122
|
6 |
|
$cleanedPath = Util\Path::withoutTrailingSlash(Util\Path::withoutLeadingSlash($config['path'])); |
123
|
6 |
|
$this->time = time(); |
124
|
6 |
|
$this->key = $config['key']; |
125
|
6 |
|
$this->secret = $config['secret']; |
126
|
6 |
|
$this->bucket = $config['bucket']; |
127
|
6 |
|
$this->bucketTTL = Util\Arr::getValue($config, 'bucketTTL'); |
128
|
6 |
|
$this->region = $config['region']; |
129
|
6 |
|
$this->path = Util\Path::replaceDatePlaceholders($cleanedPath, $this->time); |
130
|
6 |
|
$this->pathRaw = $cleanedPath; |
131
|
6 |
|
$this->acl = Util\Arr::getValue($config, 'acl', 'private'); |
132
|
6 |
|
$this->multiPartUpload = Util\Str::toBoolean(Util\Arr::getValue($config, 'useMultiPartUpload'), false); |
133
|
6 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Make sure all mandatory keys are present in given config. |
137
|
|
|
* |
138
|
|
|
* @param array $config |
139
|
|
|
* @param string[] $keys |
140
|
|
|
* @throws Exception |
141
|
|
|
*/ |
142
|
11 |
|
protected function validateConfig(array $config, array $keys) |
143
|
|
|
{ |
144
|
11 |
|
foreach ($keys as $option) { |
145
|
11 |
|
if (!Util\Arr::isSetAndNotEmptyString($config, $option)) { |
146
|
11 |
|
throw new Exception($option . ' is mandatory'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
6 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Simulate the sync execution. |
153
|
|
|
* |
154
|
|
|
* @param \phpbu\App\Backup\Target $target |
155
|
|
|
* @param \phpbu\App\Result $result |
156
|
|
|
*/ |
157
|
1 |
|
public function simulate(Target $target, Result $result) |
158
|
|
|
{ |
159
|
1 |
|
$result->debug( |
160
|
1 |
|
'sync backup to Amazon S3' . PHP_EOL |
161
|
1 |
|
. ' region: ' . $this->region . PHP_EOL |
162
|
1 |
|
. ' key: ' . $this->key . PHP_EOL |
163
|
1 |
|
. ' secret: ********' . PHP_EOL |
164
|
1 |
|
. ' location: ' . $this->bucket . PHP_EOL |
165
|
|
|
); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Should multi part upload be used. |
170
|
|
|
* |
171
|
|
|
* @param \phpbu\App\Backup\Target $target |
172
|
|
|
* @return bool |
173
|
|
|
* @throws \phpbu\App\Exception |
174
|
|
|
*/ |
175
|
2 |
|
protected function useMultiPartUpload(Target $target) |
176
|
|
|
{ |
177
|
|
|
// files bigger 5GB have to be uploaded via multi part |
178
|
|
|
// files uploaded with multi part upload has to be at least 5MB |
179
|
|
|
return ( |
180
|
2 |
|
($target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload) |
181
|
2 |
|
) && $target->getSize() > $this->minMultiPartUploadSize; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|