Last active
December 11, 2015 00:58
-
-
Save roblabla/4519638 to your computer and use it in GitHub Desktop.
mysqldump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Database MySQLDump Control File | |
* Original author : | |
* Copyright (c) 2009 by James Elliott | |
* James.d.Elliott@gmail.com | |
* GNU General Public License v3 http://www.gnu.org/licenses/gpl.html | |
*/ | |
require_once('./mysqldump.class.php'); //Location Of Class File. | |
$drop_table_if_exists = false; //Add MySQL 'DROP TABLE IF EXISTS' Statement To Output? | |
$version2 = '1.1.2'; //Script Version. | |
$dbhost = ''; //Server Hostname. | |
$dbuser = ''; //Server User Name. | |
$dbpass = ''; //Server Password. | |
$dbname = ''; //Database Name On MySQL Server. | |
$backup = new MySQLDump(); | |
$backup->droptableifexists = $drop_table_if_exists; | |
$backup->connect($dbhost,$dbuser,$dbpass,$dbname); //Connect To Database | |
if (!$backup->connected) { die('Error: '.$backup->mysql_error); } //On Failed Connection, Show Error. | |
$backup->list_tables(); //List Database Tables. | |
$broj = count($backup->tables); //Count Database Tables. | |
$backupoutput = ""; | |
for ($i=0;$i<$broj;$i++) { | |
$table_name = $backup->tables[$i]; //Get Table Names. | |
$backup->dump_table($table_name); //Dump Data to the Output Buffer. | |
$backupoutput = $backupoutput . $backup->output; //Display Output. | |
} | |
$dumpfilename = date("d-m-Y-DayzDB"); // the name of the backup | |
$fileHandle = fopen($dumpfilename.'.sql', 'w') or die ("Error : can't open file"); | |
fwrite($fileHandle, $backupoutput); // create the .sql file and write the backup data | |
fclose($fileHandle); | |
create_zip(array($dumpfilename).'.sql',$dumpfilename.'.zip'); // create .zip of .sql file | |
unlink($dumpfilename.'.sql'); // Delete .sql file | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Database MySQLDump Class File | |
* Copyright (c) 2009 by James Elliott | |
* James.d.Elliott@gmail.com | |
* GNU General Public License v3 http://www.gnu.org/licenses/gpl.html | |
*/ | |
$version1 = '1.3.2'; //This Scripts Version. | |
/* creates a compressed zip file */ | |
function create_zip($files = array(),$destination = '',$overwrite = false) { | |
//if the zip file already exists and overwrite is false, return false | |
if(file_exists($destination) && !$overwrite) { return false; } | |
//vars | |
$valid_files = array(); | |
//if files were passed in... | |
if(is_array($files)) { | |
//cycle through each file | |
foreach($files as $file) { | |
//make sure the file exists | |
if(file_exists($file)) { | |
$valid_files[] = $file; | |
} | |
} | |
} | |
//if we have good files... | |
if(count($valid_files)) { | |
//create the archive | |
$zip = new ZipArchive(); | |
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { | |
return false; | |
} | |
//add the files | |
foreach($valid_files as $file) { | |
$zip->addFile($file,$file); | |
} | |
//debug | |
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; | |
//close the zip -- done! | |
$zip->close(); | |
//check to make sure the file exists | |
return file_exists($destination); | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
class MySQLDump { | |
var $tables = array(); | |
var $connected = false; | |
var $output; | |
var $droptableifexists = false; | |
var $mysql_error; | |
function connect($host,$user,$pass,$db) { | |
$return = true; | |
$conn = @mysql_connect($host,$user,$pass); | |
if (!$conn) { $this->mysql_error = mysql_error(); $return = false; } | |
$seldb = @mysql_select_db($db); | |
if (!$conn) { $this->mysql_error = mysql_error(); $return = false; } | |
$this->connected = $return; | |
return $return; | |
} | |
function list_tables() { | |
$return = true; | |
if (!$this->connected) { $return = false; } | |
$this->tables = array(); | |
$sql = mysql_query("SHOW TABLES"); | |
while ($row = mysql_fetch_array($sql)) { | |
array_push($this->tables,$row[0]); | |
} | |
return $return; | |
} | |
function list_values($tablename) { | |
$sql = mysql_query("SELECT * FROM $tablename"); | |
$this->output .= "\n\n-- Dumping data for table: $tablename\n\n"; | |
while ($row = mysql_fetch_array($sql)) { | |
$broj_polja = count($row) / 2; | |
$this->output .= "INSERT INTO `$tablename` VALUES("; | |
$buffer = ''; | |
for ($i=0;$i < $broj_polja;$i++) { | |
$vrednost = $row[$i]; | |
if (!is_integer($vrednost)) { $vrednost = "'".addslashes($vrednost)."'"; } | |
$buffer .= $vrednost.', '; | |
} | |
$buffer = substr($buffer,0,count($buffer)-3); | |
$this->output .= $buffer . ");\n"; | |
} | |
} | |
function dump_table($tablename) { | |
$this->output = ""; | |
$this->get_table_structure($tablename); | |
$this->list_values($tablename); | |
} | |
function get_table_structure($tablename) { | |
$this->output .= "\n\n-- Dumping structure for table: $tablename\n\n"; | |
if ($this->droptableifexists) { $this->output .= "DROP TABLE IF EXISTS `$tablename`;\nCREATE TABLE `$tablename` (\n"; } | |
else { $this->output .= "CREATE TABLE `$tablename` (\n"; } | |
$sql = mysql_query("DESCRIBE $tablename"); | |
$this->fields = array(); | |
while ($row = mysql_fetch_array($sql)) { | |
$name = $row[0]; | |
$type = $row[1]; | |
$null = $row[2]; | |
if (empty($null)) { $null = "NOT NULL"; } | |
$key = $row[3]; | |
if ($key == "PRI") { $primary = $name; } | |
$default = $row[4]; | |
$extra = $row[5]; | |
if ($extra !== "") { $extra .= ' '; } | |
$this->output .= " `$name` $type $null $extra,\n"; | |
} | |
$this->output .= " PRIMARY KEY (`$primary`)\n);\n"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment