PHP Classes

PHP Multi MySQLDump: Dump MySQL database tables for file download

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 59%Total: 564 All time: 5,420 This week: 455Up
Version License PHP version Categories
multidump 1.0.8GNU General Publi...5.1PHP 5, Databases, Systems administration
Description 

Author

This package can dump MySQL database tables for file download.

It can dump one or more tables of a MySQL databases to files using PDO. All tables are dumped by parallel asynchronous AJAX requests.

The package can dump all database tables or exclude some of the tables.

It can avoid making table backups of tables that did not change since last backup by determining a checksum values for each table contents and checking if it changed.

Innovation Award
PHP Programming Innovation award nominee
January 2017
Number 2


Prize: One downloadable copy of Komodo IDE
All database applications should provide means to take backups of the database structure and contents, so there is a way to recover when there is an accident that causes database data loss.

This package provides an efficient solution to take MySQL database backups.

It can take backups of all or specific tables in a database. If a previous table backup exists, it may determine if the table has changed using the table checksum, and avoid making the backup in case the table did not change.

Manuel Lemos
Picture of Alessandro Quintiliani
  Performance   Level  
Name: Alessandro Quintiliani <contact>
Classes: 3 packages by
Country: Italy Italy
Age: 55
All time rank: 255198 in Italy Italy
Week rank: 215 Up14 in Italy Italy Up
Innovation award
Innovation award
Nominee: 2x

Recommendations

Good backup/restore MySQL
Script to backup and restore databases

Example

<?php
/*
* ***************************************************************************************************
*
* File name: index.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/>.
*
* ***************************************************************************************************
*/

/*

USAGE:
*
* suppose you have several mysql databases to dump, such as:
*
* dbone, dbtwo, dbthree, dbfour
*
* and all of these databases are installed on the same server (or also different servers), having the same credentials:
* hostname: my.hostdb.com (or, as the same, its IP address, i.e. 10.20.30.40)
* port: 3307 (not the mysql default port)
* login: mylogin2db
* password: mypasswd2db
*
*
*
*
* 1) set in the file dbConnSettings.php the connection parameters to each database to dump.
* The order you set the parameters is not relevant, except for the call to setTypeDbToDump,
* which must always be the first call on a set database connection parameters
* IMPORTANT NOTICE: the object reference variable name $odmp must be the same as the instance name of MultiDump()
* in index.php (see $odmp = new MultiDump(); defined in index.php); so if you would like to use another object reference variable name
* such as $myDmpObjRef, you must replace in index.php the variable $odmp with $myDmpObjRef
*
* # set connection parameters to dbone mysql database
* $odmp->setTypeDbToDump('dbone','mysql'); ### THE TYPE OF DATABASE MUST ALWAYS BE THE FIRST PARAMETER TO SET
* $odmp->setHostDbToDump('dbone','my.hostdb.com'); ( or, similarly, $odmp ->setHostDbToDump('dbone','10.20.30.40'); )
* $odmp->setPortDbToDump('dbone', 3307); ( this method call is not necessary if the db connection refers to the default port )
* $odmp->setLoginDbToDump('dbone', 'mylogin2db');
* $odmp->setPasswordDbToDump('dbone', 'mypasswd2db');
*
* # set connection parameters to dbtwo mysql database
* $odmp->setTypeDbToDump('dbtwo','mysql'); ### THE TYPE OF DATABASE MUST ALWAYS BE THE FIRST PARAMETER TO SET
* $odmp->setHostDbToDump('dbtwo','my.hostdb.com'); ( or, similarly, $odmp->setHostDbToDump('dbtwo','10.20.30.40'); )
* $odmp->setPortDbToDump('dbtwo', 3307); ( this method call is not necessary if the db connection refers to the default port )
* $odmp->setLoginDbToDump('dbtwo', 'mylogin2db');
* $odmp->setPasswordDbToDump('dbtwo', 'mypasswd2db');
*
* # set connection parameters to dbthree mysql database
* $odmp->setTypeDbToDump('dbthree','mysql'); ### THE TYPE OF DATABASE MUST ALWAYS BE THE FIRST PARAMETER TO SET
* $odmp->setHostDbToDump('dbthree','my.hostdb.com'); ( or, similarly, $odmp->setHostDbToDump('dbthree','10.20.30.40'); )
* $odmp->setPortDbToDump('dbthree', 3307); ( the call on this method is not necessary if the db connection refers to the default port )
* $odmp->setLoginDbToDump('dbthree', 'mylogin2db');
* $odmp->setPasswordDbToDump('dbthree', 'mypasswd2db');
*
* # set connection parameters to dbfour mysql database
* $odmp->setTypeDbToDump('dbfour','mysql'); ### THE TYPE OF DATABASE MUST ALWAYS BE THE FIRST PARAMETER TO SET
* $odmp->setHostDbToDump('dbfour','my.hostdb.com'); ( or, similarly, $odmp->setHostDbToDump('dbfour','10.20.30.40'); )
* $odmp->setPortDbToDump('dbfour', 3307); ( the call on this method is not necessary if the db connection refers to the default port )
* $odmp->setLoginDbToDump('dbfour', 'mylogin2db');
* $odmp->setPasswordDbToDump('dbfour', 'mypasswd2db');
*
*
* 2) (optional) if you want to exclude some tables from dump, i.e.:
* - alpha, beta from dbtwo database
* - gamma, delta, epsilon from dbfour database
*
* you must call the method
*
* setListExcludedTables(<dbname>, <array tables to exclude from dump>)
*
* where:
* - <dbname> is a string with the database name whose tables you want to exclude from dump are referring to
* - <array tables to exclude from dump> is an array containing the table names you want to exclude from dump
* (the order you list the tables in the array is not relevant)
*
* thus:
*
* $odmp->setListExcludedTables('dbtwo', array('alpha','beta'));
* $odmp->setListExcludedTables('dbfour', array('gamma','delta','epsilon'));
*
* (you can change the order of the calls to setListExcludedTables for each database)
*
*
* 3) (optional) if you want to dump some tables, i.e.:
* - zeta, eta, theta from dbone database
* - iota, kappa from dbfour database
*
* at each run and not only when their content/structure have changed, you must call the method
*
* setListForcedDumpTables(<dbname>, <array forced dump tables>|-1)
*
* where:
* - <dbname> is a string with the database name referring to the tables you want to dump at each run
* - <array forced dump tables>) is an array containing the tablenames you want to dump at each run
* (the order you list the tables in the array is not relevant)
* - value -1 forces the dump to all the tables of a database
*
* $odmp->setListForcedDumpTables('dbone', array('zeta','eta','theta'));
* $odmp->setListForcedDumpTables('dbfour', array('iota','kappa'));
*
* (you can change the order of the calls to setListForcedDumpTables for each database)
*
* if you want to dump at each run all the tables from a database, the second argument must be -1.
* So if you want to dump all the tables from i.e. dbthree database, you must call
*
* $odmp->setListForcedDumpTables('dbthree', -1);
*
*
* IMPORTANT NOTICE ON CALLS 2) AND 3):
*
* the excluded tables always have priority on the forced dump tables, independently whether you first call
* setListExcludedTables() or setListForcedDumpTables().
* So, if you want to force the dump of a table (i.e. my_important_table) at each run from mydb database, you call
*
* $odmp->setListForcedDumpTables('mydb', array('my_important_table'));
*
* but if you accidentally place my_important_table also in the array of the excluded tables, that is
*
* $odmp->setListExcludedTables('mydb', array('no_important_table','my_important_table'));
*
* my_important_table will be excluded from dumping instead of being dumped from mydb database
* at each run of the script
*
*
* 4) launch index.php
*/
##################################################################

