Skip to content

Commit

Permalink
backup in compress mode - fixed #4890
Browse files Browse the repository at this point in the history
  • Loading branch information
yllen committed Apr 25, 2014
1 parent fd50a89 commit 9313b17
Showing 1 changed file with 79 additions and 44 deletions.
123 changes: 79 additions & 44 deletions front/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@
}


// les deux options qui suivent devraient etre incluses dans le fichier de config plutot non ?
// 1 only with ZLib support, else change value to 0
$compression = 0;

if ($compression == 1) {
$filetype = "sql.gz";
} else {
$filetype = "sql";
}


/**
* Genere un fichier backup.xml a partir de base dbhost connecte avec l'utilisateur dbuser
* et le mot de passe dbpassword sur le serveur dbdefault
Expand Down Expand Up @@ -242,7 +231,11 @@ function restoreMySqlDump($DB, $dumpFile, $duree) {
echo sprintf(__('File %s not found.'), $dumpFile)."<br>";
return false;
}
$fileHandle = fopen($dumpFile, "rb");
if (substr($dumpFile, -2) == "gz") {
$fileHandle = gzopen($dumpFile, "rb");
} else {
$fileHandle = fopen($dumpFile, "rb");
}

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

if ($offset != 0) {
if (fseek($fileHandle,$offset,SEEK_SET) != 0) { //erreur
//TRANS: %s is the number of the byte
printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0));
echo "<br>";
return false;
if (substr($dumpFile, -2) == "gz") {
if (gzseek($fileHandle,$offset,SEEK_SET) != 0) { //erreur
//TRANS: %s is the number of the byte
printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0));
echo "<br>";
return false;
}
} else {
if (fseek($fileHandle,$offset,SEEK_SET) != 0) { //erreur
//TRANS: %s is the number of the byte
printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0));
echo "<br>";
return false;
}
}
Html::glpi_flush();
}

$formattedQuery = "";

while (!feof($fileHandle)) {
current_time();
if (($duree > 0)
&& ($TPSCOUR >= $duree)) { //on atteint la fin du temps imparti
return true;
}
if (substr($dumpFile, -2) == "gz") {
while (!gzeof($fileHandle)) {
current_time();
if (($duree > 0)
&& ($TPSCOUR >= $duree)) { //on atteint la fin du temps imparti
return true;
}

// specify read length to be able to read long lines
$buffer = fgets($fileHandle, 102400);
// specify read length to be able to read long lines
$buffer = gzgets($fileHandle, 102400);

// do not strip comments due to problems when # in begin of a data line
$formattedQuery .= $buffer;
if (Toolbox::get_magic_quotes_runtime()) {
$formattedQuery = stripslashes($formattedQuery);
// do not strip comments due to problems when # in begin of a data line
$formattedQuery .= $buffer;
if (Toolbox::get_magic_quotes_runtime()) {
$formattedQuery = stripslashes($formattedQuery);
}

if (substr(rtrim($formattedQuery),-1) == ";") {
// Do not use the $DB->query
if ($DB->query($formattedQuery)) { //if no success continue to concatenate
$offset = gztell($fileHandle);
$formattedQuery = "";
$cpt++;
}
}
}
} else {
while (!feof($fileHandle)) {
current_time();
if (($duree > 0)
&& ($TPSCOUR >= $duree)) { //on atteint la fin du temps imparti
return true;
}

// specify read length to be able to read long lines
$buffer = fgets($fileHandle, 102400);

if (substr(rtrim($formattedQuery),-1) == ";") {
// Do not use the $DB->query
if ($DB->query($formattedQuery)) { //if no success continue to concatenate
$offset = ftell($fileHandle);
$formattedQuery = "";
$cpt++;
// do not strip comments due to problems when # in begin of a data line
$formattedQuery .= $buffer;
if (Toolbox::get_magic_quotes_runtime()) {
$formattedQuery = stripslashes($formattedQuery);
}

if (substr(rtrim($formattedQuery),-1) == ";") {
// Do not use the $DB->query
if ($DB->query($formattedQuery)) { //if no success continue to concatenate
$offset = ftell($fileHandle);
$formattedQuery = "";
$cpt++;
}
}
}
}
Expand All @@ -295,7 +325,11 @@ function restoreMySqlDump($DB, $dumpFile, $duree) {
echo "<br>".$DB->error()."<hr>";
}

fclose($fileHandle);
if (substr($dumpFile, -2) == "gz") {
gzclose($fileHandle);
} else {
fclose($fileHandle);
}
$offset = -1;
return true;
}
Expand All @@ -314,7 +348,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
// $dumpFile, fichier source
// $duree=timeout pour changement de page (-1 = aucun)

$fileHandle = fopen($dumpFile, "a");
$fileHandle = gzopen($dumpFile, "a");

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

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

if ($rowtodump > 0) {
fwrite ($fileHandle,$todump);
gzwrite ($fileHandle,$todump);
$cpt += $rowtodump;
$offsetrow += $rowlimit;
if ($rowtodump<$rowlimit) {
Expand Down Expand Up @@ -390,7 +424,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
echo "<br>".$DB->error()."<hr>";
}
$offsettable = -1;
fclose($fileHandle);
gzclose($fileHandle);
return true;
}

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

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

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

Html::footer();
?>
?>

0 comments on commit 9313b17

Please sign in to comment.