<?php
/*
* ***************************************************************************************************
*
* File name: dumptable.php
*
* Copyright © 2017 Alessandro Quintiliani
*
* This file is part of MultiDump package.
*
* MultiDump 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 3 of the License, or
* (at your option) any later version.
*
* MultiDump 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 MultiDump package. If not, see <http://www.gnu.org/licenses/>.
*
* ***************************************************************************************************
*/
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT);
include_once ("Class.MultiDump.php");
$odtbl = new MultiDump ();
$dbname = $_POST [$odtbl->getDbVarName ()];
$table = $_POST [$odtbl->getTableVarName ()];
$varsDbConn = unserialize ( $_POST [$odtbl->getReferenceDbConnName ()] );
$flag_forced_dump_table = $_POST [$odtbl->getReferenceForcedDumpTableName ()];
$odtbl->setLog ( "log", "log_dump_db_" . $dbname . "__table_" . $table . ".txt", 1 );
$odtbl->wlog ( "start procedure dump table $dbname.$table" );
if(! $dbname || ! $table || ! $varsDbConn){
echo "the database name and the table name and the array with the database connection parameters must not be null<br>" ;
exit;
}
$timeoutLimit = $odtbl->getTimeoutLimit();
echo "-------------------<br>";
$odtbl->setTypeDbToDump ( $dbname, $varsDbConn ['typedb'] );
$odtbl->setHostDbToDump ( $dbname, $varsDbConn ['hostdb'] );
$odtbl->setPortDbToDump ( $dbname, $varsDbConn ['portdb'] );
$odtbl->setLoginDbToDump ( $dbname, $varsDbConn ['logindb'] );
$odtbl->setPasswordDbToDump ( $dbname, $varsDbConn ['passworddb'] );
$odtbl->setTableName ( $table );
$typeDbToDump = $odtbl->getTypeDbToDump ( $dbname );
$odtbl->setDumpParameters ();
$pobjref = $odtbl->getDumpParamsRef ();
$FOLDER_DUMP_FILES = $pobjref->getParamValue('folder_dump_files');
$pdort = new PDOconn($varsDbConn ['typedb']);
$pdort->setHost($varsDbConn ['hostdb']);
$pdort->setPort($varsDbConn ['portdb']);
$pdort->setUser($varsDbConn ['logindb']);
$pdort->setPassword($varsDbConn ['passworddb']);
$pdort->setPDOconn();
$odtbl->setPDOref($pdort);
$phpFileDownload = $odtbl->getDownloadDumpPage();
$postVarDownloadFile = $odtbl->getPostVarDumpFile();
$postVarTypeCompression = $odtbl->getPostVarCompressionType();
$action = "checksum";
$odtbl->setCtrlVar('action');
$odtbl->wlog("start block calculation checksum table $dbname.$table");
if($flag_forced_dump_table == 0){
$odtbl->wlog("dump table $dbname.$table only on changing checksum table");
$checksum = $odtbl->getCheckSum ( $dbname, $table );
$odtbl->wlog("checksum table $dbname.$table calculated: $checksum");
}
else
{
$odtbl->wlog("dump table $dbname.$table required at each run");
$fileDumpExistent = $odtbl->getFileDumpTable($dbname, $table);
if(! $fileDumpExistent ){
$odtbl->wlog("no file dump table $dbname.$table found --> first dump of the table $dbname.$table");
$checksum = $odtbl->getCheckSum ( $dbname, $table );
$odtbl->wlog("checksum calculated on first dump table $dbname.$table: $checksum");
}
else
{
// dump file < dbname > _ < table > _*.sql exists: since the dump is required at each run, the calculation of
// the checksum is unnecessary; so the checksum is taken from the dump file name
$odtbl->wlog("file dump table $dbname.$table exists with name $fileDumpExistent");
$odtbl->wlog("the dump is required at each run: a new dump is executed using the checksum found in the name of the existent file");
$fileDumpExistentNoParentDir = str_replace($FOLDER_DUMP_FILES . DIRECTORY_SEPARATOR, "", $fileDumpExistent);
$remainingDumpFile = str_replace($dbname."_".$table."_", "", $fileDumpExistentNoParentDir);
list($checksum,$fileExtension) = explode(".", $remainingDumpFile,2);
$odtbl->wlog("checksum extracted from filename $fileDumpExistent: $checksum");
}
}
$odtbl->wlog("end block calculation checksum table $dbname.$table");
### customized output file with the table dump according to database type (MySQL, Oracle, etc..)
$parameter_output_filename_dump = $odtbl->getDumpOutputFile();
$list_replacements = array ($dbname,$table,$checksum );
$PREFIX_OLD_DUMP_FILES = $odtbl->getPrefixOldDumpFile();
$dump_file_name = $pobjref->parseParamPlaceHoldersString ( $parameter_output_filename_dump, $list_replacements );
$FOLDER_DUMP_FILES = $odtbl->getFolderDumpFiles ();
mkdir ( $FOLDER_DUMP_FILES, 0755 );
$pathToDumpFileName = $FOLDER_DUMP_FILES . DIRECTORY_SEPARATOR . $dump_file_name;
$urlToPathDumpFileNameZIP = $phpFileDownload . "?" . $postVarDownloadFile . "=" . urlencode($pathToDumpFileName) . "&" . $postVarTypeCompression . "=zip";
$urlToPathDumpFileNameGZIP= $phpFileDownload . "?" . $postVarDownloadFile . "=" . urlencode($pathToDumpFileName) . "&" . $postVarTypeCompression . "=gzip";
$odtbl->wlog("start block execution dump table $dbname.$table to file $pathToDumpFileName");
$fileDumpExistent = $odtbl->getFileDumpTable($dbname, $table);
$odtbl->wlog("file dump existent: $fileDumpExistent");
$titleLinkZip = ' title="If the file '.$dump_file_name.' is very large, the creation of the zip file may take several minutes before downloading "';
$titleLinkGzip = ' title="If the file '.$dump_file_name.' is very large, the creation of the gzip file may take several minutes before downloading "';
if($flag_forced_dump_table == 1){
// ######################################## DUMP TABLE AT EACH RUN ########################################
$dump_frequency = "(Dump at each run)";
$dumping = "($dbname.$table always dumped)";
$odtbl->setCtrlVar ( 'dumping' );
$odtbl->wlog("table $dbname.$table dumped at each run");
if(! $fileDumpExistent ){
// FIRST DUMP OF THE TABLE
$dumping = "($dbname.$table always dumped -- first dump)";
$odtbl->setCtrlVar ( 'dumping' );
$odtbl->wlog("dump file for the table $dbname.$table not found: start execution first dump for the table $dbname.$table");
$cmddump = $odtbl->getCmdDumpDB ( $dbname, $table, $pathToDumpFileName );
$odtbl->wlog("execution dump command: $cmddump");
set_time_limit($timeoutLimit);
exec ( $cmddump, $resultDump );
echo "$dump_frequency <b>FIRST DUMP: CREATION DUMP FILE TABLE $dump_file_name</b><br/>";
if(preg_match ( '/error/i', $resultDump ) )
{
$odtbl->wlog("ERROR executing $cmddump");
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR ON FIRST DUMP: NO DUMP TABLE $dbname.$table</b></font></div>";
}
else
{
$odtbl->wlog("table $dbname.$table successfully dumped into the file $pathToDumpFileName");
echo "$dump_frequency table $dbname.$table successfully dumped into the file <a href='".$pathToDumpFileName."'><b>$dump_file_name</b></a> (<a href='".$urlToPathDumpFileNameZIP."' $titleLinkZip >zip</a>) (<a href='".$urlToPathDumpFileNameGZIP."' $titleLinkGzip>gzip</a>)";
}
$odtbl->wlog("end execution first dump for the table $dbname.$table");
}
else
{
$dumping = "($dbname.$table always dumped -- not first dump)";
$odtbl->setCtrlVar ( 'dumping' );
// THE TABLE $table HAS BEEN STILL DUMPED ON A PREVIOUS RUN
// NEW DUMP OF $table FROM $dbname DATABASE IS REQUIRED BECAUSE OF THE METHOD CALL setListForcedDumpTables
$odtbl->wlog("dump file for the table $dbname.$table found with name $fileDumpExistent");
$odtbl->wlog("start execution new dump for the table $dbname.$table because the dump is required at each run");
$typeDbToDump = $odtbl->getTypeDbToDump ( $dbname );
$odtbl->setDumpParameters ();
$pobjref = $odtbl->getDumpParamsRef ();
$PREFIX_OLD_DUMP_FILES = $odtbl->getPrefixOldDumpFile();
$partCurrentDumpTableFileName = pathinfo ( $fileDumpExistent );
$dirCurrentDumpTableFileName = $partCurrentDumpTableFileName ['dirname'];
$currentDumpTableFileName = $partCurrentDumpTableFileName ['basename'];
$backupCurrentDumpTableFileName = $dirCurrentDumpTableFileName . DIRECTORY_SEPARATOR . $PREFIX_OLD_DUMP_FILES . $currentDumpTableFileName;
$odtbl->wlog("rename file $currentDumpTableFileName to $PREFIX_OLD_DUMP_FILES$currentDumpTableFileName as backup");
// BLOCK RENAMING CURRENT SQL FILE PREPRENDING SUFFIX TO BECOME A BACKUP FILE BEFORE DUMPING
if(false === rename ( $fileDumpExistent, $backupCurrentDumpTableFileName ) )
{
$odtbl->wlog("ERROR renaming file $fileDumpExistent to $backupCurrentDumpTableFileName as backup: NO DUMP TABLE $dbname.$table");
// ERROR BACKUP (RENAME) CURRENT DUMP FILE
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR renaming $fileDumpExistent to $backupCurrentDumpTableFileName: NO DUMP TABLE $dbname.$table</b></font></div>";
}
else
{
// CURRENT DUMP FILE SUCCESSFULLY BACKUPPED (RENAMED)
$odtbl->wlog("file $fileDumpExistent successfully renamed to $backupCurrentDumpTableFileName as backup: new dump of $dbname.$table");
$cmddump = $odtbl->getCmdDumpDB ( $dbname, $table, $pathToDumpFileName );
$odtbl->wlog("execution dump command: $cmddump");
set_time_limit($timeoutLimit);
exec ( $cmddump, $resultDump );
echo "$dump_frequency <b>NEW DUMP: CREATION DUMP FILE TABLE $dump_file_name</b><br>";
if(preg_match ( '/error/i', $resultDump ) )
{
// ERROR NEW DUMP TABLE
$odtbl->wlog("ERROR NEW DUMP TABLE $dbname.$table TO $pathToDumpFileName: restoring backup $backupCurrentDumpTableFileName");
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR execution command dump table $dbname.$table: NO DUMP TABLE $dbname.$table</b>";
echo "$dump_frequency <b>restore backup file $backupCurrentDumpTableFileName</b></font></div>";
// RESTORING BACKUP FILE
$pathToDumpCurrentFileName = $odtbl->getFileDumpTable($dbname, $table);
$partCurrentDumpTableFileName = pathinfo ( $pathToDumpCurrentFileName );
$dirCurrentDumpTableFileName = $partCurrentDumpTableFileName ['dirname'];
$currentDumpTableFileName = $partCurrentDumpTableFileName ['basename'];
$backupCurrentDumpTableFileName = $dirCurrentDumpTableFileName . DIRECTORY_SEPARATOR . $PREFIX_OLD_DUMP_FILES . $currentDumpTableFileName;
$restoredDumpTableFileName = $dirCurrentDumpTableFileName . DIRECTORY_SEPARATOR . $currentDumpTableFileName;
$FOLDER_DUMP_FILES = $pobjref->getParamValue('folder_dump_files');
$odtbl->wlog("start restoring file $backupCurrentDumpTableFileName to $restoredDumpTableFileName");
if(false === rename ( $backupCurrentDumpTableFileName, $restoredDumpTableFileName ) )
{
// ERROR RESTORING BACKUP FILE
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR restoring file $backupCurrentDumpTableFileName in $restoredDumpTableFileName: restore file $backupCurrentDumpTableFileName manually removing prefix $PREFIX_OLD_DUMP_FILES</b></font></div>";
$odtbl->wlog("ERROR RESTORING FILE $backupCurrentDumpTableFileName TO $restoredDumpTableFileName: RESTORE FILE MANUALLY REMOVING PREFIX $PREFIX_OLD_DUMP_FILES");
echo "$dump_frequency file $backupCurrentDumpTableFileName successfully restored (renamed) in $restoredDumpTableFileName";
$odtbl->wlog("Backup file $backupCurrentDumpTableFileName successfully restored to $restoredDumpTableFileName");
}
}
else
{
// DUMP SUCCESSFUL
$odtbl->wlog("execution dump command successful: removing backup file $backupCurrentDumpTableFileName");
echo "$dump_frequency table $dbname.$table successfully dumped into the file <a href='".$pathToDumpFileName."'><b>$dump_file_name</b></a> (<a href='".$urlToPathDumpFileNameZIP."' $titleLinkZip >zip</a>) (<a href='".$urlToPathDumpFileNameGZIP."' $titleLinkGzip >gzip</a>)";
// REMOVAL BACKUP FILE
if(false === unlink ( $backupCurrentDumpTableFileName ))
{
// ERROR REMOVING BACKUP FILE
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR removing backup file $backupCurrentDumpTableFileName: remove file manually</b></font></div>";
$odtbl->wlog("ERROR REMOVING BACKUP FILE $backupCurrentDumpTableFileName: REMOVE FILE MANUALLY");
}
else
{
$odtbl->wlog("backup file $backupCurrentDumpTableFileName successfully removed");
}
}
} // if ( false === rename($pathToDumpCurrentFileName, $backupCurrentDumpTableFileName) )
$odtbl->wlog("end execution new dump for the table $dbname.$table because dump is required at each run");
}// if (!count($pathToFileDumpFound))
}
else
{
// ######################################## DUMP TABLE ONLY ON CHANGING OF CONTENT / STRUCTURE ########################################
$dump_frequency = "(Dump on change)";
$dumping = "($dbname.$table dumped on change checksum)";
$odtbl->setCtrlVar ( 'dumping' );
$odtbl->wlog("table $dbname.$table dumped only on change checksum");
if(! $fileDumpExistent ){
// FIRST DUMP OF THE TABLE
$dumping = "($dbname.$table dumped on change checksum -- first dump)";
$odtbl->setCtrlVar ( 'dumping' );
$odtbl->wlog("dump file for the table $dbname.$table not found: start execution first dump for the table $dbname.$table");
if($flag_forced_dump_table == 0)
{
$dump_frequency = "(Dump on change)";
}
else
{
$dump_frequency = "(Dump at each run)";
}
$cmddump = $odtbl->getCmdDumpDB ( $dbname, $table, $pathToDumpFileName );
$odtbl->wlog("execution dump command: $cmddump");
set_time_limit($timeoutLimit);
exec ( $cmddump, $resultDump );
echo "$dump_frequency <b>FIRST DUMP: CREATION DUMP FILE TABLE $dump_file_name</b><br/>";
if(preg_match ( '/error/i', $resultDump ) )
{
$odtbl->wlog("ERROR executing $cmddump");
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR IN FIRST DUMP: NO DUMP TABLE $dbname.$table</b></font></div>";
}
else
{
$odtbl->wlog("table $dbname.$table successfully dumped into the file $pathToDumpFileName");
echo "$dump_frequency table $dbname.$table successfully dumped into the file <a href='".$pathToDumpFileName."'><b>$dump_file_name</b></a> (<a href='".$urlToPathDumpFileNameZIP."' $titleLinkZip >zip</a>) (<a href='".$urlToPathDumpFileNameGZIP."' $titleLinkGzip >gzip</a>)";
}
$odtbl->wlog("end execution first dump for the table $dbname.$table");
}
else
{
// FILE WITH THE DUMP OF THE CURRENT TABLE EXISTS: CHECK IF IT HAS AN OLD CHECKSUM OR NOT
$dumping = "($dbname.$table dumped on change checksum -- not first dump)";
$odtbl->setCtrlVar ( 'dumping' );
$odtbl->wlog("table $dbname.$table dumped on a previous run: check if it has an old checksum detecting if file $pathToDumpFileName exists or not");
if(file_exists ( $pathToDumpFileName )){
// TABLE DUMP FILE EXISTS AND NO FORCED DUMP: NO NEW DUMP
echo "(Dump on change) no new dump table <b>$dbname.$table</b> required: the file <a href='".$pathToDumpFileName."'><b>$dump_file_name</b></a> has not changed since last dump  (<a href='".$urlToPathDumpFileNameZIP."' $titleLinkZip >zip</a>) (<a href='".$urlToPathDumpFileNameGZIP."' $titleLinkGzip >gzip</a>)";
$odtbl->wlog("file $pathToDumpFileName exists and the checksum of the table $dbname.$table has not changed since last dump");
$odtbl->wlog("no new dump of the table $dbname.$table is required");
}
else
{
// TABLE DUMP FILE NOT EXISTS: NEW DUMP REQUIRED
$odtbl->wlog("file $pathToDumpFileName does not exist: this means that the checksum of the table $dbname.$table has changed since last dump");
$odtbl->wlog("start execution new dump for the table $dbname.$table due to a change of checksum");
$typeDbToDump = $odtbl->getTypeDbToDump ( $dbname );
$odtbl->setDumpParameters ();
$pobjref = $odtbl->getDumpParamsRef ();
$PREFIX_OLD_DUMP_FILES = $odtbl->getPrefixOldDumpFile();
$partCurrentDumpTableFileName = pathinfo ( $fileDumpExistent );
$dirCurrentDumpTableFileName = $partCurrentDumpTableFileName ['dirname'];
$currentDumpTableFileName = $partCurrentDumpTableFileName ['basename'];
$backupCurrentDumpTableFileName = $dirCurrentDumpTableFileName . DIRECTORY_SEPARATOR . $PREFIX_OLD_DUMP_FILES . $currentDumpTableFileName;
$odtbl->wlog("rename file $currentDumpTableFileName to $PREFIX_OLD_DUMP_FILES$currentDumpTableFileName as backup");
$odtbl->wlog("rename file $pathToDumpFileName to $backupCurrentDumpTableFileName as backup");
// BLOCK RENAMING CURRENT SQL FILE PREPRENDING SUFFIX TO BECOME A BACKUP FILE BEFORE DUMPING
if(false === rename ( $fileDumpExistent, $backupCurrentDumpTableFileName ) )
{
$odtbl->wlog("ERROR renaming file $fileDumpExistent to $backupCurrentDumpTableFileName as backup: NO DUMP TABLE $dbname.$table");
// ERROR BACKUP (RENAME) CURRENT DUMP FILE
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR renaming $fileDumpExistent to $backupCurrentDumpTableFileName: NO DUMP TABLE $dbname.$table</b></font></div>";
}
else
{
// CURRENT DUMP FILE SUCCESSFULLY BACKUPPED (RENAMED)
$odtbl->wlog("file $pathToDumpFileName successfully renamed to $backupCurrentDumpTableFileName as backup: new dump of $dbname.$table");
$cmddump = $odtbl->getCmdDumpDB ( $dbname, $table, $pathToDumpFileName );
$odtbl->wlog("execution dump command: $cmddump");
set_time_limit($timeoutLimit);
exec ( $cmddump, $resultDump );
echo "$dump_frequency <b>NEW DUMP: CREATION DUMP FILE TABLE $dump_file_name</b><br>";
if(preg_match ( '/error/i', $resultDump ) )
{
// ERROR NEW DUMP TABLE
$odtbl->wlog("ERROR NEW DUMP TABLE $dbname.$table TO $pathToDumpFileName: restoring backup $backupCurrentDumpTableFileName");
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <font color='FF0000'><b>ERROR execution command dump table $dbname.$table: NO DUMP TABLE $dbname.$table</b>";
echo "$dump_frequency <b>restore backup file $backupCurrentDumpTableFileName</b></font></div>";
// RESTORING BACKUP FILE
$pathToDumpCurrentFileName = $odtbl->getFileDumpTable($dbname, $table);
$partCurrentDumpTableFileName = pathinfo ( $pathToDumpCurrentFileName );
$dirCurrentDumpTableFileName = $partCurrentDumpTableFileName ['dirname'];
$currentDumpTableFileName = $partCurrentDumpTableFileName ['basename'];
$backupCurrentDumpTableFileName = $dirCurrentDumpTableFileName . DIRECTORY_SEPARATOR . $PREFIX_OLD_DUMP_FILES . $currentDumpTableFileName;
$restoredDumpTableFileName = $dirCurrentDumpTableFileName . DIRECTORY_SEPARATOR . $currentDumpTableFileName;
$FOLDER_DUMP_FILES = $pobjref->getParamValue('folder_dump_files');
$odtbl->wlog("start restoring file $backupCurrentDumpTableFileName to $restoredDumpTableFileName");
if(false === rename ( $backupCurrentDumpTableFileName, $restoredDumpTableFileName ) )
{
// ERROR RESTORING BACKUP FILE
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR restoring file $backupCurrentDumpTableFileName in $restoredDumpTableFileName: restore file $backupCurrentDumpTableFileName manually removing prefix $PREFIX_OLD_DUMP_FILES</b></font></div>";
$odtbl->wlog("ERROR RESTORING FILE $backupCurrentDumpTableFileName TO $restoredDumpTableFileName: RESTORE FILE MANUALLY REMOVING PREFIX $PREFIX_OLD_DUMP_FILES");
echo "$dump_frequency file $backupCurrentDumpTableFileName successfully restored (renamed) in $restoredDumpTableFileName";
$odtbl->wlog("Backup file $backupCurrentDumpTableFileName successfully restored to $restoredDumpTableFileName");
}
}
else
{
// DUMP SUCCESSFUL
$odtbl->wlog("execution dump command successful: removing backup file $backupCurrentDumpTableFileName");
echo "$dump_frequency table $dbname.$table successfully dumped into the file <a href='".$pathToDumpFileName."'><b>$dump_file_name</b></a> (<a href='".$urlToPathDumpFileNameZIP."' $titleLinkZip >zip</a>) (<a href='".$urlToPathDumpFileNameGZIP."' $titleLinkGzip >gzip</a>)";
// REMOVAL BACKUP FILE
if(false === unlink ( $backupCurrentDumpTableFileName ))
{
// ERROR REMOVING BACKUP FILE
echo "<div id='div_dump_error_id_".$dbname."__".$table."'><font color='FF0000'>$dump_frequency <b>ERROR removing backup file $backupCurrentDumpTableFileName: remove file manually</b></font></div>";
$odtbl->wlog("ERROR REMOVING BACKUP FILE $backupCurrentDumpTableFileName: REMOVE FILE MANUALLY");
}
else
{
$odtbl->wlog("backup file $backupCurrentDumpTableFileName successfully removed");
}
}
} // if ( false === rename($pathToDumpCurrentFileName, $backupCurrentDumpTableFileName) )
$odtbl->wlog("end execution new dump for the table $dbname.$table due to a change of checksum");
} // if (file_exists($pathToDumpFileName))
}// if (!count($pathToFileDumpFound))
} // if ($flag_forced_dump_table == 1) {
$pdort->closeConn();
unset($pdort);
$odtbl->wlog("end block execution dump to file $pathToDumpFileName");
$odtbl->wlog ( "end procedure dump table $dbname.$table" );
$odtbl->end ();
unset ( $odtbl );
|