error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT);

include_once(
"Class.MultiDump.php");

$odmp = new MultiDump();
$odmp->setLog("log", "logmaindump.txt", 1);
$odmp->wlog("START PROCEDURE MultiDump");

#################### INCLUDE AT THIS POINT OF THE MAIN PHP SCRIPT THE DATABASE CONNECTION PARAMETERS SCRIPT #####################


include_once("dbConnSettings.php");


###############################################################


$LIST_DB_DUMPED = $odmp->getListDbToDump();
$PAGE_GENERATING_DUMP_TABLE = $odmp->getPhpPageTableToDump();


$LIST_ALL_TABLES_DUMPED = array();
$LIST_TYPE_DB_DUMPED = array();
$LIST_POST_PARAMETERS = array();
$LIST_OUTPUT_FILES = array();
$LIST_EXCLUDED_DUMP_TABLES = array();


# POST variable names
$POST_VAR_DB_NAME = $odmp->getDbVarName();
$POST_VAR_TABLE_NAME = $odmp->getTableVarName();
$POST_VAR_DB_CONN_NAME = $odmp->getReferenceDbConnName();
$POST_VAR_FORCED_DUMP_TABLE_NAME = $odmp->getReferenceForcedDumpTableName();


# querystring variables to URL download archived database dump
$phpFileDownload = $odmp->getDownloadDumpPage();
$qsVarCompressionType = $odmp->getPostVarCompressionType();
$qsVarDbArchived = $odmp->getPostVarDbArchived();
$qsTypeDb = $odmp->getPostVarTypeDb();
$qsVarAllTablesToArchive = $odmp->getPostVarAllTablesToArchive();

