2006-08-20 21:15:47 +02:00
< ? php
2012-02-10 10:53:11 +01:00
/* Copyright ( C ) 2006 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2011-10-29 23:52:05 +02:00
* Copyright ( C ) 2011 Juanjo Menent < jmenent @ 2 byte . es >
2006-08-20 21:15:47 +02:00
*
2011-10-15 20:45:16 +02:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2006-08-20 21:15:47 +02:00
/**
2009-03-09 13:38:22 +01:00
* \file htdocs / admin / tools / export . php
2009-09-29 19:14:52 +02:00
* \brief Page to export a database into a dump file
2009-03-09 13:38:22 +01:00
*/
2006-08-20 21:15:47 +02:00
2010-03-01 10:20:41 +01:00
require ( " ../../main.inc.php " );
2011-10-24 11:25:54 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /core/lib/admin.lib.php " );
2011-10-24 12:59:44 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /core/lib/files.lib.php " );
2010-05-03 10:43:33 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /core/class/html.formfile.class.php " );
2006-08-20 21:15:47 +02:00
$langs -> load ( " admin " );
2012-02-10 11:09:34 +01:00
$action = GETPOST ( 'action' );
2012-02-10 10:53:11 +01:00
$what = GETPOST ( " what " );
$export_type = GETPOST ( " export_type " );
$file = GETPOST ( 'filename_template' );
2006-08-20 21:15:47 +02:00
2012-02-10 10:53:11 +01:00
$sortfield = GETPOST ( " sortfield " );
$sortorder = GETPOST ( " sortorder " );
$page = GETPOST ( " page " );
if ( ! $sortorder ) $sortorder = " DESC " ;
if ( ! $sortfield ) $sortfield = " date " ;
if ( $page < 0 ) { $page = 0 ; }
$limit = $conf -> liste_limit ;
$offset = $limit * $page ;
if ( ! $user -> admin ) accessforbidden ();
2006-08-20 21:15:47 +02:00
2009-03-09 13:38:22 +01:00
if ( $file && ! $what )
2006-08-20 21:15:47 +02:00
{
2011-10-14 16:02:18 +02:00
//print DOL_URL_ROOT.'/dolibarr_export.php';
header ( " Location: " . DOL_URL_ROOT . '/admin/tools/dolibarr_export.php?msg=' . urlencode ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentities ( " ExportMethod " ))));
exit ;
2006-08-20 21:15:47 +02:00
}
2008-08-03 13:34:47 +02:00
/*
2012-02-10 10:53:11 +01:00
* Actions
*/
2008-08-03 13:34:47 +02:00
2012-02-10 11:09:34 +01:00
if ( $action == 'delete' )
{
dol_delete_file ( $conf -> admin -> dir_output . '/backup/' . GETPOST ( 'urlfile' ), 1 );
$action = '' ;
}
2006-08-24 20:51:59 +02:00
2012-02-10 10:53:11 +01:00
/*
* View
*/
2006-08-24 20:51:59 +02:00
2012-02-10 10:53:11 +01:00
// Increase limit of time. Works only if we are not in safe mode
2011-03-05 02:53:20 +01:00
$ExecTimeLimit = 600 ;
2012-02-10 10:53:11 +01:00
if ( ! empty ( $ExecTimeLimit ))
{
2011-03-05 02:53:20 +01:00
$err = error_reporting ();
error_reporting ( 0 ); // Disable all errors
//error_reporting(E_ALL);
@ set_time_limit ( $ExecTimeLimit ); // Need more than 240 on Windows 7/64
error_reporting ( $err );
2010-08-21 22:16:52 +02:00
}
2012-02-10 10:53:11 +01:00
if ( ! empty ( $MemoryLimit ))
{
2011-03-05 02:53:20 +01:00
@ ini_set ( 'memory_limit' , $MemoryLimit );
2006-08-20 21:15:47 +02:00
}
2012-02-10 10:53:11 +01:00
$form = new Form ( $db );
$formfile = new FormFile ( $db );
$help_url = 'EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad' ;
llxHeader ( '' , '' , $help_url );
print_fiche_titre ( $langs -> trans ( " Backup " ), '' , 'setup' );
2006-08-20 21:15:47 +02:00
// Start with empty buffer
$dump_buffer = '' ;
$dump_buffer_len = 0 ;
2012-02-10 10:53:11 +01:00
// We will send fake headers to avoid browser timeout when buffering
2006-08-20 21:15:47 +02:00
$time_start = time ();
2009-10-26 04:10:12 +01:00
// MYSQL
2006-08-20 21:15:47 +02:00
if ( $what == 'mysql' )
{
2012-03-27 18:36:32 +02:00
$cmddump = GETPOST ( " mysqldump " ); // Do not sanitize here with 'alpha', will be sanitize later by escapeshellarg
2011-10-14 16:02:18 +02:00
if ( $cmddump )
{
dolibarr_set_const ( $db , 'SYSTEMTOOLS_MYSQLDUMP' , $cmddump , 'chaine' , 0 , '' , $conf -> entity );
}
$outputdir = $conf -> admin -> dir_output . '/backup' ;
$outputfile = $outputdir . '/' . $file ;
// for compression format, we add extension
2012-03-27 18:33:34 +02:00
$compression = GETPOST ( 'compression' ) ? GETPOST ( 'compression' , 'alpha' ) : 'none' ;
2011-10-14 16:02:18 +02:00
if ( $compression == 'gz' ) $outputfile .= '.gz' ;
if ( $compression == 'bz' ) $outputfile .= '.bz2' ;
$outputerror = $outputfile . '.err' ;
dol_mkdir ( $conf -> admin -> dir_output . '/backup' );
// Parameteres execution
$command = $cmddump ;
if ( preg_match ( " / \ s/ " , $command )) $command = escapeshellarg ( $command ); // Use quotes on command
//$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
$param = $dolibarr_main_db_name . " -h " . $dolibarr_main_db_host ;
$param .= " -u " . $dolibarr_main_db_user ;
if ( ! empty ( $dolibarr_main_db_port )) $param .= " -P " . $dolibarr_main_db_port ;
2012-03-27 18:33:34 +02:00
if ( ! GETPOST ( " use_transaction " )) $param .= " -l --single-transaction " ;
if ( GETPOST ( " disable_fk " )) $param .= " -K " ;
if ( GETPOST ( " sql_compat " ) && GETPOST ( " sql_compat " ) != 'NONE' ) $param .= " --compatible= " . GETPOST ( " sql_compat " , " alpha " );
if ( GETPOST ( " drop_database " )) $param .= " --add-drop-database " ;
if ( GETPOST ( " sql_structure " ))
2011-10-14 16:02:18 +02:00
{
2012-03-27 18:33:34 +02:00
if ( GETPOST ( " drop " )) $param .= " --add-drop-table " ;
2011-10-14 16:02:18 +02:00
}
else
{
$param .= " -t " ;
}
2012-03-27 18:33:34 +02:00
if ( GETPOST ( " disable-add-locks " )) $param .= " --add-locks=FALSE " ;
if ( GETPOST ( " sql_data " ))
2011-10-14 16:02:18 +02:00
{
$param .= " --tables " ;
2012-03-27 18:33:34 +02:00
if ( GETPOST ( " showcolumns " )) $param .= " -c " ;
if ( GETPOST ( " extended_ins " )) $param .= " -e " ;
2012-02-10 11:50:41 +01:00
else $param .= " --skip-extended-insert " ;
2012-03-27 18:33:34 +02:00
if ( GETPOST ( " delayed " )) $param .= " --delayed-insert " ;
if ( GETPOST ( " sql_ignore " )) $param .= " --insert-ignore " ;
if ( GETPOST ( " hexforbinary " )) $param .= " --hex-blob " ;
2011-10-14 16:02:18 +02:00
}
else
{
2012-02-10 10:53:11 +01:00
$param .= " -d " ; // No row information (no data)
2011-10-14 16:02:18 +02:00
}
2012-02-10 10:53:11 +01:00
$param .= " --default-character-set=utf8 " ; // We always save output into utf8 charset
2011-10-14 16:02:18 +02:00
$paramcrypted = $param ;
$paramclear = $param ;
if ( ! empty ( $dolibarr_main_db_pass ))
{
2011-12-21 19:56:56 +01:00
$paramcrypted .= ' -p"' . preg_replace ( '/./i' , '*' , $dolibarr_main_db_pass ) . '"' ;
$paramclear .= ' -p"' . str_replace ( '"' , '\"' , $dolibarr_main_db_pass ) . '"' ;
2011-10-14 16:02:18 +02:00
}
print '<b>' . $langs -> trans ( " RunCommandSummary " ) . ':</b><br>' . " \n " ;
print '<textarea rows="' . ROWS_2 . '" cols="120">' . $command . " " . $paramcrypted . '</textarea><br>' . " \n " ;
print '<br>' ;
2011-12-21 19:56:56 +01:00
//print $paramclear;
2011-10-14 16:02:18 +02:00
// Now run command and show result
print '<b>' . $langs -> trans ( " BackupResult " ) . ':</b> ' ;
$errormsg = '' ;
$result = dol_mkdir ( $outputdir );
// Debut appel methode execution
$fullcommandcrypted = $command . " " . $paramcrypted . " 2>&1 " ;
$fullcommandclear = $command . " " . $paramclear . " 2>&1 " ;
if ( $compression == 'none' ) $handle = fopen ( $outputfile , 'w' );
if ( $compression == 'gz' ) $handle = gzopen ( $outputfile , 'w' );
if ( $compression == 'bz' ) $handle = bzopen ( $outputfile , 'w' );
if ( $handle )
{
$ok = 0 ;
dol_syslog ( " Run command " . $fullcommandcrypted );
$handlein = popen ( $fullcommandclear , 'r' );
while ( ! feof ( $handlein ))
{
$read = fgets ( $handlein );
fwrite ( $handle , $read );
2011-10-20 00:04:57 +02:00
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 ;
2011-10-14 16:02:18 +02:00
}
pclose ( $handlein );
if ( $compression == 'none' ) fclose ( $handle );
if ( $compression == 'gz' ) gzclose ( $handle );
if ( $compression == 'bz' ) bzclose ( $handle );
if ( ! empty ( $conf -> global -> MAIN_UMASK ))
@ chmod ( $outputfile , octdec ( $conf -> global -> MAIN_UMASK ));
}
else
{
$langs -> load ( " errors " );
dol_syslog ( " Failed to open file " . $outputfile , LOG_ERR );
$errormsg = $langs -> trans ( " ErrorFailedToWriteInDir " );
}
2011-10-15 20:45:16 +02:00
2011-10-14 16:02:18 +02:00
// Get errorstring
if ( $compression == 'none' ) $handle = fopen ( $outputfile , 'r' );
if ( $compression == 'gz' ) $handle = gzopen ( $outputfile , 'r' );
if ( $compression == 'bz' ) $handle = bzopen ( $outputfile , 'r' );
if ( $handle )
{
// Get 2048 first chars of error message.
$errormsg = fgets ( $handle , 2048 );
// Close file
if ( $compression == 'none' ) fclose ( $handle );
if ( $compression == 'gz' ) gzclose ( $handle );
if ( $compression == 'bz' ) bzclose ( $handle );
if ( $ok && preg_match ( '/^-- MySql/i' , $errormsg )) $errormsg = '' ; // Pas erreur
else
{
// Renommer fichier sortie en fichier erreur
//print "$outputfile -> $outputerror";
@ dol_delete_file ( $outputerror , 1 );
@ rename ( $outputfile , $outputerror );
// Si safe_mode on et command hors du parametre exec, on a un fichier out vide donc errormsg vide
2011-12-21 19:34:16 +01:00
if ( ! $errormsg )
2011-10-29 23:52:05 +02:00
{
$langs -> load ( " errors " );
$errormsg = $langs -> trans ( " ErrorFailedToRunExternalCommand " );
}
2011-10-14 16:02:18 +02:00
}
}
// Fin execution commande
}
if ( $what == 'mysqlnobin' )
{
$outputdir = $conf -> admin -> dir_output . '/backup' ;
$outputfile = $outputdir . '/' . $file ;
$outputfiletemp = $outputfile . '-TMP.sql' ;
// for compression format, we add extension
2012-03-27 18:33:34 +02:00
$compression = GETPOST ( 'compression' ) ? GETPOST ( 'compression' , 'alpha' ) : 'none' ;
2011-10-14 16:02:18 +02:00
if ( $compression == 'gz' ) $outputfile .= '.gz' ;
if ( $compression == 'bz' ) $outputfile .= '.bz2' ;
$outputerror = $outputfile . '.err' ;
dol_mkdir ( $conf -> admin -> dir_output . '/backup' );
if ( $compression == 'gz' or $compression == 'bz' )
{
backup_tables ( $outputfiletemp );
dol_compress_file ( $outputfiletemp , $outputfile , $compression );
unlink ( $outputfiletemp );
}
else
{
backup_tables ( $outputfile );
}
2009-10-26 04:10:12 +01:00
}
// POSTGRESQL
if ( $what == 'postgresql' )
{
2012-03-27 18:36:32 +02:00
$cmddump = GETPOST ( " postgresqldump " ); // Do not sanitize here with 'alpha', will be sanitize later by escapeshellarg
2011-10-14 16:02:18 +02:00
if ( $cmddump )
{
dolibarr_set_const ( $db , 'SYSTEMTOOLS_POSTGRESQLDUMP' , $cmddump , 'chaine' , 0 , '' , $conf -> entity );
}
$outputdir = $conf -> admin -> dir_output . '/backup' ;
$outputfile = $outputdir . '/' . $file ;
// for compression format, we add extension
2012-03-27 18:33:34 +02:00
$compression = GETPOST ( 'compression' ) ? GETPOST ( 'compression' , 'alpha' ) : 'none' ;
2011-10-14 16:02:18 +02:00
if ( $compression == 'gz' ) $outputfile .= '.gz' ;
if ( $compression == 'bz' ) $outputfile .= '.bz2' ;
$outputerror = $outputfile . '.err' ;
dol_mkdir ( $conf -> admin -> dir_output . '/backup' );
// Parameteres execution
$command = $cmddump ;
if ( preg_match ( " / \ s/ " , $command )) $command = $command = escapeshellarg ( $command ); // Use quotes on command
//$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
$param = " --no-tablespaces --inserts -h " . $dolibarr_main_db_host ;
$param .= " -U " . $dolibarr_main_db_user ;
if ( ! empty ( $dolibarr_main_db_port )) $param .= " -p " . $dolibarr_main_db_port ;
2012-03-27 18:33:34 +02:00
if ( GETPOST ( " sql_compat " ) && GETPOST ( " sql_compat " ) == 'ANSI' ) $param .= " --disable-dollar-quoting " ;
if ( GETPOST ( " drop_database " )) $param .= " -c -C " ;
if ( GETPOST ( " sql_structure " ))
2011-10-14 16:02:18 +02:00
{
2012-03-27 18:33:34 +02:00
if ( GETPOST ( " drop " )) $param .= " --add-drop-table " ;
if ( ! GETPOST ( " sql_data " )) $param .= " -s " ;
2011-10-14 16:02:18 +02:00
}
2012-03-27 18:36:32 +02:00
if ( GETPOST ( " sql_data " ))
2011-10-14 16:02:18 +02:00
{
2012-03-27 18:33:34 +02:00
if ( ! GETPOST ( " sql_structure " )) $param .= " -a " ;
if ( GETPOST ( " showcolumns " )) $param .= " -c " ;
2011-10-14 16:02:18 +02:00
}
$param .= ' -f "' . $outputfile . '"' ;
//if ($compression == 'none')
if ( $compression == 'gz' ) $param .= ' -Z 9' ;
//if ($compression == 'bz')
$paramcrypted = $param ;
$paramclear = $param ;
/* if ( ! empty ( $dolibarr_main_db_pass ))
{
2011-10-15 20:45:16 +02:00
$paramcrypted .= " -W " . preg_replace ( '/./i' , '*' , $dolibarr_main_db_pass );
$paramclear .= " -W " . $dolibarr_main_db_pass ;
} */
2011-10-14 16:02:18 +02:00
$paramcrypted .= " -w " . $dolibarr_main_db_name ;
$paramclear .= " -w " . $dolibarr_main_db_name ;
print $langs -> trans ( " RunCommandSummaryToLaunch " ) . ':<br>' . " \n " ;
print '<textarea rows="' . ROWS_3 . '" cols="120">' . $command . " " . $paramcrypted . '</textarea><br>' . " \n " ;
print '<br>' ;
// Now show to ask to run command
print $langs -> trans ( " YouMustRunCommandFromCommandLineAfterLoginToUser " , $dolibarr_main_db_user , $dolibarr_main_db_user );
print '<br>' ;
print '<br>' ;
$what = '' ;
2006-08-20 21:15:47 +02:00
}
2009-10-26 04:10:12 +01:00
2006-08-20 21:15:47 +02:00
// Si on a demande une generation
if ( $what )
{
2011-10-14 16:02:18 +02:00
if ( $errormsg )
{
print '<div class="error">' . $langs -> trans ( " Error " ) . " : " . $errormsg . '</div>' ;
// print '<a href="'.DOL_URL_ROOT.$relativepatherr.'">'.$langs->trans("DownloadErrorFile").'</a><br>';
print '<br>' ;
print '<br>' ;
}
else
{
print '<div class="ok">' ;
print $langs -> trans ( " BackupFileSuccessfullyCreated " ) . '.<br>' ;
print $langs -> trans ( " YouCanDownloadBackupFile " );
print '</div>' ;
print '<br>' ;
}
2006-08-20 21:15:47 +02:00
}
2012-02-10 10:53:11 +01:00
$filearray = dol_dir_list ( $conf -> admin -> dir_output . '/backup' , 'files' , 0 , '' , '' , $sortfield ,( strtolower ( $sortorder ) == 'asc' ? SORT_ASC : SORT_DESC ), 1 );
2012-02-10 11:09:34 +01:00
$result = $formfile -> list_of_documents ( $filearray , null , 'systemtools' , '' , 1 , 'backup/' , 1 , 0 ,( $langs -> trans ( " NoBackupFileAvailable " ) . '<br>' . $langs -> trans ( " ToBuildBackupFileClickHere " , DOL_URL_ROOT . '/admin/tools/dolibarr_export.php' )), 0 , $langs -> trans ( " PreviousDumpFiles " ));
2009-03-09 13:38:22 +01:00
2006-08-20 21:15:47 +02:00
print '<br>' ;
$time_end = time ();
2011-08-27 16:24:16 +02:00
llxFooter ();
2011-10-14 16:02:18 +02:00
$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
*
* @ 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
$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 ;
}
2011-10-15 20:45:16 +02:00
// Print headers and global mysql config vars
$sqlhead = '' ;
2011-12-28 01:28:16 +01:00
$sqlhead .= " -- " . getStaticMember ( $db , 'label' ) . " dump via php
2011-10-15 20:45:16 +02:00
--
-- 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 */ ;
" ;
fwrite ( $handle , $sqlhead );
// Process each table and print their definition + their datas
2011-10-14 16:02:18 +02:00
foreach ( $tables as $table )
{
2011-10-15 20:45:16 +02:00
// Saving the table structure
fwrite ( $handle , " -- \n -- Table structure for table ` " . $table . " ` \n -- \n \n " );
2011-10-14 16:02:18 +02:00
2011-10-15 20:45:16 +02:00
fwrite ( $handle , " DROP TABLE IF EXISTS ` " . $table . " `; \n " );
fwrite ( $handle , " /*!40101 SET @saved_cs_client = @@character_set_client */; \n " );
fwrite ( $handle , " /*!40101 SET character_set_client = utf8 */; \n " );
2011-10-14 16:02:18 +02:00
$resqldrop = $db -> query ( 'SHOW CREATE TABLE ' . $table );
$row2 = $db -> fetch_row ( $resqldrop );
2011-10-15 20:45:16 +02:00
fwrite ( $handle , $row2 [ 1 ] . " ; \n " );
fwrite ( $handle , " /*!40101 SET character_set_client = @saved_cs_client */; \n \n " );
2011-10-14 16:02:18 +02:00
2011-10-15 20:45:16 +02:00
// Dumping the data (locking the table and disabling the keys check while doing the process)
fwrite ( $handle , " -- \n -- Dumping data for table ` " . $table . " ` \n -- \n \n " );
fwrite ( $handle , " LOCK TABLES ` " . $table . " ` WRITE; \n " );
fwrite ( $handle , " /*!40000 ALTER TABLE ` " . $table . " ` DISABLE KEYS */; \n " );
$sql = 'SELECT * FROM ' . $table ;
$result = $db -> query ( $sql );
$num_fields = $db -> num_rows ( $result );
while ( $row = $db -> fetch_row ( $result )) {
// For each row of data we print a line of INSERT
fwrite ( $handle , 'INSERT INTO `' . $table . '` VALUES (' );
2011-10-14 16:02:18 +02:00
$columns = count ( $row );
2011-10-15 20:45:16 +02:00
$rowsarr = array ();
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 and ! is_string ( $row [ $j ])) {
// IMPORTANT: if the field is NULL we set it NULL
$row [ $j ] = 'NULL' ;
} elseif ( is_string ( $row [ $j ]) and $row [ $j ] == '' ) {
// if it's an empty string, we set it as an empty string
$row [ $j ] = " '' " ;
} elseif ( is_numeric ( $row [ $j ])) {
// 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 ] . " ' " ;
2011-10-14 16:02:18 +02:00
}
}
2011-10-15 20:45:16 +02:00
fwrite ( $handle , implode ( ',' , $row ) . " ); \n " );
2011-10-14 16:02:18 +02:00
}
2011-10-15 20:45:16 +02:00
fwrite ( $handle , " /*!40000 ALTER TABLE ` " . $table . " ` ENABLE KEYS */; \n " ); // Enabling back the keys/index checking
fwrite ( $handle , " UNLOCK TABLES; \n " ); // Unlocking the tables
2011-10-14 16:02:18 +02:00
fwrite ( $handle , " \n \n \n " );
}
/* Backup Procedure structure*/
/*
2011-10-15 20:45:16 +02:00
$result = $db -> query ( 'SHOW PROCEDURE STATUS' );
2011-10-14 16:02:18 +02:00
if ( $db -> num_rows ( $result ) > 0 )
{
2011-10-15 20:45:16 +02:00
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 " );
}
2011-10-14 16:02:18 +02:00
}
*/
/* Backup Procedure structure*/
2011-10-15 20:45:16 +02:00
// Write the footer (restore the previous database settings)
$sqlfooter = '' ;
$sqlfooter .= "
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */ ;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */ ;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */ ;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */ ;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */ ;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */ ;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */ ;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */ ;
-- Dump completed on " .date('Y-m-d G-i-s');
fwrite ( $handle , $sqlfooter );
2011-10-14 16:02:18 +02:00
fclose ( $handle );
return 1 ;
}
2006-08-20 21:15:47 +02:00
?>