Skip to content

Commit 9313b17

Browse files
author
yllen
committed
backup in compress mode - fixed #4890
1 parent fd50a89 commit 9313b17

File tree

1 file changed

+79
-44
lines changed

1 file changed

+79
-44
lines changed

front/backup.php

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@
6262
}
6363

6464

65-
// les deux options qui suivent devraient etre incluses dans le fichier de config plutot non ?
66-
// 1 only with ZLib support, else change value to 0
67-
$compression = 0;
68-
69-
if ($compression == 1) {
70-
$filetype = "sql.gz";
71-
} else {
72-
$filetype = "sql";
73-
}
74-
75-
7665
/**
7766
* Genere un fichier backup.xml a partir de base dbhost connecte avec l'utilisateur dbuser
7867
* et le mot de passe dbpassword sur le serveur dbdefault
@@ -242,7 +231,11 @@ function restoreMySqlDump($DB, $dumpFile, $duree) {
242231
echo sprintf(__('File %s not found.'), $dumpFile)."<br>";
243232
return false;
244233
}
245-
$fileHandle = fopen($dumpFile, "rb");
234+
if (substr($dumpFile, -2) == "gz") {
235+
$fileHandle = gzopen($dumpFile, "rb");
236+
} else {
237+
$fileHandle = fopen($dumpFile, "rb");
238+
}
246239

247240
if (!$fileHandle) {
248241
//TRASN: %s is the name of the file
@@ -251,39 +244,76 @@ function restoreMySqlDump($DB, $dumpFile, $duree) {
251244
}
252245

253246
if ($offset != 0) {
254-
if (fseek($fileHandle,$offset,SEEK_SET) != 0) { //erreur
255-
//TRANS: %s is the number of the byte
256-
printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0));
257-
echo "<br>";
258-
return false;
247+
if (substr($dumpFile, -2) == "gz") {
248+
if (gzseek($fileHandle,$offset,SEEK_SET) != 0) { //erreur
249+
//TRANS: %s is the number of the byte
250+
printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0));
251+
echo "<br>";
252+
return false;
253+
}
254+
} else {
255+
if (fseek($fileHandle,$offset,SEEK_SET) != 0) { //erreur
256+
//TRANS: %s is the number of the byte
257+
printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0));
258+
echo "<br>";
259+
return false;
260+
}
259261
}
260262
Html::glpi_flush();
261263
}
262264

263265
$formattedQuery = "";
264266

265-
while (!feof($fileHandle)) {
266-
current_time();
267-
if (($duree > 0)
268-
&& ($TPSCOUR >= $duree)) { //on atteint la fin du temps imparti
269-
return true;
270-
}
267+
if (substr($dumpFile, -2) == "gz") {
268+
while (!gzeof($fileHandle)) {
269+
current_time();
270+
if (($duree > 0)
271+
&& ($TPSCOUR >= $duree)) { //on atteint la fin du temps imparti
272+
return true;
273+
}
271274

272-
// specify read length to be able to read long lines
273-
$buffer = fgets($fileHandle, 102400);
275+
// specify read length to be able to read long lines
276+
$buffer = gzgets($fileHandle, 102400);
274277

275-
// do not strip comments due to problems when # in begin of a data line
276-
$formattedQuery .= $buffer;
277-
if (Toolbox::get_magic_quotes_runtime()) {
278-
$formattedQuery = stripslashes($formattedQuery);
278+
// do not strip comments due to problems when # in begin of a data line
279+
$formattedQuery .= $buffer;
280+
if (Toolbox::get_magic_quotes_runtime()) {
281+
$formattedQuery = stripslashes($formattedQuery);
282+
}
283+
284+
if (substr(rtrim($formattedQuery),-1) == ";") {
285+
// Do not use the $DB->query
286+
if ($DB->query($formattedQuery)) { //if no success continue to concatenate
287+
$offset = gztell($fileHandle);
288+
$formattedQuery = "";
289+
$cpt++;
290+
}
291+
}
279292
}
293+
} else {
294+
while (!feof($fileHandle)) {
295+
current_time();
296+
if (($duree > 0)
297+
&& ($TPSCOUR >= $duree)) { //on atteint la fin du temps imparti
298+
return true;
299+
}
300+
301+
// specify read length to be able to read long lines
302+
$buffer = fgets($fileHandle, 102400);
280303

281-
if (substr(rtrim($formattedQuery),-1) == ";") {
282-
// Do not use the $DB->query
283-
if ($DB->query($formattedQuery)) { //if no success continue to concatenate
284-
$offset = ftell($fileHandle);
285-
$formattedQuery = "";
286-
$cpt++;
304+
// do not strip comments due to problems when # in begin of a data line
305+
$formattedQuery .= $buffer;
306+
if (Toolbox::get_magic_quotes_runtime()) {
307+
$formattedQuery = stripslashes($formattedQuery);
308+
}
309+
310+
if (substr(rtrim($formattedQuery),-1) == ";") {
311+
// Do not use the $DB->query
312+
if ($DB->query($formattedQuery)) { //if no success continue to concatenate
313+
$offset = ftell($fileHandle);
314+
$formattedQuery = "";
315+
$cpt++;
316+
}
287317
}
288318
}
289319
}
@@ -295,7 +325,11 @@ function restoreMySqlDump($DB, $dumpFile, $duree) {
295325
echo "<br>".$DB->error()."<hr>";
296326
}
297327

298-
fclose($fileHandle);
328+
if (substr($dumpFile, -2) == "gz") {
329+
gzclose($fileHandle);
330+
} else {
331+
fclose($fileHandle);
332+
}
299333
$offset = -1;
300334
return true;
301335
}
@@ -314,7 +348,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
314348
// $dumpFile, fichier source
315349
// $duree=timeout pour changement de page (-1 = aucun)
316350

317-
$fileHandle = fopen($dumpFile, "a");
351+
$fileHandle = gzopen($dumpFile, "a");
318352

319353
if (!$fileHandle) {
320354
//TRANS: %s is the name of the file
@@ -326,7 +360,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
326360
$time_file = date("Y-m-d-H-i");
327361
$cur_time = date("Y-m-d H:i");
328362
$todump = "#GLPI Dump database on $cur_time\n";
329-
fwrite ($fileHandle, $todump);
363+
gzwrite ($fileHandle, $todump);
330364
}
331365

332366
$result = $DB->list_tables();
@@ -340,7 +374,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
340374
// Dump de la structure table
341375
if ($offsetrow == -1) {
342376
$todump = "\n".get_def($DB,$tables[$offsettable]);
343-
fwrite ($fileHandle,$todump);
377+
gzwrite ($fileHandle,$todump);
344378
$offsetrow++;
345379
$cpt++;
346380
}
@@ -355,7 +389,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
355389
$rowtodump = substr_count($todump, "INSERT INTO");
356390

357391
if ($rowtodump > 0) {
358-
fwrite ($fileHandle,$todump);
392+
gzwrite ($fileHandle,$todump);
359393
$cpt += $rowtodump;
360394
$offsetrow += $rowlimit;
361395
if ($rowtodump<$rowlimit) {
@@ -390,7 +424,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
390424
echo "<br>".$DB->error()."<hr>";
391425
}
392426
$offsettable = -1;
393-
fclose($fileHandle);
427+
gzclose($fileHandle);
394428
return true;
395429
}
396430

@@ -400,7 +434,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
400434
if (isset($_GET["dump"]) && $_GET["dump"] != "") {
401435
$time_file = date("Y-m-d-H-i");
402436
$cur_time = date("Y-m-d H:i");
403-
$filename = $path . "/glpi-".GLPI_VERSION."-$time_file.$filetype";
437+
$filename = $path . "/glpi-".GLPI_VERSION."-$time_file.sql.gz";
404438

405439
if (!isset($_GET["duree"]) && is_file($filename)) {
406440
echo "<div class='center'>".__('The file already exists')."</div>";
@@ -598,7 +632,8 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
598632
$files = array();
599633
while ($file = readdir($dir)) {
600634
if (($file != ".") && ($file != "..")
601-
&& preg_match("/\.sql$/i",$file)) {
635+
&& (preg_match("/\.sql.gz$/i",$file)
636+
|| preg_match("/\.sql$/i",$file))) {
602637

603638
$files[$file] = filemtime($path."/".$file);
604639
}
@@ -680,4 +715,4 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
680715
echo "</div>";
681716

682717
Html::footer();
683-
?>
718+
?>

0 commit comments

Comments
 (0)