foreach(
$LIST_DB_DUMPED as $db_dumped){
   
$odmp->wlog("database to dump: $db_dumped");
   
$odmp->setListTablesToDump($db_dumped);

   
$LIST_TYPE_DB_DUMPED[] = $odmp->getTypeDbToDump($db_dumped);
   
$LIST_TABLES_DUMP_FORCED = $odmp->getListForcedDumpTables($db_dumped);
   
$LIST_EXCLUDED_DUMP_TABLES[$db_dumped] = $odmp->getListExcludedTables($db_dumped);
   
$LIST_TABLES_DUMPED = $odmp->getListTablesToDump($db_dumped);

   
$POST_SERIALIZED_DB_CONN_INFO = serialize($odmp->getInfoDbConn($db_dumped));
   
$POST_SERIALIZED_EXCLUDED_TABLES = serialize($LIST_EXCLUDED_DUMP_TABLES[$db_dumped]);

    foreach(
$LIST_TABLES_DUMPED as $table_dumped){

       
$array_post_vars = array();
       
$array_post_vars[] = $POST_VAR_DB_NAME.'='.urlencode($db_dumped);
       
$array_post_vars[] = $POST_VAR_TABLE_NAME.'='.urlencode($table_dumped);
       
$array_post_vars[] = $POST_VAR_DB_CONN_NAME.'='.urlencode($POST_SERIALIZED_DB_CONN_INFO);

        if( (
is_array($LIST_TABLES_DUMP_FORCED) && in_array($table_dumped, $LIST_TABLES_DUMP_FORCED)) || ($LIST_TABLES_DUMP_FORCED == - 1) )
        {
           
$flagForcedDumpTable = 1;
        }
        else
        {
           
$flagForcedDumpTable = 0;
        }

       
$array_post_vars[] = $POST_VAR_FORCED_DUMP_TABLE_NAME.'='.$flagForcedDumpTable;

       
$stringPostVars = implode("&", $array_post_vars);
        if(!
in_array($table_dumped, $LIST_EXCLUDED_DUMP_TABLES[$db_dumped]))
        {
           
$LIST_ALL_TABLES_DUMPED[] = $db_dumped . "." . $table_dumped;
           
$LIST_POST_PARAMETERS[$db_dumped . "." . $table_dumped] = $stringPostVars;
        }
    }
}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title>
            MULTIDUMP
        </title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <style>

            table.tablelinksdld tr
            {
                line-height: 10px;
            }

            td
            {
                padding-right: 10px;
            }

            ul.listdld
            {
                margin: 0;
                padding: 0;
                list-style: none;
            }

            li.lidld
            {
                margin: 24;
                padding: 0;
            }

            div.pre
            {
                display: block;
                unicode-bidi: embed;
                font-family: monospace;
                white-space: nowrap;
            }

            div.divcallsdump
            {
                font-weight: bold;
                left: 30px;
            }

            div.divdashed
            {
                left: 0px;
            }


        </style>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
        </script>
        <script type="text/javascript">

            var db_dumped = <?php echo json_encode($LIST_DB_DUMPED, JSON_PRETTY_PRINT); ?>;
            var type_db_dumped = <?php echo json_encode($LIST_TYPE_DB_DUMPED, JSON_PRETTY_PRINT); ?>;
            var totalNumberTablesToDump = <?php echo count($LIST_ALL_TABLES_DUMPED); ?>;
            var all_tables_to_archive = '<?php echo urlencode(json_encode(serialize($LIST_ALL_TABLES_DUMPED), JSON_PRETTY_PRINT)); ?>';
            var numberJQueryCallsDone = 0;
            var errorsDump = 0;
            var strPostParamsArchiveZip = "";
            var strPostParamsArchiveTgz = "";

            <?php
            $array_ajax_requests
= array();

           
$ajaxRequestsHead = "var ajaxRequests = [";

            foreach(
$LIST_ALL_TABLES_DUMPED as $it=>$full_qualified_table_dumped)
            {

               
$post_vars_string = $LIST_POST_PARAMETERS[$full_qualified_table_dumped];

               
$id_div_response_dump = $it + 1;

               
$strJqueryPost = "jQuery.post('".$odmp->getPhpPageTableToDump()."','".$post_vars_string."',";
               
$strJqueryPost .= "function(responseDump,status,xhr) {\n";
               
$strJqueryPost .= " numberJQueryCallsDone++;\n";
               
$strJqueryPost .= " var errorsDumpRegExp = /div_dump_error_id_/;\n";
               
$strJqueryPost .= " var matchedErrorDumpResponse = responseDump.match(errorsDumpRegExp);\n";
               
$strJqueryPost .= " if (matchedErrorDumpResponse === true) {errorsDump++;};\n";
               
$strJqueryPost .= " document.getElementById('ajaxcallsdumptables').style.color = \"#0600ff\"\n";
               
$strJqueryPost .= " document.getElementById('ajaxcallsdumptables').innerHTML = 'Dumped ' + numberJQueryCallsDone + ' tables on ' + totalNumberTablesToDump + '...';\n";
               
$strJqueryPost .= " document.getElementById('dumpresult' + " . $id_div_response_dump . ").innerHTML = responseDump;\n";
               
$strJqueryPost .= "},";
               
$strJqueryPost .= "\"html\")";

               
$array_ajax_requests[] = $strJqueryPost;
            }

           
$strAjaxRequests = implode(",\n",$array_ajax_requests);

           
$ajaxRequestsTail = "];\n";

           
$ajaxRequests = $ajaxRequestsHead . $strAjaxRequests . $ajaxRequestsTail;

            echo
$ajaxRequests;
           
