X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=admin%2Fdb%2Fmaintenance_db.inc;h=ce8e550619a22ee44fb388c06a54a1b04eac3d22;hb=7840540918bfd37c886877a5d52e3ba0b3373289;hp=cd4afdacf477f1178da6fd18352834eddaa0abba;hpb=24a22844421c7b78b7988e23e8d7c4d6fd30f749;p=fa-stable.git diff --git a/admin/db/maintenance_db.inc b/admin/db/maintenance_db.inc index cd4afdac..ce8e5506 100644 --- a/admin/db/maintenance_db.inc +++ b/admin/db/maintenance_db.inc @@ -18,6 +18,8 @@ * @param $strSortType String containing either asc or desc [default to asc] * @desc Naturally sorts an array using by the column $strSortBy */ +define('EXPORT_MAX_INSERT', 50000); + function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false) { // if the parameters are invalid @@ -444,23 +446,27 @@ function db_unzip($mode, $path) return explode("\n", $file_data); } -function db_backup($conn, $ext='no', $comm='', $tbpref = TB_PREF) +function db_backup($conn, $ext='no', $comm='', $path=null) { if ($conn['tbpref'] != "") $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql"; else $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql"; - return db_export($conn, clean_file_name($filename), $ext, $comm, $tbpref); + if (!isset($path)) + $path = BACKUP_PATH; + + return db_export($conn, $path . clean_file_name($filename), $ext, $comm); } // generates a dump of $db database // $drop and $zip tell if to include the drop table statement or dry to pack -function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, $no_default=false) +function db_export($conn, $filename, $zip='no', $comment='', $no_default=false) { global $app_title, $version, $power_url, $path_to_root; + $error = false; // set max string size before writing to file $max_size = 1048576 * 2; // 2 MB @@ -475,7 +481,7 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, $backupfile = $filename . ".zip"; else $backupfile = $filename; - $company = get_company_pref('coy_name', $tbpref); + $company = $conn['name']; // get_company_pref('coy_name'); //create comment $out="# MySQL dump of database '".$conn["dbname"]."' on host '".$conn["host"]."'\n"; @@ -555,12 +561,12 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, // if ($auto_incr[$tablename]) // $out.=" AUTO_INCREMENT=".$auto_incr[$tablename]; $out.=" ;"; - $out.="\n\n\n"; + $out.="\n\n"; // export data if (!$error) { - $out.="### Data of table `".$tablename."` ###\n\n"; + $out.="### Data of table `".$tablename."` ###\n"; // check if field types are NULL or NOT NULL $res3 = db_query("SHOW COLUMNS FROM `" . $tablename . "`"); @@ -573,18 +579,40 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, } $res2 = db_query("SELECT * FROM `" . $tablename . "`"); + $maxinsert = 0; + $insert = ''; for ($j = 0; $j < db_num_rows($res2); $j++) { - $out .= "INSERT INTO `" . $tablename . "` VALUES ("; $row2 = db_fetch_row($res2); - // run through each field + $values = '('; for ($k = 0; $k < $nf = db_num_fields($res2); $k++) { - $out .= db_escape($row2[$k], $field_null[$k]); + $values .= db_escape($row2[$k], $field_null[$k]); if ($k < ($nf - 1)) - $out .= ", "; + $values .= ', '; + } + $values .= ')'; + $len = strlen($values); + if ($maxinsert < $len+1) + { + $maxinsert = EXPORT_MAX_INSERT; + if ($insert) + { + $out .= $insert .';'; // flush insert query + $insert = ''; + } } - $out .= ");\n"; + + if ($insert == '') + { + $insert = "\nINSERT INTO `" . $tablename . "` VALUES\n"; + $maxinsert -= strlen($insert); + } else { + $insert .= ",\n"; + } + + $maxinsert -= $len; + $insert .= $values; // if saving is successful, then empty $out, else set error flag if (strlen($out) > $max_size && $zip != "zip") @@ -595,12 +623,13 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, $error = true; } } - + if ($insert) + $out .= $insert. ';'; // an error occurred! Try to delete file and return error status } elseif ($error) { - @unlink(BACKUP_PATH . $backupfile); + @unlink($backupfile); return false; } @@ -618,7 +647,7 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, } else { - @unlink(BACKUP_PATH . $backupfile); + @unlink($backupfile); return false; } @@ -630,7 +659,7 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, } else { - @unlink(BACKUP_PATH . $backupfile); + @unlink($backupfile); return false; } return $backupfile; @@ -687,13 +716,15 @@ function order_sql_tables($tables, $fks) // saves the string in $fileData to the file $backupfile as gz file or not ($zip) // returns backup file name if name has changed (zip), else TRUE. If saving failed, return value is FALSE -function save_to_file($backupfile, $zip, $fileData) +function save_to_file($path, $zip, $fileData) { global $path_to_root; + $backupfile = basename($path); + if ($zip == "gzip") { - if ($zp = @gzopen(BACKUP_PATH . $backupfile, "a9")) + if ($zp = @gzopen($path, "a9")) { @gzwrite($zp, $fileData); @gzclose($zp); @@ -770,7 +801,7 @@ function save_to_file($backupfile, $zip, $fileData) // total # of entries "on this disk", total # of entries overall, size of central dir, offset to start of central dir, .zip file comment length $fileData .= pack('v', 1) . pack('v', 1) . pack('V', strlen($cdrec)) . pack('V', strlen($fr)) . "\x00\x00"; - if ($zp = @fopen(BACKUP_PATH . $backupfile, "a")) + if ($zp = @fopen($path, "a")) { @fwrite($zp, $fileData); @fclose($zp); @@ -785,7 +816,7 @@ function save_to_file($backupfile, $zip, $fileData) } else { - if ($zp = @fopen(BACKUP_PATH . $backupfile, "a")) + if ($zp = @fopen($path, "a")) { @fwrite($zp, $fileData); @fclose($zp);