From 75c968b4ae07ba424a19fb2a61ba4d5d2e17be58 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 4 Oct 2018 13:10:48 +0200 Subject: [PATCH] FIX Backup of database without mysqladmin available from cron. --- htdocs/admin/tools/export.php | 173 --------------------- htdocs/core/class/utils.class.php | 240 ++++++++++++++++++++++++++++-- 2 files changed, 224 insertions(+), 189 deletions(-) diff --git a/htdocs/admin/tools/export.php b/htdocs/admin/tools/export.php index f3d2057395f..e10b5530f52 100644 --- a/htdocs/admin/tools/export.php +++ b/htdocs/admin/tools/export.php @@ -235,176 +235,3 @@ $time_end = time(); $db->close(); - - -// MYSQL NO BINARIES (only php) -/** Backup the db OR just a table without mysqldump binary (does not require any exec permission) - * Author: David Walsh (http://davidwalsh.name/backup-mysql-database-php) - * Updated and enhanced by Stephen Larroque (lrq3000) and by the many commentators from the blog - * Note about foreign keys constraints: for Dolibarr, since there are a lot of constraints and when imported the tables will be inserted in the dumped order, not in constraints order, then we ABSOLUTELY need to use SET FOREIGN_KEY_CHECKS=0; when importing the sql dump. - * Note2: db2SQL by Howard Yeend can be an alternative, by using SHOW FIELDS FROM and SHOW KEYS FROM we could generate a more precise dump (eg: by getting the type of the field and then precisely outputting the right formatting - in quotes, numeric or null - instead of trying to guess like we are doing now). - * - * @param string $outputfile Output file name - * @param string $tables Table name or '*' for all - * @return int <0 if KO, >0 if OK - */ -function backup_tables($outputfile, $tables='*') -{ - global $db, $langs; - global $errormsg; - - // Set to UTF-8 - if (is_a($db, 'DoliDBMysqli')) { - /** @var DoliDBMysqli $db */ - $db->db->set_charset('utf8'); - } else { - /** @var DoliDB $db */ - $db->query('SET NAMES utf8'); - $db->query('SET CHARACTER SET utf8'); - } - - //get all of the tables - if ($tables == '*') - { - $tables = array(); - $result = $db->query('SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\''); - while($row = $db->fetch_row($result)) - { - $tables[] = $row[0]; - } - } - else - { - $tables = is_array($tables) ? $tables : explode(',',$tables); - } - - //cycle through - $handle = fopen($outputfile, 'w+'); - if (fwrite($handle, '') === false) - { - $langs->load("errors"); - dol_syslog("Failed to open file ".$outputfile,LOG_ERR); - $errormsg=$langs->trans("ErrorFailedToWriteInDir"); - return -1; - } - - // Print headers and global mysql config vars - $sqlhead = ''; - $sqlhead .= "-- ".$db::LABEL." dump via php with Dolibarr ".DOL_VERSION." --- --- Host: ".$db->db->host_info." Database: ".$db->database_name." --- ------------------------------------------------------ --- Server version ".$db->db->server_info." - -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8 */; -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; -/*!40103 SET TIME_ZONE='+00:00' */; -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; - -"; - - if (GETPOST("nobin_disable_fk")) $sqlhead .= "SET FOREIGN_KEY_CHECKS=0;\n"; - //$sqlhead .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n"; - if (GETPOST("nobin_use_transaction")) $sqlhead .= "SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n"; - - fwrite($handle, $sqlhead); - - $ignore = ''; - if (GETPOST("nobin_sql_ignore")) $ignore = 'IGNORE '; - $delayed = ''; - if (GETPOST("nobin_delayed")) $delayed = 'DELAYED '; - - // Process each table and print their definition + their datas - foreach($tables as $table) - { - // Saving the table structure - fwrite($handle, "\n--\n-- Table structure for table `".$table."`\n--\n"); - - if (GETPOST("nobin_drop")) fwrite($handle,"DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it - fwrite($handle,"/*!40101 SET @saved_cs_client = @@character_set_client */;\n"); - fwrite($handle,"/*!40101 SET character_set_client = utf8 */;\n"); - $resqldrop=$db->query('SHOW CREATE TABLE '.$table); - $row2 = $db->fetch_row($resqldrop); - if (empty($row2[1])) - { - fwrite($handle, "\n-- WARNING: Show create table ".$table." return empy string when it should not.\n"); - } - else - { - fwrite($handle,$row2[1].";\n"); - //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n"); - - // Dumping the data (locking the table and disabling the keys check while doing the process) - fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n"); - if (!GETPOST("nobin_nolocks")) fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back) - if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n"); - else fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n"); - - $sql='SELECT * FROM '.$table; - $result = $db->query($sql); - while($row = $db->fetch_row($result)) - { - // For each row of data we print a line of INSERT - fwrite($handle,'INSERT '.$delayed.$ignore.'INTO `'.$table.'` VALUES ('); - $columns = count($row); - for($j=0; $j<$columns; $j++) { - // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier) - if ($row[$j] == null && !is_string($row[$j])) { - // IMPORTANT: if the field is NULL we set it NULL - $row[$j] = 'NULL'; - } elseif(is_string($row[$j]) && $row[$j] == '') { - // if it's an empty string, we set it as an empty string - $row[$j] = "''"; - } elseif(is_numeric($row[$j]) && !strcmp($row[$j], $row[$j]+0) ) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0) - // if it's a number, we return it as-is -// $row[$j] = $row[$j]; - } else { // else for all other cases we escape the value and put quotes around - $row[$j] = addslashes($row[$j]); - $row[$j] = preg_replace("#\n#", "\\n", $row[$j]); - $row[$j] = "'".$row[$j]."'"; - } - } - fwrite($handle,implode(',', $row).");\n"); - } - if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking - if (!GETPOST("nobin_nolocks")) fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table - fwrite($handle,"\n\n\n"); - } - } - - /* Backup Procedure structure*/ - /* - $result = $db->query('SHOW PROCEDURE STATUS'); - if ($db->num_rows($result) > 0) - { - while ($row = $db->fetch_row($result)) { $procedures[] = $row[1]; } - foreach($procedures as $proc) - { - fwrite($handle,"DELIMITER $$\n\n"); - fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n"); - $resqlcreateproc=$db->query("SHOW CREATE PROCEDURE '$proc'"); - $row2 = $db->fetch_row($resqlcreateproc); - fwrite($handle,"\n".$row2[2]."$$\n\n"); - fwrite($handle,"DELIMITER ;\n\n"); - } - } - */ - /* Backup Procedure structure*/ - - // Write the footer (restore the previous database settings) - $sqlfooter="\n\n"; - if (GETPOST("nobin_use_transaction")) $sqlfooter .= "COMMIT;\n"; - if (GETPOST("nobin_disable_fk")) $sqlfooter .= "SET FOREIGN_KEY_CHECKS=1;\n"; - $sqlfooter.="\n\n-- Dump completed on ".date('Y-m-d G-i-s'); - fwrite($handle, $sqlfooter); - - fclose($handle); - - return 1; -} diff --git a/htdocs/core/class/utils.class.php b/htdocs/core/class/utils.class.php index 3a7a79122c9..b0b641eca57 100644 --- a/htdocs/core/class/utils.class.php +++ b/htdocs/core/class/utils.class.php @@ -176,14 +176,14 @@ class Utils * @param int $usedefault 1=Use default backup profile (Set this to 1 when used as cron) * @param string $file 'auto' or filename to build * @param int $keeplastnfiles Keep only last n files (not used yet) + * @param int $execmethod 0=Use default method (that is 1 by default), 1=Use the PHP 'exec', 2=Use the 'popen' method * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) */ - function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0) + function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0, $execmethod=0) { global $db, $conf, $langs, $dolibarr_main_data_root; global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass; - $langs->load("admin"); dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG); @@ -291,21 +291,58 @@ class Utils if ($handle) { + $execmethod=1; + if (! empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod=$conf->global->MAIN_EXEC_USE_POPEN; + if (empty($execmethod)) $execmethod=1; + $ok=0; - dol_syslog("Run command ".$fullcommandcrypted); - $handlein = popen($fullcommandclear, 'r'); - $i=0; - while (!feof($handlein)) + dol_syslog("Run command with method ".$execmethod." with ".$fullcommandcrypted); + + // TODO Replace with executeCLI function + if ($execmethod == 1) { - $i++; // output line number - $read = fgets($handlein); - // Exclude warning line we don't want - if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue; - fwrite($handle,$read); - if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1; - elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; + exec($fullcommandclear, $readt, $retval); + $result = $retval; + + if ($retval != 0) + { + $langs->load("errors"); + dol_syslog("Datadump retval after exec=".$retval, LOG_ERR); + $error = 'Error '.$retval; + $ok=0; + } + else + { + $i=0; + if (!empty($readt)) + foreach($readt as $key=>$read) + { + $i++; // output line number + if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue; + fwrite($handle,$read); + if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1; + elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; + } + } } - pclose($handlein); + if ($execmethod == 2) // With this method, there is no way to get the return code, only output + { + $handlein = popen($fullcommandclear, 'r'); + $i=0; + while (!feof($handlein)) + { + $i++; // output line number + $read = fgets($handlein); + // Exclude warning line we don't want + if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue; + fwrite($handle,$read); + if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1; + elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; + } + pclose($handlein); + + } + if ($compression == 'none') fclose($handle); if ($compression == 'gz') gzclose($handle); @@ -370,13 +407,13 @@ class Utils if ($compression == 'gz' or $compression == 'bz') { - backup_tables($outputfiletemp); + $this->backup_tables($outputfiletemp); dol_compress_file($outputfiletemp, $outputfile, $compression); unlink($outputfiletemp); } else { - backup_tables($outputfile); + $this->backup_tables($outputfile); } $this->output = ""; @@ -782,4 +819,175 @@ class Utils $this->output = 'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.' files) done.'; return 0; } + + /** Backup the db OR just a table without mysqldump binary, with PHP only (does not require any exec permission) + * Author: David Walsh (http://davidwalsh.name/backup-mysql-database-php) + * Updated and enhanced by Stephen Larroque (lrq3000) and by the many commentators from the blog + * Note about foreign keys constraints: for Dolibarr, since there are a lot of constraints and when imported the tables will be inserted in the dumped order, not in constraints order, then we ABSOLUTELY need to use SET FOREIGN_KEY_CHECKS=0; when importing the sql dump. + * Note2: db2SQL by Howard Yeend can be an alternative, by using SHOW FIELDS FROM and SHOW KEYS FROM we could generate a more precise dump (eg: by getting the type of the field and then precisely outputting the right formatting - in quotes, numeric or null - instead of trying to guess like we are doing now). + * + * @param string $outputfile Output file name + * @param string $tables Table name or '*' for all + * @return int <0 if KO, >0 if OK + */ + function backup_tables($outputfile, $tables='*') + { + global $db, $langs; + global $errormsg; + + // Set to UTF-8 + if (is_a($db, 'DoliDBMysqli')) { + /** @var DoliDBMysqli $db */ + $db->db->set_charset('utf8'); + } else { + /** @var DoliDB $db */ + $db->query('SET NAMES utf8'); + $db->query('SET CHARACTER SET utf8'); + } + + //get all of the tables + if ($tables == '*') + { + $tables = array(); + $result = $db->query('SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\''); + while($row = $db->fetch_row($result)) + { + $tables[] = $row[0]; + } + } + else + { + $tables = is_array($tables) ? $tables : explode(',',$tables); + } + + //cycle through + $handle = fopen($outputfile, 'w+'); + if (fwrite($handle, '') === false) + { + $langs->load("errors"); + dol_syslog("Failed to open file ".$outputfile,LOG_ERR); + $errormsg=$langs->trans("ErrorFailedToWriteInDir"); + return -1; + } + + // Print headers and global mysql config vars + $sqlhead = ''; + $sqlhead .= "-- ".$db::LABEL." dump via php with Dolibarr ".DOL_VERSION." +-- +-- Host: ".$db->db->host_info." Database: ".$db->database_name." +-- ------------------------------------------------------ +-- Server version ".$db->db->server_info." + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +"; + + if (GETPOST("nobin_disable_fk")) $sqlhead .= "SET FOREIGN_KEY_CHECKS=0;\n"; + //$sqlhead .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n"; + if (GETPOST("nobin_use_transaction")) $sqlhead .= "SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n"; + + fwrite($handle, $sqlhead); + + $ignore = ''; + if (GETPOST("nobin_sql_ignore")) $ignore = 'IGNORE '; + $delayed = ''; + if (GETPOST("nobin_delayed")) $delayed = 'DELAYED '; + + // Process each table and print their definition + their datas + foreach($tables as $table) + { + // Saving the table structure + fwrite($handle, "\n--\n-- Table structure for table `".$table."`\n--\n"); + + if (GETPOST("nobin_drop")) fwrite($handle,"DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it + fwrite($handle,"/*!40101 SET @saved_cs_client = @@character_set_client */;\n"); + fwrite($handle,"/*!40101 SET character_set_client = utf8 */;\n"); + $resqldrop=$db->query('SHOW CREATE TABLE '.$table); + $row2 = $db->fetch_row($resqldrop); + if (empty($row2[1])) + { + fwrite($handle, "\n-- WARNING: Show create table ".$table." return empy string when it should not.\n"); + } + else + { + fwrite($handle,$row2[1].";\n"); + //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n"); + + // Dumping the data (locking the table and disabling the keys check while doing the process) + fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n"); + if (!GETPOST("nobin_nolocks")) fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back) + if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n"); + else fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n"); + + $sql='SELECT * FROM '.$table; + $result = $db->query($sql); + while($row = $db->fetch_row($result)) + { + // For each row of data we print a line of INSERT + fwrite($handle,'INSERT '.$delayed.$ignore.'INTO `'.$table.'` VALUES ('); + $columns = count($row); + for($j=0; $j<$columns; $j++) { + // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier) + if ($row[$j] == null && !is_string($row[$j])) { + // IMPORTANT: if the field is NULL we set it NULL + $row[$j] = 'NULL'; + } elseif(is_string($row[$j]) && $row[$j] == '') { + // if it's an empty string, we set it as an empty string + $row[$j] = "''"; + } elseif(is_numeric($row[$j]) && !strcmp($row[$j], $row[$j]+0) ) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0) + // if it's a number, we return it as-is + // $row[$j] = $row[$j]; + } else { // else for all other cases we escape the value and put quotes around + $row[$j] = addslashes($row[$j]); + $row[$j] = preg_replace("#\n#", "\\n", $row[$j]); + $row[$j] = "'".$row[$j]."'"; + } + } + fwrite($handle,implode(',', $row).");\n"); + } + if (GETPOST("nobin_disable_fk")) fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking + if (!GETPOST("nobin_nolocks")) fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table + fwrite($handle,"\n\n\n"); + } + } + + /* Backup Procedure structure*/ + /* + $result = $db->query('SHOW PROCEDURE STATUS'); + if ($db->num_rows($result) > 0) + { + while ($row = $db->fetch_row($result)) { $procedures[] = $row[1]; } + foreach($procedures as $proc) + { + fwrite($handle,"DELIMITER $$\n\n"); + fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n"); + $resqlcreateproc=$db->query("SHOW CREATE PROCEDURE '$proc'"); + $row2 = $db->fetch_row($resqlcreateproc); + fwrite($handle,"\n".$row2[2]."$$\n\n"); + fwrite($handle,"DELIMITER ;\n\n"); + } + } + */ + /* Backup Procedure structure*/ + + // Write the footer (restore the previous database settings) + $sqlfooter="\n\n"; + if (GETPOST("nobin_use_transaction")) $sqlfooter .= "COMMIT;\n"; + if (GETPOST("nobin_disable_fk")) $sqlfooter .= "SET FOREIGN_KEY_CHECKS=1;\n"; + $sqlfooter.="\n\n-- Dump completed on ".date('Y-m-d G-i-s'); + fwrite($handle, $sqlfooter); + + fclose($handle); + + return 1; + } } \ No newline at end of file