?>

            function createListHiddenInput(qstring)
            {
                var hiddenFields = "";
                var ahf = qstring.split('&');
                for (var i = 0; i < ahf.length; i++)
                {
                    var cp = ahf[i].split('=');
                    var keyPost = encodeURIComponent(cp[0]);
                    var valuePost = cp[1] || '';
                    if (valuePost.charAt(0)=='"' && valuePost.charAt(valuePost.length - 1)=='"')
                    {
                        hiddenFields+='<input type="hidden" name="'+keyPost+'" value='+valuePost+' />\n';
                    } else
                    {
                        hiddenFields+='<input type="hidden" name="'+keyPost+'" value="'+valuePost+'" />\n';
                    }
                }
                return hiddenFields;
            }


            $(document).ready(function()
                {

                    jQuery.when.apply(jQuery, ajaxRequests).done(function ()
                        {

                            var urlToDownloadDbArchived = '<?php echo $phpFileDownload; ?>';
                            var qsVarDbArchived = '<?php echo $qsVarDbArchived; ?>';
                            var qsTypeDb = '<?php echo $qsTypeDb; ?>';
                            var qsVarCompressionType = '<?php echo $qsVarCompressionType; ?>';
                            var qsVarAllTablesToArchive = '<?php echo $qsVarAllTablesToArchive; ?>';
                            var msgdump = "";

                            if (errorsDump == 0)
                            {
                                document.getElementById("ajaxcallsdumptables").style.color = "#0600ff";
                                msgdump = 'Dumped ' + numberJQueryCallsDone + ' tables on ' + totalNumberTablesToDump;
                            } else
                            {
                                document.getElementById("ajaxcallsdumptables").style.color = "red";
                                msgdump = "Dump accomplished with " + errorsDump + " errors!!!";
                            }

                            var links_dld = "<br>";

                            links_dld += '<ul class="listdld">';
                            links_dld += '<li class="lidld"><table class="tablelinksdld" cellpadding="0" cellspacing="0" border="0">';


                            for (var id=0; id<db_dumped.length; id++)
                            {

                                var db_name_dld = db_dumped[id];
                                var type_db = type_db_dumped[id];

                                strPostParamsArchiveZip = qsVarDbArchived+'='+db_name_dld+'&'+qsTypeDb+'='+type_db+'&'+qsVarAllTablesToArchive+'='+all_tables_to_archive+'&'+qsVarCompressionType+'=zip';
                                strPostParamsArchiveTgz = qsVarDbArchived+'='+db_name_dld+'&'+qsTypeDb+'='+type_db+'&'+qsVarAllTablesToArchive+'='+all_tables_to_archive+'&'+qsVarCompressionType+'=tgz';

                                var hiddenFieldsToZip = createListHiddenInput(strPostParamsArchiveZip);
                                var hiddenFieldsToTgz = createListHiddenInput(strPostParamsArchiveTgz);
                                var titleziparchive = 'If the database '+db_name_dld+' contains extremely large tables, the creation of the zip archive may take several minutes before downloading';
                                var titletgzarchive = 'If the database '+db_name_dld+' contains extremely large tables, the creation of the tgz archive may take several minutes before downloading';

                                links_dld += '<tr><td>Download db <i>'+db_name_dld+'</i></td><td>&nbsp;<form action="'+urlToDownloadDbArchived+'" method="post" id="formdldzip'+id+'" name="formdldzip'+id+'">'+hiddenFieldsToZip+'<a href="javascript:void(0);" id="linkdldzip'+id+'" title="'+titleziparchive+'" onClick="document.formdldzip'+id+'.submit()">zip</a></form></td><td>&nbsp;&nbsp;&nbsp;<form action="'+urlToDownloadDbArchived+'" method="post" id="formdldtgz'+id+'" name="formdldtgz'+id+'">'+hiddenFieldsToTgz+'<a href="javascript:void(0);" id="linkdldtgz'+id+'" title="'+titletgzarchive+'" onClick="document.formdldtgz'+id+'.submit()">tgz</a></form></td></tr>';
                            }

                            links_dld += '</table>';
                            links_dld += '</li>';
                            links_dld += '</ul>';

                            setTimeout(function()
                                {
                                    document.getElementById("divbreak").innerHTML="";
                                    document.getElementById("ajaxcallsdumptables").style.color = "green";
                                    msgdump = "Dump successfully accomplished!!!";
                                    document.getElementById("ajaxcallsdumptables").innerHTML = msgdump + links_dld;
                                }, 500);


                        }).fail(function (jqXhr, textStatus, error)
                        {
                            document.getElementById("ajaxcallsdumptables").style.color = "red";
                            document.getElementById("ajaxcallsdumptables").innerHTML = "Dump accomplished with errors!!! (error: " + error + ")";
                        })
                });

        </script>

    </head>
    <body>
        <div class="pre">

            <?php
            $odmp
->printInfoDump();
           
?>
<br>
            <div id="ajaxcallsdumptables" class="divcallsdump">
                <span>
                </span>
            </div>

            <?php
            $dbCurrentName
= "";
            foreach(
$LIST_ALL_TABLES_DUMPED as $i=>$fullyQualifiedTableDumped){

               
$j = $i + 1;

                list(
$dbnameDumped,$nameTableDumped) = explode(".",$fullyQualifiedTableDumped,2);

                if(
$dbnameDumped != $dbCurrentName){
                   
$dbCurrentName = $dbnameDumped;

                    if(
$i == 0) echo '<div id="divbreak"><br></div>';

                    echo
'<div class="divdashed">--------------------------------------------------------------------------</div>';

                    echo
"<h3>Report tables dumped from database <i>".$dbnameDumped."</i></h3>";
                }

                echo
'<div id="dumpresult'.$j.'">-------------------<br>Dumping table '.$fullyQualifiedTableDumped.'...</div>';

            }

            echo
'<div class="divdashed">--------------------------------------------------------------------------</div>';
           
?>

        </div>

    </body>
</html>
<?php
$odmp
->wlog("END PROCEDURE MultiDump");
$odmp->end();
unset(
$odmp);
?>


Details

Multi MySQLDump

PACKAGE DUMPING ONE OR MORE MySQL DATABASES THROUGH ASYNCHRONOUS AJAX REQUESTS, EACH FOR A SINGLE DATABASE TABLE TO DUMP; ON THE FIRST DUMP, EACH AJAX REQUEST CALLS A PHP SCRIPT WHICH DUMPS A DATABASE TABLE TO A SQL FILE; ON FURTHER REQUESTS (CALLED ON DEMAND OR SCHEDULED PERIODICALLY), A TABLE IS RE-DUMPED ONLY IF THEIR DATA/STRUCTURE HAVE CHANGED SINCE THE LAST DUMP; YOU CAN ALSO EXCLUDE ONE OR MORE TABLES OR FORCE THE DUMP AT EACH RUN. WHEN THE DUMP IS IN PROGRESS, THE HOME PAGE DISPLAYS:

  • THE NAME OF ALL THE DATABASES SET TO DUMP
  • OPTIONALLY, THE LIST OF THE TABLES EXCLUDED OR FORCED TO DUMP FOR EACH DATABASE
  • THE NUMBER OF AJAX REQUESTS STILL ACTIVE AND THE TOTAL NUMBER

