-
Notifications
You must be signed in to change notification settings - Fork 5
/
xts3get.php
166 lines (138 loc) · 4.4 KB
/
xts3get.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* gets files from s3 server
* usage example: php -f xts3get.php access="******" secret="**********" bucket="xtartem" basedir="/home/mamay/Documents/test/xs3get" remoteBasedir="files"
*
* @package Xtbackup
*/
// make sure we will find include files
set_include_path(realpath(dirname(__FILE__)));
// don't search for ~.aws/sdk/config.inc.php which causes open_basedir problem
define('AWS_DISABLE_CONFIG_AUTO_DISCOVERY', true);
require "lib/AWSSDKforPHP/sdk.class.php";
/**
* gets files from s3 server
*
* @package Xtbackup
*/
class Xts3get
{
protected $_options = array();
protected $_s3;
protected $_updatedCount = 0;
public function __construct($argv)
{
define('AWS_CERTIFICATE_AUTHORITY', true);
$this->_prepareParams($argv);
$this->_s3 = new AmazonS3($this->_options['access'], $this->_options['secret']);
$this->_out(">>> Params initialised. Amazon s3 object created");
}
protected function _prepareParams($cliParams)
{
foreach ($cliParams as $param) {
if (strpos($param, "=")) {
$splited = explode("=", $param);
$this->_options[trim($splited[0])] = trim($splited[1]);
}
}
}
protected function _out($v)
{
echo $v.PHP_EOL;
}
/**
*
* @param CFSimpleXML $o object gotten from list response
*/
protected function _accept($o)
{
$filename = $localFilename = $this->_getLocalFilename($o);
if (!file_exists($filename)) {
return true;
} else {
if (md5_file($filename) != trim((string)$o->ETag, '"')) {
return true;
}
}
return false;
}
/**
*
* @param CFSimpleXML $o object gotten from list response
*
* @return String
*/
protected function _getLocalFilename(&$o)
{
$basedir = $this->_options['basedir'];
return $basedir.substr((string)$o->Key, strlen(trim($this->_options['remoteBasedir'],"/")), strlen((string)$o->Key) - 1 );
}
/**
*
* @param CFSimpleXML $o object gotten from list response
*/
protected function _update($o)
{
// prepare data for loop
$bucket = $this->_options['bucket'];
$localFilename = $this->_getLocalFilename($o);
$response = $this->_s3->get_object(
$bucket,
(string)$o->Key,
array(
'fileDownload' => $localFilename
));
if ($response->isOK()) {
$this->_out(">>>Updated: $localFilename");
$this->_updatedCount++;
}
}
public function updateFromRemote()
{
// prepare data for loop
$bucket = $this->_options['bucket'];
$remoteBasedir = $this->_options['remoteBasedir'];
$marker = '';
$itemCount = 0;
$v = false;
$firstBatch = true;
$this->_out(">>> Starting update");
do {
//create list of objects on s3 server
$list = $this->_s3->list_objects(
$bucket,
array(
'marker' => $marker,
'prefix' => $remoteBasedir,
)
);
if (!is_object($list->body->Contents)) {
$this->_out("S3 response problem, no content returned");
exit;
}
$count = $list->body->Contents->count();
if ($count === 0) {
if ($firstBatch) {
break;
} else {
$this->_out("S3 response problem, not all files returned");
exit;
}
}
$itemCount += $count;
$metaId = 0;
foreach ($list->body->Contents as $v) {
if ($this->_accept($v)) {
$this->_update($v);
}
}
// move to next batch of files
$marker = $v->Key;
$firstBatch = false;
} while((string) $list->body->IsTruncated == 'true');
$this->_out("All listed files: $itemCount");
$this->_out("All updated files: {$this->_updatedCount}");
}
}
$xts3 = new Xts3get($argv);
$xts3->updateFromRemote();