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\Arr; |
7
|
|
|
use phpbu\App\Util\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Amazon S3 Sync base class |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @subpackage Backup |
14
|
|
|
* @author Sebastian Feldmann <[email protected]> |
15
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
17
|
|
|
* @link http://phpbu.de/ |
18
|
|
|
* @since Class available since Release 3.0.0 |
19
|
|
|
*/ |
20
|
|
|
abstract class AmazonS3 implements Simulator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* AWS key |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $key; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AWS secret |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $secret; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* AWS S3 bucket |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $bucket; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* TTL for all items in this bucket. |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $bucketTTL; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* AWS S3 region |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $region; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* AWS remote path / object key |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $path; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* AWS acl |
66
|
|
|
* 'private' by default |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $acl; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Use multi part config |
74
|
6 |
|
* |
75
|
|
|
* @var boolean |
76
|
6 |
|
*/ |
77
|
|
|
protected $multiPartUpload; |
78
|
|
|
|
79
|
6 |
|
/** |
80
|
1 |
|
* Min multi part upload size |
81
|
|
|
* |
82
|
5 |
|
* @var int |
83
|
1 |
|
*/ |
84
|
|
|
protected $minMultiPartUploadSize = 5242880; |
85
|
4 |
|
|
86
|
1 |
|
/** |
87
|
|
|
* Max stream upload size |
88
|
3 |
|
* |
89
|
1 |
|
* @var int |
90
|
|
|
*/ |
91
|
2 |
|
protected $maxStreamUploadSize = 104857600; |
92
|
1 |
|
|
93
|
|
|
/** |
94
|
1 |
|
* Configure the sync. |
95
|
1 |
|
* |
96
|
1 |
|
* @see \phpbu\App\Backup\Sync::setup() |
97
|
1 |
|
* @param array $config |
98
|
1 |
|
* @throws \phpbu\App\Backup\Sync\Exception |
99
|
1 |
|
*/ |
100
|
1 |
|
public function setup(array $config) |
101
|
|
|
{ |
102
|
|
|
if (!class_exists('\\Aws\\S3\\S3Client')) { |
103
|
|
|
throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// check for mandatory options |
107
|
|
View Code Duplication |
foreach (['key', 'secret', 'bucket', 'region', 'path'] as $option) { |
108
|
|
|
if (!Arr::isSetAndNotEmptyString($config, $option)) { |
109
|
|
|
throw new Exception('AWS S3 ' . $option . ' is mandatory'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->key = $config['key']; |
114
|
|
|
$this->secret = $config['secret']; |
115
|
|
|
$this->bucket = $config['bucket']; |
116
|
|
|
$this->bucketTTL = Arr::getValue($config, 'bucketTTL'); |
117
|
|
|
$this->region = $config['region']; |
118
|
|
|
$this->path = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])); |
119
|
|
|
$this->acl = Arr::getValue($config, 'acl', 'private'); |
120
|
|
|
$this->multiPartUpload = Str::toBoolean(Arr::getValue($config, 'useMultiPartUpload'), false); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Execute the sync |
125
|
|
|
* |
126
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
127
|
|
|
* @param \phpbu\App\Backup\Target $target |
128
|
|
|
* @param \phpbu\App\Result $result |
129
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
130
|
|
|
*/ |
131
|
|
|
abstract public function sync(Target $target, Result $result); |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Simulate the sync execution. |
135
|
|
|
* |
136
|
|
|
* @param \phpbu\App\Backup\Target $target |
137
|
|
|
* @param \phpbu\App\Result $result |
138
|
|
|
*/ |
139
|
|
|
public function simulate(Target $target, Result $result) |
140
|
|
|
{ |
141
|
|
|
$result->debug( |
142
|
|
|
'sync backup to Amazon S3' . PHP_EOL |
143
|
|
|
. ' region: ' . $this->region . PHP_EOL |
144
|
|
|
. ' key: ' . $this->key . PHP_EOL |
145
|
|
|
. ' secret: ********' . PHP_EOL |
146
|
|
|
. ' location: ' . $this->bucket |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Should multi part upload be used. |
152
|
|
|
* |
153
|
|
|
* @param \phpbu\App\Backup\Target $target |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
protected function useMultiPartUpload(Target $target) |
157
|
|
|
{ |
158
|
|
|
// files bigger 5GB have to be uploaded via multi part |
159
|
|
|
// files uploaded with multi part upload has to be at least 5MB |
160
|
|
|
return ( |
161
|
|
|
$target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload |
162
|
|
|
) && $target->getSize() > $this->minMultiPartUploadSize; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|