WHEN ALL THE DUMP ARE SUCCESSFULLY COMPLETED, THE HOME PAGE ALSO DISPLAYS:

  • HYPERLINKS TO DOWNLOAD EACH DATABASE COMPRESSED IN BOTH ZIP AND TGZ FORMAT
  • FOR EACH TABLE DUMPED, HYPERLINKS TO DOWNLOAD THE SQL FILE UNCOMPRESSED OR COMPRESSED IN BOTH ZIP AND GZIP FORMAT PLUS THE INFORMATION ON WHETHER THE TABLE IS AT ITS FIRST DUMP OR NOT, ALONG WITH IF THE TABLE IS DUMPED AT EACH RUN OR ONLY ON CHANGING THEIR STRUCTURE/DATA.

*

AUTHOR

Alessandro Quintiliani <alex23rps at gmail dot com>

* LICENSE

GNU GPL (see file COPYING.txt)

*

PREREQUISITES

PHP >= 5.1

* DESCRIPTION

This package allows you to dump one or more MySQL databases, splitting the dump into a number of sql files, each for a table to dump.

The script *dbConnSettings.php* is the file where first you must define, for each database you want to dump:

  • the type of the database (i.e. mysql). This parameter must always be the first to set for each database
  • the hostname or IP address of the database server
  • the port number. This parameter can also be omitted if the database port is the default port (3306 for mysql)
  • the username to the database
  • the password to the database

the order you define the hostname, port, login, password is not relevant, but they must always be defined after the type of the database.

If you want to exclude one or more tables from dump, you can add this optional information always in dbConnSettings.php, as well as if the whole database or only one or more tables must be dumped at each run (see example on USAGE section).

NOTICE: excluded tables from dump always have priority on the ones set to dump at each run. This means that does not matter the order which you define the list of the excluded tables and the list of the forced dump tables: if you accidentally put a table in both the lists, the table will be excluded from dump.

There is an extra file, *parameters.txt*, called by the procedure, that is still properly configured to dump MySQL databases. This file contains a list of pairs

parameter=value

where parameter and value MUST NOT BE MODIFIED. All the pairs are grouped by:

  • GROUP OF FOLDERS<br>having the following pair

    folder_dump_files=dumpsql which defines the directory name (dumpsql) containing all the sql files created when dump databases are completed. THIS IS THE ONLY VALUE THAT CAN BE MODIFIED INSIDE parameters.txt. This parameter value can be a relative or absolute path. If it is a relative path (default), it is created if not exists inside the main package directory multidump; if you change it to an absolute path and place outside the webroot directory, make sure it has the right permission to make the procedure to create and read the sql files inside it

  • GROUP OF DB TYPE QUERIES group of 4 parametric queries, executed by the procedure when the php page dumptable.php is requested via AJAX. The procedure executes 4 different queries, which are:

    dump_mysql_query_show_tables=SHOW FULL TABLES FROM @@@database.name@@@ WHERE Table_Type = 'BASE TABLE' AND tables_in_@@@database.name@@@ NOT IN (@@@excluded.tables.name@@@)

    dump_mysql_query_checksum=CHECKSUM TABLE @@@database.name@@@.@@@table.name@@@

    dump_mysql_query_engine_db=SELECT DISTINCT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA='@@@database.name@@@'

    dump_mysql_query_engine_table=SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '@@@database.name@@@' AND TABLE_NAME = '@@@table.name@@@' the parameter names always must start with dump\_&lt;database type&gt; (for MySQL, they start with dump\_mysql). The words enclosed between @@@ in the queries are placeholders, replaced by the procedure with their current values; respectfully

    - @@@database.name@@@ is replaced by the name of the database that is being dumped - @@@excluded.tables.name@@@ is replaced by the list of tables excluded from dump and defined in the method setListIgnoredTables() called in the file dbConnSettings.php (see example on USAGE section) - @@@table.name@@@ is replaced by the name of the table that is being dumped

NOTICE: the third and the fourth query respectfully determine the engine type database and the engine type table to MySQL platform, which both result in MyISAM or InnoDB; on other database platforms, these two pairs might not defined

  • GROUP OF DUMP COMMAND LINE OPTIONS<br>this is the group af all the options required to dump a table from the command line, called in the file dumptable.php. The pairs with the options are:

    dump_type=mysql

    dump_mysql_option_command_storage_engine_myisam=--lock-tables=TRUE

    dump_mysql_option_command_storage_engine_innodb=--single-transaction

    dump_mysql_option_command_host=-h

    dump_mysql_option_command_port=-P

    dump_mysql_option_command_user=-u

    dump_mysql_option_command_password=-p the first parameter dump_type defines the type of database to dump (MySQL). The value must always be written in lowercase. The second and third parameters are specifically defined to mysql database (on other types of database these parameters might not be defined).<br>The remaining four pairs are the options required to set a database connection in the dump command line (host, port, username, password)

