|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* keeps a record for every database backup made... |
|
5
|
|
|
* |
|
6
|
|
|
* |
|
7
|
|
|
* |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class DatabasebackupLog extends DataObject |
|
13
|
|
|
{ |
|
14
|
|
|
private static $singular_name = "Database Backup"; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
private static $plural_name = "Database Backups"; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
private static $db = array( |
|
|
|
|
|
|
19
|
|
|
"Title" => "Varchar(255)", |
|
20
|
|
|
"Notes" => "Text", |
|
21
|
|
|
"FullLocation" => "Varchar(255)", |
|
22
|
|
|
"SizeInBytes" => "Int", |
|
23
|
|
|
"DebugMessage" => "Text" |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
27
|
|
|
"FullLocation" => true |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
private static $default_sort = "Created DESC"; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
private static $casting = array( |
|
|
|
|
|
|
33
|
|
|
"SizeInMegabytes" => "Int" |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
37
|
|
|
"Title" => "Title", |
|
38
|
|
|
"Created" => "Created", |
|
39
|
|
|
"SizeInMegabytes" => "Size (in Mb.)", |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* location for backup file e.g. /var/backups/db.sql |
|
44
|
|
|
* @var String |
|
45
|
|
|
*/ |
|
46
|
|
|
private static $full_location_for_db_backup_file = ""; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* number of cycles before the database backups get deleted forgood... |
|
50
|
|
|
* @var Int |
|
51
|
|
|
*/ |
|
52
|
|
|
private static $max_db_copies = 3; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* at the moment only the gzip compression is supported! |
|
56
|
|
|
* @var String |
|
57
|
|
|
*/ |
|
58
|
|
|
private static $compression = ""; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* for security reasons we set this to false ... |
|
62
|
|
|
* @var Boolean |
|
63
|
|
|
*/ |
|
64
|
|
|
private static $allow_restores_in_live_environment = false; |
|
65
|
|
|
|
|
66
|
|
|
public function canCreate($member = null) |
|
67
|
|
|
{ |
|
68
|
|
|
return Permission::check("ADMIN"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function canDelete($member = null) |
|
72
|
|
|
{ |
|
73
|
|
|
return Permission::check("ADMIN"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function canEdit($member = null) |
|
77
|
|
|
{ |
|
78
|
|
|
return Permission::check("ADMIN"); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getCMSFields() |
|
82
|
|
|
{ |
|
83
|
|
|
$fields = parent::getCMSFields(); |
|
84
|
|
|
$fields->addFieldToTab("Root.Main", new ReadonlyField("Created")); |
|
85
|
|
|
$fields->addFieldToTab("Root.Main", new ReadonlyField("FullLocation")); |
|
86
|
|
|
$fields->addFieldToTab("Root.Main", new ReadonlyField("SizeInBytes")); |
|
87
|
|
|
$fields->addFieldToTab("Root.Main", new ReadonlyField("SizeInMegabytes")); |
|
88
|
|
|
$fields->removeFieldFromTab("Root.Main", "DebugMessage"); |
|
89
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("DownloadLink", "<h2><a href=\"/backupsystem/download/".$this->ID."/\">download now</a></h2>")); |
|
90
|
|
|
return $fields; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @casting |
|
95
|
|
|
* @return Int |
|
|
|
|
|
|
96
|
|
|
*/ |
|
97
|
|
|
public function getSizeInMegabytes() |
|
98
|
|
|
{ |
|
99
|
|
|
if($this->SizeInBytes) { |
|
|
|
|
|
|
100
|
|
|
return round($this->SizeInBytes / 1024 / 1024, 2); |
|
|
|
|
|
|
101
|
|
|
} else { |
|
102
|
|
|
return 'n/a'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Adds a button the Site Config page of the CMS to rebuild the Lucene search index. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getCMSActions() |
|
110
|
|
|
{ |
|
111
|
|
|
$actions = parent::getCMSActions(); |
|
112
|
|
|
if (Permission::check("ADMIN")) { |
|
113
|
|
|
if ($fileLocation = $this->getFullLocationWithExtension()) { |
|
114
|
|
|
clearstatcache(); |
|
115
|
|
|
if ($this->FullLocation && file_exists($this->FullLocation)) { |
|
|
|
|
|
|
116
|
|
|
//do nothing |
|
117
|
|
|
} else { |
|
118
|
|
|
$lastChanged = _t('Databasebackup.NO_BACKUP_IS_AVAILABLE', 'This Backup is NOT Available ... Once created, you can access it here: '.$fileLocation); |
|
119
|
|
|
} |
|
120
|
|
|
if (!$this->exists()) { |
|
121
|
|
|
$actions->push( |
|
122
|
|
|
new FormAction( |
|
123
|
|
|
'doMakeDatabaseBackup', |
|
124
|
|
|
_t('Databasebackup.MAKE_DATABASE_BACKUP', 'Make Database Backup')."; ".$lastChanged |
|
|
|
|
|
|
125
|
|
|
) |
|
126
|
|
|
); |
|
127
|
|
|
} else { |
|
128
|
|
|
if ($this->FullLocation && file_exists($this->FullLocation)) { |
|
|
|
|
|
|
129
|
|
|
if (!Director::IsLive() || $this->Config()->get("allow_restores_in_live_environment")) { |
|
130
|
|
|
$actions->push( |
|
131
|
|
|
new FormAction( |
|
132
|
|
|
'doRestoreDatabaseBackup', |
|
133
|
|
|
_t('Databasebackup.RESTORE_DB_BACKUP_NOW', 'Restore This Database (override current one)') |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
$this->extend('updateCMSActions', $actions); |
|
141
|
|
|
} |
|
142
|
|
|
return $actions; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* if backup does not exist then make it ... |
|
147
|
|
|
* set size |
|
148
|
|
|
*/ |
|
149
|
|
|
public function onBeforeWrite() |
|
150
|
|
|
{ |
|
151
|
|
|
parent::onBeforeWrite(); |
|
152
|
|
|
clearstatcache(); |
|
153
|
|
|
if (!$this->exists()) { |
|
154
|
|
|
if (!$this->FullLocation) { |
|
|
|
|
|
|
155
|
|
|
if ($fileLocation = $this->getFullLocationWithExtension()) { |
|
156
|
|
|
$fileLocation = $this->cycleDatabaseBackupFiles($fileLocation); |
|
157
|
|
|
global $databaseConfig; |
|
158
|
|
|
$compression = $this->Config()->get("compression"); |
|
159
|
|
View Code Duplication |
if ($compression == "gzip") { |
|
|
|
|
|
|
160
|
|
|
$command = "mysqldump -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]." ".$databaseConfig["database"]." | gzip > ".$fileLocation; |
|
161
|
|
|
} else { |
|
162
|
|
|
$command = "mysqldump -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]." ".$databaseConfig["database"]." > ".$fileLocation; |
|
163
|
|
|
} |
|
164
|
|
|
$this->DebugMessage = exec($command); |
|
|
|
|
|
|
165
|
|
|
$this->FullLocation = $fileLocation; |
|
|
|
|
|
|
166
|
|
|
clearstatcache(); |
|
167
|
|
|
$this->SizeInBytes = filesize($this->FullLocation); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
//just in case, we do this everytime... |
|
172
|
|
|
if (!$this->SizeInBytes && file_exists($this->FullLocation)) { |
|
|
|
|
|
|
173
|
|
|
$this->SizeInBytes = filesize($this->FullLocation); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
if (!$this->Title) { |
|
|
|
|
|
|
176
|
|
|
$this->Title = $this->FullLocation." (" .$this->getSizeInMegabytes()."mb.)"; |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* delete me if file does not exist |
|
182
|
|
|
*/ |
|
183
|
|
|
public function onAfterWrite() |
|
184
|
|
|
{ |
|
185
|
|
|
parent::onAfterWrite(); |
|
186
|
|
|
clearstatcache(); |
|
187
|
|
|
if (!file_exists($this->FullLocation)) { |
|
|
|
|
|
|
188
|
|
|
$this->delete(); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* delete file if I get deleted |
|
195
|
|
|
*/ |
|
196
|
|
|
public function onBeforeDelete() |
|
197
|
|
|
{ |
|
198
|
|
|
parent::onBeforeDelete(); |
|
199
|
|
|
clearstatcache(); |
|
200
|
|
|
if (file_exists($this->FullLocation)) { |
|
|
|
|
|
|
201
|
|
|
unlink($this->FullLocation); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* |
|
207
|
|
|
* @return Boolean |
|
208
|
|
|
*/ |
|
209
|
|
|
public function restoreDatabaseBackup() |
|
210
|
|
|
{ |
|
211
|
|
|
if (!Director::IsLive() || $this->Config()->get("allow_restores_in_live_environment")) { |
|
212
|
|
|
$fileLocation = $this->FullLocation; |
|
|
|
|
|
|
213
|
|
|
if (file_exists($fileLocation)) { |
|
214
|
|
|
$this->saveToSession(); |
|
215
|
|
|
global $databaseConfig; |
|
216
|
|
|
$compression = $this->Config()->get("compression"); |
|
217
|
|
View Code Duplication |
if ($compression == "gzip") { |
|
|
|
|
|
|
218
|
|
|
$command = "gunzip < ".$fileLocation. " | mysql -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]." ".$databaseConfig["database"]." "; |
|
219
|
|
|
} else { |
|
220
|
|
|
$command = "mysql -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]." ".$databaseConfig["database"]." < ".$fileLocation; |
|
221
|
|
|
} |
|
222
|
|
|
exec($command); |
|
223
|
|
|
//reset list of backups ... |
|
224
|
|
|
$this->requireDefaultRecords(); |
|
225
|
|
|
$this->retrieveFromSession(); |
|
226
|
|
|
Controller::curr()->redirect("/admin/databasebackuplog/"); |
|
227
|
|
|
return true; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
return false; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* we have this so that when we restore a database |
|
235
|
|
|
* we dont loose the backup information... |
|
236
|
|
|
* saves all the logs to session |
|
237
|
|
|
*/ |
|
238
|
|
|
protected function saveToSession() |
|
239
|
|
|
{ |
|
240
|
|
|
foreach (DatabasebackupLog::get() as $object) { |
|
241
|
|
|
$array[$object->ID] = array( |
|
|
|
|
|
|
242
|
|
|
"FullLocation" => $object->FullLocation, |
|
243
|
|
|
"Title" => $object->Title, |
|
244
|
|
|
"Notes" => $object->Notes, |
|
245
|
|
|
"Created" => $object->Created |
|
246
|
|
|
); |
|
247
|
|
|
Session::set("DatabasebackupLogs", serialize($array)); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* |
|
253
|
|
|
* retrieves and updates all the logs from session |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function retrieveFromSession() |
|
256
|
|
|
{ |
|
257
|
|
|
$array = unserialize(Session::get("DatabasebackupLogs")); |
|
258
|
|
|
foreach ($array as $id => $values) { |
|
259
|
|
|
$obj = DatabasebackupLog::get()->filter(array("FullLocation" => $values["FullLocation"]))->first(); |
|
260
|
|
|
if ($obj) { |
|
261
|
|
|
$obj->Title = convert::raw2sql($values["Title"]); |
|
262
|
|
|
$obj->Notes = convert::raw2sql($values["Notes"]); |
|
263
|
|
|
$obj->Created = convert::raw2sql($values["Created"]); |
|
264
|
|
|
$obj->write(); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* move all of the database copies up one, |
|
271
|
|
|
* deleting the upper one. |
|
272
|
|
|
* |
|
273
|
|
|
* Returns the name of the file name freed up ... (by moving all of them one up...) |
|
274
|
|
|
* |
|
275
|
|
|
* @param string $fileLocation |
|
276
|
|
|
* |
|
277
|
|
|
* @return string File Location |
|
278
|
|
|
* |
|
279
|
|
|
*/ |
|
280
|
|
|
protected function cycleDatabaseBackupFiles($fileLocation) |
|
281
|
|
|
{ |
|
282
|
|
|
$copyFileLocation = $fileLocation; |
|
|
|
|
|
|
283
|
|
|
$max = $this->Config()->get("max_db_copies"); |
|
284
|
|
|
for ($i = $max; $i > -1; $i--) { |
|
285
|
|
|
$lowerFileLocation = $this->olderBackupFileName($fileLocation, $i); |
|
286
|
|
|
if ($i == $max) { |
|
287
|
|
|
//delete the top one ... |
|
288
|
|
|
clearstatcache(); |
|
289
|
|
View Code Duplication |
if (file_exists($lowerFileLocation)) { |
|
|
|
|
|
|
290
|
|
|
clearstatcache(); |
|
291
|
|
|
$obj = DatabasebackupLog::get()->filter(array("FullLocation" => $lowerFileLocation))->First(); |
|
292
|
|
|
if ($obj) { |
|
293
|
|
|
$obj->delete(); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} else { |
|
297
|
|
|
$j = $i + 1; |
|
298
|
|
|
$higherFileLocation = $fileLocation.".".$j.".bak"; |
|
299
|
|
|
clearstatcache(); |
|
300
|
|
|
if (file_exists($lowerFileLocation)) { |
|
301
|
|
|
//double-check the top one ... |
|
302
|
|
View Code Duplication |
if (file_exists($higherFileLocation)) { |
|
|
|
|
|
|
303
|
|
|
clearstatcache(); |
|
304
|
|
|
$obj = DatabasebackupLog::get()->filter(array("FullLocation" => $higherFileLocation))->First(); |
|
305
|
|
|
if ($obj) { |
|
306
|
|
|
$obj->delete(); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
clearstatcache(); |
|
310
|
|
View Code Duplication |
if (rename($lowerFileLocation, $higherFileLocation)) { |
|
|
|
|
|
|
311
|
|
|
$obj = DatabasebackupLog::get()->filter(array("FullLocation" => $lowerFileLocation))->First(); |
|
312
|
|
|
if ($obj) { |
|
313
|
|
|
$obj->FullLocation = $higherFileLocation; |
|
314
|
|
|
$obj->write(); |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
//just in case there were NO DBes to cycle.... |
|
321
|
|
|
if (!isset($lowerFileLocation)) { |
|
322
|
|
|
$lowerFileLocation = $this->olderBackupFileName($fileLocation, ".0.bak"); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
clearstatcache(); |
|
326
|
|
|
if (file_exists($fileLocation)) { |
|
327
|
|
|
if (file_exists($lowerFileLocation)) { |
|
328
|
|
|
unlink($lowerFileLocation); |
|
329
|
|
|
clearstatcache(); |
|
330
|
|
View Code Duplication |
if (!file_exists($lowerFileLocation)) { |
|
|
|
|
|
|
331
|
|
|
$obj = DatabasebackupLog::get()->filter(array("FullLocation" => $lowerFileLocation))->First(); |
|
332
|
|
|
if ($obj) { |
|
333
|
|
|
$obj->delete(); |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
clearstatcache(); |
|
338
|
|
View Code Duplication |
if (rename($fileLocation, $lowerFileLocation)) { |
|
|
|
|
|
|
339
|
|
|
$obj = DatabasebackupLog::get()->filter(array("FullLocation" => $fileLocation))->First(); |
|
340
|
|
|
if ($obj) { |
|
341
|
|
|
$obj->FullLocation = $lowerFileLocation; |
|
342
|
|
|
$obj->write(); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
return $fileLocation; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* returns best file location with compression extension... |
|
351
|
|
|
* |
|
352
|
|
|
* @return String | Null |
|
353
|
|
|
*/ |
|
354
|
|
|
protected function getFullLocationWithExtension() |
|
355
|
|
|
{ |
|
356
|
|
|
$fileLocation = $this->Config()->get("full_location_for_db_backup_file"); |
|
357
|
|
|
if ($fileLocation) { |
|
358
|
|
|
$compression = $this->Config()->get("compression"); |
|
359
|
|
|
if ($compression == "gzip") { |
|
360
|
|
|
$fileLocation .= ".gz"; |
|
361
|
|
|
} |
|
362
|
|
|
return $fileLocation; |
|
363
|
|
|
} |
|
364
|
|
|
return null; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* returns file name for older back up file (cycled one) |
|
369
|
|
|
* |
|
370
|
|
|
* @param String $fileLocation |
|
371
|
|
|
* @param Int $position |
|
372
|
|
|
* |
|
373
|
|
|
* @return String |
|
374
|
|
|
*/ |
|
375
|
|
|
protected function olderBackupFileName($fileLocation, $position) |
|
376
|
|
|
{ |
|
377
|
|
|
return $fileLocation.".".$position.".bak"; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* check for existing backups |
|
382
|
|
|
* |
|
383
|
|
|
*/ |
|
384
|
|
|
public function requireDefaultRecords() |
|
385
|
|
|
{ |
|
386
|
|
|
parent::requireDefaultRecords(); |
|
387
|
|
|
$array = array($this->getFullLocationWithExtension()); |
|
388
|
|
|
$arrayOfIDs = DatabasebackupLog::get()->map("ID", "ID")->toArray(); |
|
389
|
|
|
for ($i = 0; $i < 100; $i++) { |
|
390
|
|
|
$array[] = $this->olderBackupFileName($array[0], $i); |
|
391
|
|
|
} |
|
392
|
|
|
foreach ($array as $fileLocation) { |
|
393
|
|
|
clearstatcache(); |
|
394
|
|
|
if (file_exists($fileLocation)) { |
|
395
|
|
|
$obj = DatabasebackupLog::get()->filter(array("FullLocation" => $fileLocation))->First(); |
|
396
|
|
|
if ($obj) { |
|
397
|
|
|
//do nothing |
|
398
|
|
|
} else { |
|
399
|
|
|
$className = $this->class; |
|
400
|
|
|
$obj = new $className; |
|
401
|
|
|
//make sure it has a full file location! |
|
402
|
|
|
$obj->FullLocation = $fileLocation; |
|
403
|
|
|
$obj->write(); |
|
404
|
|
|
} |
|
405
|
|
|
unset($arrayOfIDs[$obj->ID]); |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
$objects = DatabasebackupLog::get()->filter(array("ID" => $arrayOfIDs)); |
|
409
|
|
|
foreach ($objects as $obj) { |
|
410
|
|
|
$obj->delete(); |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
|