-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackup.php
265 lines (222 loc) · 8.69 KB
/
backup.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env php
<?php
/**
* Backup Exchange mailboxes hosted at OVH
*
* @version 0.4 - 2015-04-07
* @author Olivier Doucet (github odoucet)
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @link https://www.github.com/odoucet/backupexchangeovh
*/
// We need an up to date cert.pem (to access htps://api.ovh.com)
ini_set('curl.cainfo', '/etc/ssl/cert.pem');
// how many seconds to wait on each loop when OVH API is working
define('SLEEPTIME', 10);
// Script verbosity : 0 for minimal output, 5 for medium, 10 for very verbose
define('VERBOSITY', 5);
require 'OvhApi.php';
// Check parameters.ini
$credentials = parse_ini_file('parameters.ini');
if (strlen($credentials['application_key']) < 16) {
exit('Please fill application_key in parameters.ini');
}
if (strlen($credentials['application_secret']) < 32) {
exit('Please fill application_secret in parameters.ini');
}
if (strlen($credentials['consumer_key']) < 32) {
exit('Please fill consumer_key in parameters.ini');
}
if (!preg_match('@^[0-9]{1,}$@', $credentials['max_age_backup'])) {
exit('max_age_backup should be filled (unit is "hours")');
}
if (!isset($argv[2])) {
echo "Syntax: ".$argv[0]." <backup_folder> <account: <org>/service/<service>/account/<email>>\n";
echo "Example: ".$argv[0].' /backup/email "private-aa-1/service/private-aa-1/account/[email protected]"'."\n";
die(1);
}
// @todo check syntax argv2 + backupdir
define('BACKUPFILE', $argv[1].'/'.basename($argv[2]).'.pst');
define('ACCOUNTSTR', $argv[2]);
//---------------------------------------------------------------------------------
if (file_exists(BACKUPFILE)) {
if (filemtime(BACKUPFILE) < time()-3600*$credentials['max_age_backup']) {
unlink(BACKUPFILE);
} else {
echo "Account was already backuped less than ".$credentials['max_age_backup']." hours ago\n";
die(0);
}
} elseif (!is_dir(dirname(BACKUPFILE))) {
mkdir(dirname(BACKUPFILE));
}
//---------------------------------------------------------------------------------
// First, check account exists
$ovhApi = new OvhApi(OVH_API_EU, $credentials['application_key'], $credentials['application_secret'], $credentials['consumer_key']);
$result = $ovhApi->get('/email/exchange/'.ACCOUNTSTR.'/export');
if (VERBOSITY >= 5) {
echo 'Checking '.ACCOUNTSTR.' ...';
} elseif (VERBOSITY > 0) {
echo "Backuping ".ACCOUNTSTR." ...\n";
}
if ($result === null || (is_object($result) && isset($result->message) && preg_match('@does not exist@', $result->message))) {
if (VERBOSITY >= 5) {
printf(" (no previous backup done)\n");
}
$newExportNeeded = true;
} elseif (!is_object($result) || !isset($result->creationDate)) {
logError("an error occured when grabing export data.".print_r($result, 1));
} else {
if ($result->percentComplete == 100) {
$status = 'finished';
} else {
$status = $result->percentComplete.' %';
}
$dateBackup = new Datetime($result->creationDate);
$hoursAgo = floor((time() - $dateBackup->getTimestamp())/3600);
if (VERBOSITY >= 5) {
printf(
" Last backup (%10s) @ %20s -%4s hours ago\n",
$status,
$dateBackup->format('d-m-y H:i:s'),
$hoursAgo
);
}
// need to reset to make a new export
// Reset everytime, because if percentComplete != 100, after several hours, it means there is a bug so reset it.
if ($hoursAgo > $credentials['max_age_backup']) {
if (VERBOSITY >= 5) {
printf(" Reset export status (and wait 60 seconds) ...");
}
$result = $ovhApi->delete(
'/email/exchange/'.ACCOUNTSTR.'/export'
);
if (isset($result->errorCode) && $result->errorCode == 'NOT_GRANTED_CALL') {
printf(
"Cannot reset export status for account %s because it seems ".
"grant DELETE is missing. Please read again readme file.\n"
);
die(1);
}
for ($i = 0; $i<60; $i++) {
sleep(1);
}
$newExportNeeded = true;
} else {
// no need to reset
$newExportNeeded = false;
}
}
if ($newExportNeeded) {
$result = $ovhApi->post('/email/exchange/'.ACCOUNTSTR.'/export', null);
// debug
if (VERBOSITY >= 5) {
if (isset($result->status)) {
printf(" New export requested. Status: %s\n", $result->status);
} else {
logError(sprintf(" Error when requesting new export. Message: %s\n", $result->message));
die(1);
}
}
}
// infinite loop
$backupStatus = null;
while (sleep(SLEEPTIME) === 0) {
$result = @$ovhApi->get('/email/exchange/'.ACCOUNTSTR.'/export');
if ($result === null) {
// error retrieving data from API
//printf("Error retrieving info from API for %s/%s, will try again later\n", $service['service'], $email);
continue;
}
if (isset($result->percentComplete) && $result->percentComplete < 100) {
if (VERBOSITY >= 5) {
printf("%-20s backup done at %3s %%\r", basename(ACCOUNTSTR), $result->percentComplete);
}
} elseif (isset($result->percentComplete) && $result->percentComplete == 100) {
if (!isset($backupStatus)) {
// Yes ! Generate URL
if (VERBOSITY > 0) {
printf("\n%-20s is OK, generating URL ...\n", basename(ACCOUNTSTR));
}
$result = $ovhApi->post(
'/email/exchange/'.ACCOUNTSTR.'/exportURL',
null
);
$backupStatus = 'pendingUrl';
} elseif ($backupStatus == 'pendingUrl') {
// waiting for URL
$result = @$ovhApi->get(
'/email/exchange/'.ACCOUNTSTR.'/exportURL'
);
if (isset($result->url)) {
// Yes, backup !
if (file_exists(BACKUPFILE)) {
printf("URL %s already downloaded (?!) \n", $result->url);
die(0);
}
if (VERBOSITY >= 5) {
printf("Downloading %s to %s (with %s)\n", $result->url, BACKUPFILE, $credentials['backup_method']);
}
$backupStatus = 'downloadUrl';
$downloadUrl = $result->url;
} else {
if (VERBOSITY >= 5) {
printf("Export not ready yet (no url given).");
}
// sometimes, OVH forgets what we ask for ...
$result = @$ovhApi->post('/email/exchange/'.ACCOUNTSTR.'/exportURL', null);
}
} elseif ($backupStatus == 'downloadUrl') {
if ($credentials['backup_method'] == 'wget') {
if (file_exists(BACKUPFILE)) {
unlink(BACKUPFILE); // clean it before download
}
passthru('wget -nc -O "'.BACKUPFILE.'" -q "'.$downloadUrl.'"');
} elseif ($credentials['backup_method'] == 'fopen') {
$fp = fopen($result->url, 'r');
$out= fopen(BACKUPFILE, 'w');
while (!feof($fp)) {
fwrite($out, fread($fp, 65536));
}
fclose($out);
fclose($fp);
}
// check download OK
if (!file_exists(BACKUPFILE) || filesize(BACKUPFILE) == 0) {
continue; // try again
}
if (VERBOSITY > 0) {
echo "Backup Done! ";
}
die(0);
} else {
logError("Case not handled for backupStatus=".$backupStatus." line ".__LINE__.': '.print_r($result, 1));
}
// @todo do not base behaviour on message ...
} elseif (isset($result->message) && $result->message == 'The requested object (export) does not exist') {
// Export has not been done. This is a problem ... Ask for new export
$result = $ovhApi->post(
'/email/exchange/'.$service['org'].'/service/'.
$service['service'].'/account/'.$email.'/export',
null
);
// debug
if (VERBOSITY > 0) {
if (isset( $result->status)) {
printf(" New export requested. Status: %s\n", $result->status);
} else {
printf(" Error when requesting new export. Message: %s\n", $result->message);
}
}
} elseif (isset($result->message) && $result->message == 'Internal server error') {
// OVH had an issue ... Do nothing, try again
} else {
logError(ACCOUNTSTR.": Case not handled line ".__LINE__.': '.print_r($result, 1));
}
}
function logError($str)
{
if (VERBOSITY > 0) {
echo $str."\n";
}
file_put_contents(dirname(BACKUPFILE).'/errors.log', ACCOUNTSTR.': '.$str."\n", FILE_APPEND);
die(1);
}