Backup Database

Не съм го тествал, намерих го в гугъл ->
Creating a backup of MySQL DB using PHP.
All of the hard work is done using built-in MySQL utility called mysqldump.

Step 1 (backup): Create backup & D/L to local PC
#PHP CODE
$host = 'localhost';
$dbuser = 'db_user';
$dbpword = 'db_password';
$dbname = 'db_name';
$backupFile = date("Y-m-d") . '.gz';
# Use system functions: MySQLdump & GZIP to generate compressed backup file
$command = "mysqldump -h$host -u$dbuser -p$dbpword $dbname | gzip> $backupFile";
system($command);
# Start the download process
$len = filesize($backupFile);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/gzip");
header("Content-Disposition: attachment; filename=$filename;");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($backupFile);
# Delete the temporary backup file from server
unlink($backupFile);

Step 2 (restore): Browse for backup file
#HTML CODE
<form name="form" method="post" action="proc_restore.php" enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="100000000000" />
<input type="file" name="restorefile" />
<input type="submit" name="submit" value="Submit" />
</form>

Step 3 (restore): U/L & recreate the DB
#PHP CODE
$uploadFile = $_FILES['restorefile']['name'];
$move = move_uploaded_file($_FILES['restorefile']['tmp_name'], $uploadFile);
# GZOPEN & GZGETS work on gzip'ed files like FOPEN & FGETS work w/ text files
$zp = gzopen($uploadFile, 'r');
while (!gzeof($zp))
{
$line = gzgets ($zp, 4096);
# db_query() is a user defined function that executes SQL commands
$rtl = db_query($line);
}
gzclose($zp);
unlink($uploadFile);
Ето и линка -> http://www.tech-geeks.org/geeklog/article.php?story=2005031409340971
 
дава ми поредица от грешки

едит:
намерих един урок от ТУК сега ше го пробвам и ще кажа :?
 
стана с този код но защи има timestamp today да не би да е нагласен да прави backup през определено време
в момента сваля .sql в главната директория как ще стане да е в папката sql/ и всяка таближа да е през един ред ако може <br / >
<?php
include "../../config/config_db.php";
function dbquery($query) {
@mysql_query("SET CHARACTER SET cp1251");
$result = @mysql_query($query);

if (!$result) {
echo mysql_error();
return false;
} else {
return $result;
}
}
function dbrows($query) {
$result = @mysql_num_rows($query);
return $result;
}
function dbarraynum($query) {
$result = @mysql_fetch_row($query);

if (!$result) {
echo mysql_error();
return false;
} else {
return $result;
}
}
$q=mysql_query("SHOW TABLES");
while($r=mysql_fetch_array($q)){
$db_tables[]=$r['0'];
}

if(count($db_tables)>0){
$crlf = "\n";
ob_start();
@ob_implicit_flush(0);
dbquery('SET SQL_QUOTE_SHOW_CREATE=1');
foreach($db_tables as $table){
@set_time_limit(1200);
dbquery("OPTIMIZE TABLE $table");

echo "DROP TABLE IF EXISTS `$table`;$crlf";
$row=dbarraynum(dbquery("SHOW CREATE TABLE $table"));
echo $row[1].";".$crlf;
$result=dbquery("SELECT * FROM $table");

if($result&&dbrows($result)){
$column_list="";
$num_fields=mysql_num_fields($result);
for($i=0;$i<$num_fields;$i++){
$column_list.=(($column_list!="")?", ":"")."`".mysql_field_name($result,$i)."`";
}
}
while($row=dbarraynum($result)){
$dump="INSERT INTO `$table` ($column_list) VALUES (";
for($i=0;$i<$num_fields;$i++){
$dump.=($i>0)?", ":"";
if(!isset($row[$i])){
$dump.="NULL";
}elseif($row[$i]=="0"||$row[$i]!=""){
$type=mysql_field_type($result,$i);
if($type=="tinyint"||$type=="smallint"||$type=="mediumint"||$type=="int"||$type=="bigint"||$type=="timestamp"){
$dump.=$row[$i];
}else{
$search_array=array('\\','\'',"\x00","\x0a","\x0d","\x1a");
$replace_array=array('\\\\','\\\'','\0','\n','\r','\Z');
$row[$i]=str_replace($search_array,$replace_array,$row[$i]);
$dump.="'$row[$i]'";
}
}else{
$dump.="''";
}
}
$dump.=');';
echo $dump.$crlf;
}
}
$contents = ob_get_contents();
ob_end_clean();
$file = date("d.m.Y H-i").".sql";
$fh = fopen($file, 'w');
fwrite($fh, $contents);
fclose($fh);
}
?>
 

Back
Горе