kopia_mysql.php
<?php
$nazwa_pliku = date ("Y-m-d");
include ("mysqldumper.class.php");
$dumper = new Mysqldumper("localhost", "root", "haslo", "baza");
$dumper->setDroptables(true);
$dumpstring = $dumper->createDump();
$plik = "D:backupDB $nazwa_pliku.sql";
$fp = fopen("$plik", "w"); ?>
mysqldumper.class.php
<?php
/**
* MySQLdumper is a small php class that lets you generate a dump of a MySQL data
base
* with just 2 lines of code. The dump can be used as a database backup. The dump
* is a valid MySQL-query in plain text. It doesn't depend on the 'mysqldump' command
* line utility, so you won't encounter a problem if this isn't available on the server.
*
* Example 1: Create a database drump.
* <code>
* $dumper = new MysqlDumper("localhost", "user", "password", "databasename");
* $dumpstring = $dumper->createDump();
* </code>
*
* Example 2: Create a database drump with a 'DROP TABLE IF EXISTS'-statement for each table.
* <code>
* $dumper = new MysqlDumper("localhost", "user", "password", "databasename");
* $dumper->setDroptables(true);
* $dumpstring = $dumper->createDump();
* </code>
*
* Example 3: Create dumps of different databases.
* <code>
* $dumper = new MysqlDumper("localhost", "user", "password", "database1");
* $dumpstring1 = $dumper->createDump();
* $dumper->setDBname"("database2");
* $dumpstring2 = $dumper->createDump();
* </code>
*
* @package MySQLdumper
* @version 1.0
* @author Dennis Mozes <opensource@mosix.nl>
* @url <a href="http://www.mosix.nl/mysqldumper" target="_blank">http://www.mosix.nl/mysqldumper</a>
* @since php 4.0
* @copyright Dennis Mozes
* @license GNU/LGPL License: <a href="http://www.gnu.org/copyleft/lgpl.html" target="_blank">http://www.gnu.org/copyleft/lgpl.html</a>
**/
class Mysqldumper {
var $_host;
var $_dbuser;
var $_dbpassword;
var $_dbname;
var $_isDroptables;
function Mysqldumper($host = "localhost", $dbuser = "root", $dbpassword = "haslo", $dbname = "baza") {
$this->setHost($host);
$this->setDBuser($dbuser);
$this->setDBpassword($dbpassword);
$this->setDBname($dbname);
// Don't drop tables by default.
$this->setDroptables(false);
}
function setHost($host) {
$this->_host = $host;
}
function getHost() {
return $this->_host;
}
function setDBname($dbname) {
$this->_dbname = $dbname;
}
function getDBname() {
return $this->_dbname;
}
function getDBuser() {
return $this->_dbuser;
}
function setDBpassword($dbpassword) {
$this->_dbpassword = $dbpassword;
}
function getDBpassword() {
return $this->_dbpassword;
}
function setDBuser($dbuser) {
$this->_dbuser = $dbuser;
}
// If set to true, it will generate 'DROP TABLE IF EXISTS'-statements for each table.
function setDroptables($state) {
$this->_isDroptables = $state;
}
function isDroptables() {
return $this->_isDroptables;
}
function createDump() {
// Set line feed
$lf = "rn";
$resource = mysql_connect($this->getHost(), $this->getDBuser(), $this->getDBpassword()); $tables = $this->result2Array(0, $result);
foreach ($tables as $tblval) {
$createtable[$tblval] = $this->result2Array(1, $result);
}
// Set header
$output = "#". $lf;
$output .= "# mysqldumper SQL Dump" . $lf;
$output .= "# Version 1.0" . $lf;
$output .= "# ". $lf;
$output .= "# Host: " . $this->getHost() . $lf;
$output .= "# Czas wygenerowania: " . date("M j, Y at H:i") . $lf; $output .= "# Baza danych : `" . $this->getDBname() . "`" . $lf;
$output .= "#";
// Generate dumptext for the tables.
foreach ($tables as $tblval)
{
If (($tblval!="bip_logowanie") and ($tblval!="bip_users"))
{
$output .= $lf . $lf . "# --$tabela------------------------------------------------------" . $lf . $lf;
$output .= "#". $lf . "# Struktura tabeli dla `$tblval`" . $lf;
$output .= "#" . $lf . $lf;
// Generate DROP TABLE statement when client wants it to.
if($this->isDroptables())
{
$output .= "DROP TABLE IF EXISTS `$tblval`;" . $lf;
}
$output .= $createtable[$tblval][0].";" . $lf;
$output .= $lf;
$output .= "#". $lf . "# Zrzut danych tabeli `$tblval`". $lf . "#" . $lf;
$rows = $this->loadObjectList("", $result);
foreach($rows as $row)
{
$insertdump = $lf;
$insertdump .= "INSERT INTO `$tblval` VALUES (";
$arr = $this->object2Array($row);
foreach($arr as $key => $value)
{
$insertdump .= "'$value',";
}
$output .= rtrim($insertdump,',') . ");"; }
}
}
return $output;
}
// Private function object2Array.
function object2Array($obj) {
$array = null;
foreach (get_object_vars($obj) as $key => $value) {
$array[$key] = $this->object2Array($value);
else
$array[$key] = $value;
}
}
return $array;
}
// Private function loadObjectList.
function loadObjectList($key='', $resource) {
if ($key)
$array[$row->$key] = $row;
else
$array[] = $row;
}
return $array;
}
// Private function result2Array.
function result2Array($numinarray = 0, $resource) {
$array[] = $row[$numinarray];
}
return $array;
}
}
?>
Ten post edytował L.Pociask 22.07.2006, 19:11:45