-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathawsRecover.php
121 lines (118 loc) · 3.85 KB
/
awsRecover.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
require 'aws.phar'; // http://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.phar -> php > 5.6
//Start Settings
$refTimestamp = 1452816000; // Set recover version date - http://www.unixtimestamp.com/index.php
$bucket = 'bucketname';
$prefixpath = 'foder/subfolder';
$AWSKey = "xxxxx"; // Create Specific S3 bucket read only user
$AWSSecret = "xxxxx";
//End Settings Settings
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'eu-central-1',
'credentials' => [
'key' => $AWSKey,
'secret' => $AWSSecret,
],
]);
/* // Little Snippet for testing purpouse
$result = $s3->listBuckets();
foreach ($result['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";
}
*/
$results = $s3->getPaginator('ListObjectVersions', ['Bucket' => $bucket, 'Prefix' => $prefixpath]);
$count = 0;
$objs3 = array();
$folder = array();
$deleteMarker = array();
foreach ($results as $result) {
echo ' Block of 1000 - ';
echo ' Versions: '.count($result['Versions']).' ';
foreach ($result['Versions'] as $object) {
$timestamp = strtotime($object['LastModified']->__toString());
if ($object['Size'] == 0) {
$folder[$object['Key']] = array('VersionId' => $object['VersionId'], 'timestamp' => $timestamp);
} else {
if ($timestamp <= $refTimestamp) {
if (array_key_exists($object['Key'], $objs3)) {
$timeStampGiaPresente = $objs3[$object['Key']]['timestamp'];
} else {
$timeStampGiaPresente = 1;
}
if ($timestamp > $timeStampGiaPresente) {
$objs3[$object['Key']] = array('VersionId' => $object['VersionId'], 'timestamp' => $timestamp);
echo '.';
}
}
}
}
echo ' Delete Marker: '.count($result['DeleteMarkers']).' ';
if (count($result['DeleteMarkers']) > 0) {
foreach ($result['DeleteMarkers'] as $object) {
$timestamp = strtotime($object['LastModified']->__toString());
if ($timestamp <= $refTimestamp) {
if (array_key_exists($object['Key'], $deleteMarker)) {
$timeStampGiaPresente = $deleteMarker[$object['Key']]['timestamp'];
} else {
$timeStampGiaPresente = 1;
}
if ($timestamp > $timeStampGiaPresente) {
$deleteMarker[$object['Key']] = array('VersionId' => $object['VersionId'], 'timestamp' => $timestamp);
echo '.';
}
}
}
}
++$count;
}
echo "Total Pages: $count\n";
$toDownload = $objs3;
foreach ($deleteMarker as $key => $value) {
if ($value['timestamp'] <= $refTimestamp) {
if (array_key_exists($key, $toDownload)) {
if ($value['timestamp'] > $toDownload[$key]['timestamp']) {
unset($toDownload[$key]);
}
}
}
}
echo 'Download '.count($toDownload)." files...\n";
$cnt = 0;
foreach ($toDownload as $obk => $obv) {
createFile($obk, $obv['VersionId'], $s3, $bucket);
++$cnt;
echo '.';
if ($cnt % 100 == 0) {
echo " $cnt ";
}
}
echo 'End Procedure.';
function createFile($ffaws, $versionId, $s3, $bucket)
{
$prefix = 'dwn';
$ff = $prefix.'/'.$ffaws;
$diname = dirname($ff);
if ($diname == '.') { //Nothing to Do
} else {
make_path($ff);
}
$handle = fopen($ff, 'w+');
fclose($handle);
$result = $s3->getObject(array('Bucket' => $bucket, 'Key' => $ffaws, 'VersionId' => $versionId, 'SaveAs' => $ff));
}
function make_path($path)
{
$dir = pathinfo($path, PATHINFO_DIRNAME);
if (is_dir($dir)) {
return true;
} else {
if (make_path($dir)) {
if (mkdir($dir)) {
chmod($dir, 0777);
return true;
}
}
}
return false;
}