 |
|
มีโค้ดสำรองข้อมูลดังนี้
<?
ob_start();
//****************************************************************************************************************//
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>
<body>
<?php
// +------------------------------------------------------------------+
// | M-Solutions.co.in MySQL Backup Utility,
// | Copyright (c) 2005-2007 M-Solutions
// | You are free to use and edit this code as you wish,
// | This script can be used freely as long as all copyright messages are intact.
// +------------------------------------------------------------------+
// +------------------------------------------------------------------+
// | Usage :
// | 1. Fill in the dbuser, dbname, and dbpwd fields below.
// | 2. set up a cron job to call this file.
// | this will create a backup of the db, in an .sql file, which can be restored via phpmyadmin.
// |
// | you can test it via http://www.domain.com/[folder]/cron_backupdb.php?verbose=1
// |
// | Referrences :http://www.interspire.com/content/articles/5/1/An-Introduction-to-Cron
// +------------------------------------------------------------------+
$dbuser = '';
$dbpwd = '';
$dbname = '';
$dbhost = 'localhost';
$dbprefix = '';
if(!$mycon = @mysql_connect($dbhost,$dbuser,$dbpwd)) die('Database Error: '.mysql_error());
elseif(!@mysql_select_db($dbname,$mycon)) die ('Database Error: '.mysql_error());
mysql_query("SET NAMES TIS620");
error_reporting(E_ALL & ~E_NOTICE);
$verbose = !empty($_REQUEST['verbose']) ? 1 : 0;
@set_time_limit(600); // try to give ourselves plenty of time to run
function backup_mysql()
{
global $dbname, $verbose;
$backticks =1;
$ticks = ($backticks == 1)? '`' : "'";
$set['data'] = 1; //with data;
$set['endit'] = 1;
$set['usedrop']= 1;
$tables = mysql_list_tables($dbname) or die(mysql_error());
$gettables = @mysql_num_rows($tables);
$a .= "##--------------------------------------------\r\n";
$a .= "##--------------------------------------------\r\n";
$a .= "##--".$_SERVER['SERVER_NAME']." mySQL Database : ".$dbname."\r\n";
$a .= "##--Total Tables : ".$gettables." Saved On :".date("Y-m-d H:i:s",time())." \r\n";
$a .= "##--------------------------------------------\r\n";
$a .= "##--------------------------------------------\r\n\r\n";
if($verbose) echo "<pre>".$a."</pre>";
$ender = ($set['endit']) ? ';':'';
$sql = mysql_query("SHOW TABLES");
while($table1 = mysql_fetch_array($sql))
{
$table = $table1[0];
$a .= "\r\n##------------------ ".$table." ----------------------\r\n \r\n";
$drop = "DROP TABLE IF EXISTS $ticks".$table.$ticks.$ender."\r\n";
$a .= ($set['usedrop']== 1) ? $drop:'';
$row1 = mysql_query("SHOW CREATE TABLE ".$table) or die(mysql_error());
$row2 = mysql_fetch_array($row1);
$row2 = $backticks == 1 ? $row2[1] : str_replace("`","'",$row2[1]);
$a .= $row2.$ender."\r\n";
//data
$a .= $set['data'] == 1 ? "\r\n".backup_table($table,$set['endit']):'';
$a .= "\r\n\r\n\r\n\r\n";
}
define( '_MSOL_PATH', dirname(__FILE__) );
$filename = _MSOL_PATH.'/sql_'.$dbname.'_'.date("dmy",time()).'.sql';
$filelength = strlen($a);
$filedata = $a;
if($verbose) echo "<pre> Path : ".$filename."</pre>";
if (!$handle = fopen($filename, 'w+')) {
die("Failed to open stream : ".$filename);
}
if (!fwrite($handle,$filedata)){
die("Fail - Not able to right to file : ".$filename);
}
fclose($handle);
exit();
}
function backup_table($table,$endit=0)
{
global $dbname, $verbose;
$sql = mysql_query("SELECT * FROM $table") or die(mysql_error());
$count = mysql_num_rows($sql);
$backticks = 1;
$ticks = ($backticks == 1)? '`' : "'";
$countit = mysql_num_fields($sql);
while ($row = mysql_fetch_array($sql))
{
$a .= "INSERT INTO $ticks".$table."$ticks SET ";
for ($i=0; $i < $countit; $i++)
{
$get = mysql_fetch_field($sql,$i);
$a .= $ticks.$get->name.$ticks."='".addslashes(stripslashes($row[$i]))."'";
if ($i+1 < $countit)
{
$a .= ", ";
}
}
$a .= $endit == 1 ? ";":"";
$a .= "\r\n";
}
return $a;
}
if($verbose) echo "<br>Starting Backup!";
backup_mysql();
?>
</body>
</html>
มันใช้งานได้ดี แต่ว่า มันจะสำรองไว้ในเครื่องเซอร์ฟเวอร์เท่านั้น เราต้องการให้มันคลิ๊กแล้วขึ้นหน้า save as ขึ้นมา เผื่อเซอร์ฟเวอร์เจ้ง จะต้องทำไงครับ เพิ่มโดค้ดส่วนไหน ขอบคุณครับ
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
11 ก.พ. 2550 10:45:29 |
By :
guzzbell |
View :
2139 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |