1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Sync as SyncInterface; |
5
|
|
|
use phpbu\App\Result; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Util; |
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 extends SyncInterface |
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 remote raw path / object key |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $pathRaw; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* AWS acl |
73
|
|
|
* 'private' by default |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $acl; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Use multi part config |
81
|
|
|
* |
82
|
|
|
* @var boolean |
83
|
|
|
*/ |
84
|
|
|
protected $multiPartUpload; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Min multi part upload size |
88
|
|
|
* |
89
|
|
|
* @var int |
90
|
|
|
*/ |
91
|
|
|
protected $minMultiPartUploadSize = 5242880; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Max stream upload size |
95
|
|
|
* |
96
|
|
|
* @var int |
97
|
|
|
*/ |
98
|
|
|
protected $maxStreamUploadSize = 104857600; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Configure the sync. |
102
|
|
|
* |
103
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
104
|
|
|
* @param array $config |
105
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
106
|
|
|
*/ |
107
|
9 |
|
public function setup(array $config) |
108
|
|
|
{ |
109
|
9 |
|
if (!class_exists('\\Aws\\S3\\S3Client')) { |
110
|
|
|
throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// check for mandatory options |
114
|
9 |
|
$this->validateConfig($config, ['key', 'secret', 'bucket', 'region', 'path']); |
115
|
|
|
|
116
|
4 |
|
$this->time = time(); |
117
|
4 |
|
$this->key = $config['key']; |
118
|
4 |
|
$this->secret = $config['secret']; |
119
|
4 |
|
$this->bucket = $config['bucket']; |
120
|
4 |
|
$this->bucketTTL = Util\Arr::getValue($config, 'bucketTTL'); |
121
|
4 |
|
$this->region = $config['region']; |
122
|
4 |
|
$this->path = Util\Path::withTrailingSlash(Util\Path::replaceDatePlaceholders($config['path'], $this->time)); |
123
|
4 |
|
$this->pathRaw = $config['path']; |
124
|
4 |
|
$this->acl = Util\Arr::getValue($config, 'acl', 'private'); |
125
|
4 |
|
$this->multiPartUpload = Util\Str::toBoolean(Util\Arr::getValue($config, 'useMultiPartUpload'), false); |
126
|
4 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Simulate the sync execution. |
130
|
|
|
* |
131
|
|
|
* @param \phpbu\App\Backup\Target $target |
132
|
|
|
* @param \phpbu\App\Result $result |
133
|
|
|
*/ |
134
|
1 |
|
public function simulate(Target $target, Result $result) |
135
|
|
|
{ |
136
|
1 |
|
$result->debug( |
137
|
1 |
|
'sync backup to Amazon S3' . PHP_EOL |
138
|
1 |
|
. ' region: ' . $this->region . PHP_EOL |
139
|
1 |
|
. ' key: ' . $this->key . PHP_EOL |
140
|
1 |
|
. ' secret: ********' . PHP_EOL |
141
|
1 |
|
. ' location: ' . $this->bucket |
142
|
|
|
); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Should multi part upload be used. |
147
|
|
|
* |
148
|
|
|
* @param \phpbu\App\Backup\Target $target |
149
|
|
|
* @return bool |
150
|
|
|
* @throws \phpbu\App\Exception |
151
|
|
|
*/ |
152
|
|
|
protected function useMultiPartUpload(Target $target) |
153
|
|
|
{ |
154
|
|
|
// files bigger 5GB have to be uploaded via multi part |
155
|
|
|
// files uploaded with multi part upload has to be at least 5MB |
156
|
|
|
return ( |
157
|
|
|
$target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload |
158
|
|
|
) && $target->getSize() > $this->minMultiPartUploadSize; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|