I hope that you all backup your databases regularly, and with regularly I mean at least once a day. Those data are too valuable to get lost.

I have been using MySQLDumper for years, a PHP and Perl based tool for backing up MySQL databases and I really loved it. Now with PHP 7 it stopped working properly and the developer decided to stop working on that project. So not wanting to make a dump every day manually from the phpAdmin surface, I wrote a php file which runs as a cronjob on my server. Feel free to use and/or modify the code.

You must change the bold data to your specifications, table1, table2,… the tables you want to dump.

EXPORT_TABLES('host', 'user', 'password', 'databasename', array('table1', 'table2', 'table3',....));
function EXPORT_TABLES($host,$user,$pass,$name,$tables){
set_time_limit(0); $mysqli = new mysqli($host,$user,$pass,$name); $mysqli->select_db($name); $mysqli->query("SET NAMES 'utf8'");
$queryTables = $mysqli->query('SHOW TABLES'); while($row = $queryTables->fetch_row()) { $target_tables[] = $row[0]; } if($tables !== false) { $target_tables = array_intersect( $target_tables, $tables); }
$content = "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\r\nSET time_zone = \"+00:00\";\r\n\r\n\r\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\r\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\r\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\r\n/*!40101 SET NAMES utf8 */;\r\n--\r\n-- Database: `".$name."`\r\n--\r\n\r\n\r\n";
foreach($target_tables as $table){
if (empty($table)){ continue; }
$result = $mysqli->query('SELECT * FROM `'.$table.'`'); $fields_amount=$result->field_count; $rows_num=$mysqli->affected_rows; $res = $mysqli->query('SHOW CREATE TABLE '.$table); $TableMLine=$res->fetch_row();
$content .= "\n\n".$TableMLine[1].";\n\n"; $TableMLine[1]=str_ireplace('CREATE TABLE `','CREATE TABLE IF NOT EXISTS `',$TableMLine[1]);
for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) {
while($row = $result->fetch_row()) { //when started (and every after 100 command cycle):
if ($st_counter%100 == 0 || $st_counter == 0 ) {$content .= "\nINSERT INTO ".$table." VALUES";}
$content .= "\n("; for($j=0; $j<$fields_amount; $j++){ $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) ); if (isset($row[$j])){$content .= '"'.$row[$j].'"' ;} else{$content .= '""';} if ($j<($fields_amount-1)){$content.= ',';} } $content .=")";
//every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) {$content .= ";";} else {$content .= ",";} $st_counter=$st_counter+1;
}
} $content .="\n\n\n";
}
$content .= "\r\n\r\n/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\r\n/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\r\n/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;";
$myFile="backups/pedigree_dog_".date('d-m-Y')."_".date('H-i-s').".sql";
$file = fopen($myFile,"w");
echo fwrite($file,$content);
fclose($file);
}

If you want to make a dump of the complete database, replace the single tables by ‘array($target_tables)’ and delete the following part in line 5:

if($tables !== false) { $target_tables = array_intersect( $target_tables, $tables); }
Facebooktwitterpinterestmail
Last modified on March 2nd, 2021

Leave a Reply

Your email address will not be published. Required fields are marked *