When the main page index.php runs, accordingly to the database connections set in the script dbConnSettings.php, a first PDO connection is set to obtain the list of the database tables to dump by executing the first query from the group GROUP OF MySQL QUERIES (with the placeholders properly replaced by the procedure); for each table, an AJAX request to dumptable.php along with some POST data is called asynchronously.

When dumptable.php page is executed by an AJAX request, a PDO connection to a database is set; the data sent via the POST with the AJAX request are:

  • the database name
  • the table name
  • the serialized database connection string
  • a numeric flag which determines if the table must be dumped at each run or only if its content/structure have changed since the last dump

Whether the dump of the table is done at each run or not, the second query from the group GROUP OF MySQL QUERIES (with the placeholders properly replaced) is executed and the CHECKSUM value from the database table referred to an AJAX request is calculated; the checksum value, along with the database name and the table name are set together to see if the file

>&lt;database name&gt;\_&lt;table name&gt;\_&lt;checksum value&gt;.sql

which is the file format where each table is dumped, exists inside the directory containing all the dumped tables (defined in the GROUP OF FOLDERS). All possible cases are:

  1. There is no file *&lt;database name&gt;\_&lt;table name&gt;\_&#42;.sql* This means the dump which is going to be executed is the first absolute dump of the table --> The dump command line of the table is promptly executed and the output redirected to &lt;database name&gt;\_&lt;table name&gt;\_&lt;checksum value&gt;.sql
  2. A file in the format *&lt;database name&gt;\_&lt;table name&gt;\_&#42;.sql has been found* This means that the table has been previously dumped and there are two possible options:

2.1 - the table must be dumped at each run --> the dump command line of the table is executed and the output redirected to &lt;database name&gt;\_&lt;table name&gt;\_&lt;checksum value&gt;.sql

2.2 - the table must be dumped only on change of their content/structure --> Here comes up the CHECKSUM function!!! CHECKSUM is a MySQL built-in hashing function which applicates to a table and results in an integer; if something in a table changes (data/structure), consequently their CHECKSUM value changes. The checksum value calculated by the the second query from the group GROUP OF MySQL QUERIES is compared with the checksum value contained in the dump filename and:

- 2.2.1 - if the two checksum values are identical this means that the table structure/data have not changed since the last dump and no new dump of the table is required

- 2.2.2 - if the two checksum values are different this means that the table structure/data have changed since the last dump and a new dump of the table is required and hence executed

In any case, the dump is made by using a MySQL native command *mysqldumpwhich is composed using the database connection parameters set in thedbConnSettings.phpand the option set taken from the group GROUP OF DUMP COMMAND LINE OPTIONS in the fileparameters.txt* according to the syntax

mysqldump <dump_mysql_option_command_storage_engine_myisam|dump_mysql_option_command_storage_engine_innodb> <dump_mysql_option_command_host> <host name or IP>   <dump_mysql_option_command_port> <port number>  <dump_mysql_option_command_user> <login>  <dump_mysql_option_command_password><password>  <database name> <table name>   >  <path to sql dump file>

IMPORTANT NOTICE

  • As pointed on the step 2.2, the checksum is the core function of the whole package which helps to determine if a table has changed or not between two consecutive runs of the main php script and hence if a new dump of a table is required. TAKE INTO ACCOUNT THAT CHECKSUM IS NOT GUARANTEED TO BE COLLISION-FREE: this means that there is a slight chance that if the content/structure of a table changes between to consecutive runs of the script, the checksum might not change, which could result in a skipped dump of the table the second time. But, as mentioned in the official MySQL documentation, the chances of such this collision are very few. Anyway you can always set the dump of a table (or all the database tables) at each run, even though this can reduce the total speed to complete a dump
  • The main command mysqldump is defined inside the class Class.DumpFileParameters.php and not in the file parameters.txt only for safety reason, to avoid setting any command, even desruptive, inside the file parameters.txt
  • Just before redumping a table, a dump file is always automatically backupped and the copy is deleted only when the new dump is completed with no error. So if a new dump fails, the original sql file is automatically restored and if the automatical restore fails too, a warning message of a required manual restore is displayed * USAGE

suppose you have several mysql databases to dump, such as:

  • dbone
  • dbtwo
  • dbthree
  • dbfour

and all of these databases are installed on the same server (but this is not relevant), having the same credentials:

  • hostname: my\.hostdb\.com (or, its IP address, i.e. 10.20.30.40)
  • port: 3307 (this is not the mysql default port)
  • login: mylogin2db
  • password: mypasswd2db

1) Uncompress the package multidump.zip and place the folder multidump and all of its content in the root directory of your web application or in any folder where permissions of creating folders and files are set properly. If you want to specify your own absolute path to the directory containing all the sql files (see the section of the group GROUP FOLDERS in the description of parameters.txt file), make sure the permission are set properly to allow the main php script to write to this folder

*

2) set in the file dbConnSettings.php one set connection for each database you want to dump. According to the above example ( dbone, dbtwo, dbthree, dbfour ) the settings are as it follows (NOTICE: the method call setTypeDbToDump must always be the first called on each set, while the order you call the other methods is not relevant):

# set connection to dbone mysql database
$odmp->setTypeDbToDump('dbone','mysql'); // !!! FIRST METHOD TO CALL ON dbone CONNECTION !!!
$odmp->setHostDbToDump('dbone','my.hostdb.com');  // or $odmp->setHostDbToDump('dbone','10.20.30.40');
$odmp->setPortDbToDump('dbone', 3307);   // the call on this method is not necessary if the db connection refers to the default port
$odmp->setLoginDbToDump('dbone', 'mylogin2db'); 
$odmp->setPasswordDbToDump('dbone', 'mypasswd2db'); 
 
# set connection to dbtwo mysql database
$odmp->setTypeDbToDump('dbtwo','mysql'); // !!! FIRST METHOD TO CALL ON dbtwo CONNECTION !!!
$odmp->setHostDbToDump('dbtwo','my.hostdb.com');  // or $odmp->setHostDbToDump('dbtwo','10.20.30.40');
$odmp->setPortDbToDump('dbtwo', 3307);   // the call on this method is not necessary if the db connection refers to the default port
$odmp->setLoginDbToDump('dbtwo', 'mylogin2db'); 
$odmp->setPasswordDbToDump('dbtwo', 'mypasswd2db');
 
# set connection to dbthree mysql database
$odmp->setTypeDbToDump('dbthree','mysql'); // !!! FIRST METHOD TO CALL ON dbthree CONNECTION !!!
$odmp->setHostDbToDump('dbthree','my.hostdb.com'); // or $odmp->setHostDbToDump('dbthree','10.20.30.40');
$odmp->setPortDbToDump('dbthree', 3307);  	// the call on this method is not necessary if the db connection refers to the default port
$odmp->setLoginDbToDump('dbthree', 'mylogin2db'); 
$odmp->setPasswordDbToDump('dbthree', 'mypasswd2db');

# set connection to dbfour mysql database
$odmp->setTypeDbToDump('dbfour','mysql'); // !!! FIRST METHOD TO CALL ON dbfour CONNECTION !!!
$odmp->setHostDbToDump('dbfour','my.hostdb.com');  //or $odmp->setHostDbToDump('dbfour','10.20.30.40');
$odmp->setPortDbToDump('dbfour', 3307);  	// the call on this method is not necessary if the db connection refers to the default port 
$odmp->setLoginDbToDump('dbfour', 'mylogin2db'); 
$odmp->setPasswordDbToDump('dbfour', 'mypasswd2db');

IMPORTANT NOTICE

the object reference variable name $odmp called in dbConnSettings.php must be the same as the instance name of MultiDump() in index.php (see $odmp = new MultiDump(); defined in index.php); so if you would like to use another object reference variable name such as $myDmpObjRef, you must replace in index.php the variable $odmp with $myDmpObjRef *

3) (optional) if you want to skip some tables from dumping, after setting all the db connections, you must call the method

$odmp->setListIgnoredTables(<dbname>, <array tables of dbname to exclude from dump>);

According to the above example, if you want to skip the following tables, i.e.:

  • alpha, beta from dbtwo database
  • gamma, delta, epsilon from dbfour database
  • lambda from dbthree database

you must insert the following method calls:

$odmp->setListIgnoredTables('dbtwo', array('alpha', 'beta')); 
$odmp->setListIgnoredTables('dbfour', array('gamma', 'delta', 'epsilon'));
$odmp->setListIgnoredTables('dbthree', array('lambda'));

the order you list the excluded tables from dump in the array is not relevant, as well as the order you call the method setListIgnoredTables to each database

*

4) (optional) if you want to dump some tables (or all the tables) at each run and not only when their data/structure have changed, you must call the method

$odmp->setListForcedDumpTables(<dbname>,<array tables to force dump>|-1);

According to the above example, if you want to dump the following tables at each run, i.e.:

  • zeta, eta, theta from dbone database
  • iota, kappa from dbfour database

you must insert the following method calls:

$odmp->setListForcedDumpTables('dbone', array('zeta', 'eta', 'theta'));
$odmp->setListForcedDumpTables('dbfour', array('iota', 'kappa'));

the order you list the forced-dump tables in the array is not relevant, as well as the order you call the method setListForcedDumpTables to each database; instead, if you want to dump all the tables from i.e. dbthree at each run, you must insert the following method call:

$odmp->setListForcedDumpTables('dbthree', -1);

<br>

IMPORTANT NOTICE ON THE STEPS 3) AND 4)

As mentioned in the first NOTICE of the DESCRIPTION, the tables excluded from dump always have priority on the tables dumped at each run, independently whether you first call setListIgnoredTables() or setListForcedDumpTables(). So, if you want to dump a table (i.e. my\_important\_table) at each run from mydb database, you call

$odmp->setListForcedDumpTables('mydb', array('my_important_table'));

but if you place by mistake my\_important\_table also in the array of the excluded tables and call the method setListIgnoredTables, such as

$odmp->setListIgnoredTables('mydb', array('no_important_table','my_important_table')); 

the table my\_important\_table will be excluded from dumping instead of being dumped at each run of the main script *

5) launch the main page

http://localhost/multidump

or, if you place this package inside a webroot at my.domain.com, launch the URL

http://my.domain.com/multidump

CUSTOMIZATION

Currently, this package is configured to dump only MySQL and PostGreSQL databases, but the dump can be extended to other database platforms. This package uses the following steps to dump MySQL tables, which are:

> 1. query to get the list of the tables to dump from a database filtered on the excluded tables > 2. query to calculate the checksum of a table > 3. query to get the storage engine of a table (depending on the database platform) > 4. get the host option to use in the dump command line > 5. get the port option to use in the dump command line > 6. get the user option to use in the dump command line > 7. get the password option to use in the dump command line > 8. get the storage engine option to use in the dump command line (depending on the database platform) > 9. creation and execution of the dump command line according to the checksum table comparison between two consecutive runs of the main script

these are all the steps necessary to implement the whole procedure to dump MySQL database, but some of them may not be defined to other database platform (such as 3. and 8. valid for MySQL, but not to Oracle).

If you want to dump another database platform, make sure:

  1. a hashing function applied to a table (such as CHECKSUM for MySQL) is defined
  2. a dump command line outputting to a SQL file is defined

if both the two points are satisfied, you can configure Multi MySQLDump to dump other database doing the following two mainsteps:

1) you must add two groups pair inside the file parameters.txt :

  • GROUP OF DB TYPE QUERIES

including the following pairs:

    
	dump_<dbtype>_query_show_tables=<query getting all tables from database name, filtered on excluded tables>
    dump_<dbtype>_query_checksum=<query getting a valid hashcode function to a database table>

replace &lt;dbtype&gt; with the type of database (a list of allowable types is defined in the file parameters.txt), as well as replace &lt;query...&gt; with the required query written in the right syntax and including placeholders); the extra pairs

    dump_<dbtype>_query_engine_db=<query getting the database engine type>
    dump_<dbtype>_query_engine_table=<query getting the table engine type>

which refer to the engine database and table and defined to MySQL database (MyISAM, InnoDB) must not be included to other database platform if not defined

  • GROUP OF DUMP COMMAND LINE OPTIONS

including the following pairs:

    dump_type=<db type, one-word only, written in lowercase with no hyphens, no blanks, no empty spaces between words>
    dump_<db type>_option_command_host=<host option>
    dump_oracle_option_command_port=<port option>
    dump_oracle_option_command_user=<username option>
    dump_oracle_option_command_password=<password option>

the two extra options

	
	dump_<dbtype>_option_command_storage_engine_myisam=--lock-tables=TRUE 		
	dump_<dbtype>_option_command_storage_engine_innodb=--single-transaction

which refer to the storage engines as defined to MySQL dump command line, must not be included  to other database platform if not defined

2) In the class Class.DumpFileParameters.php the following indexed array is defined


private $_db_references = array("mysql" => array("dump_command" => "mysqldump",	
	                                             "dump_redirect_output"=> ">"
												),
								"postgresql" => array("dump_command" => "pg_dump",
													  "dump_redirect_output"=> ">"
													 )
							   );

each key of $\_db\_references is the db type (value of dump_type in parameters.txt) and their value is a two-elements indexed array, whose keys must always be the strings dump\_command and dump\_redirect\_output, and their values must be respectfully the main command to dump a database (mysqldump for MySQL, pg\_dump for PostGreSQL) and the redirect operator to output sql file (">").

If you want an extra database platform, you must add a third pair key,value, where the key is one of the allowed database type defined in parameters.txt and the value is a two-elements indexed array, having "dump\_command" and "dump\_redirect\_output" as keys, and each value must be the command and the output redirect operator written accordingly to the syntax of the database platform. If some database platform does not require such this operator, you must place an empty string ("")

LOGGING RESULTS

You can include a log class named LogDeltaTime to track steps on the call of dumptable.php by the AJAX requests; to activate the log:

  • download the class Class.LogDeltaTime.php from its package LogDeltaTime and place inside multidump
  • uncomment the following instruction in the php file Class.MultiDump.php (remove the heading #)

    `include_once("Class.LogDeltaTime.php");`

    after index.php runs, a new directory log is created under multidump directory if not exists, and it will contain

    - a txt log file named logmaindump.txt - one log file named log\_dump\_db\_&lt;dbname&gt;\_\_table\_&lt;tablename&gt;.txt for each table dumped from &lt;dbname&gt;

    if you want to deactivate the logs, just comment the instruction

    include_once("Class.LogDeltaTime.php");

prepending a hash (#) or a double shlash (//) at it


  Files folder image Files (11)  
File Role Description
Plain text file Class.DumpFileParameters.php Class Class containing methods to extract parameters and parse some keywords from a parameters file
Plain text file Class.MapQuery.php Class Class containing methods to execute PDO queries
Plain text file Class.MultiDump.php Class Main class containing methods to get database connections, parameters to execute MySQL command to dump tables asynchronously each to a SQL file
Plain text file Class.PDO.php Class Class containing methods to set a PDO connection to a MySQL database
Accessible without login Plain text file COPYING.txt Lic. GNU General Public License is a free, copyleft license for software and other kinds of works
Accessible without login Plain text file dbConnSettings.php Conf. configuration file to set all database connections to dump and optionally tables to exclude or to dump at each run of the main script
Accessible without login Plain text file downloaddump.php Example file to create dinamically zip, tgz archives to download all the sql dump tables database or one single sql dump table in both zip or gzip format
Accessible without login Plain text file dumptable.php Example Script called by an AJAX request and used to execute a mysql dump command line to a single database table
Accessible without login Plain text file index.php Example Main php script to dump MySQL databases through AJAX requests to php script asynchronously called
Accessible without login Plain text file parameters.txt Data file containing configuration parameters to execute MySQL queries and set option to MySQL dump command
Accessible without login Plain text file README.md Doc. File containing description and usage on the whole package Multi MySQLDump

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:564
This week:0
All time:5,420
This week:455Up
User Ratings User Comments (2)
 All time
Utility:75%StarStarStarStar
Consistency:81%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:62%StarStarStarStar
Tests:-
Videos:-
Overall:59%StarStarStar
Rank:1311