php简单备份

php

2014-07-22 14:21

<?


$conn=mysql_connect('127.0.0.1','root','')  or die ('Not connected : ' . mysql_error());

mysql_select_db("yy");         //要备份的数据库

mysql_query("set names utf8");

bak_tables('yy');              //开始备份





function bak_tables($dbname){

$result = mysql_list_tables($dbname);  

    if (!$result) {

        echo "数据库不存在". mysql_error();

        exit;

    }


    while ($tablename = mysql_fetch_row($result)) {

        echo "Table:". $tablename[0]."<br>";

        put_sql($tablename[0])."<br>";

    }

}




function get_sql($name){

$result = mysql_query("select * from $name")  or die("Query failed: " . mysql_error());

$i = 0;

$sql="CREATE TABLE `$name` (\n ";

while ($i < mysql_num_fields($result)) {

//echo "列 $i:";

$meta = mysql_fetch_field($result);

if (!$meta) {

return '';

}else{

$sql.= "`$meta->name` $meta->type($meta->max_length)";

if($meta->not_null)($sql.=" NOT NULL ");

$sql.=", \n";

if($meta->primary_key){ $key=" PRIMARY KEY  (`$meta->name`) "; };

// echo "name : $meta->name ; type : $meta->type  <br>";

}

$i++;

}

$sql.=$key;

$sql.=" ); ";

mysql_free_result($result);

return $sql;

}



function put_sql($name){

$str='';

$fields='';

$values='';

$field=array();

$value=array();

$result = mysql_query("SELECT * FROM $name");

    

while ($row = mysql_fetch_assoc($result)) {

if(empty($fields)){

foreach($row as $i => $v) 

$field[]="`$i`";

$value[]="`$v`";

}

$fields=@implode(",",$field);

  $values=@implode(",",$value);

}

$str.= " insert into $name ($fields) values ($values); \n";


}

$str=get_sql($name).$str;

file_put_contents($name."_sql.txt",$str);

}


?>

-----------------------------------------------------------------------------------------------

<?


//列出所有数据库名称

$conn=mysql_connect('127.0.0.1','root','')  or die ('Not connected : ' . mysql_error());

$db_list = mysql_list_dbs($conn);

while ($row = mysql_fetch_object($db_list)) {

    echo $row->Database . "<br>";

}





 // 列出所有表

$conn=mysql_connect('127.0.0.1','root','')  or die ('Not connected : ' . mysql_error());

$result = mysql_list_tables("yy");  //YY是数据库名称

    if (!$result) {

        echo "数据库不存在". mysql_error();

        exit;

    }


    while ($row = mysql_fetch_row($result)) {

        echo "Table:". $row[0]."<br>";

    }



 // 列出表的字段信息

$conn=mysql_connect('127.0.0.1','root','')  or die ('Not connected : ' . mysql_error());

mysql_select_db("yy");

$result = mysql_query("select * from ms_mess")  or die("Query failed: " . mysql_error());

$i = 0;

while ($i < mysql_num_fields($result)) {

    echo "列 $i:<br />\n";

    $meta = mysql_fetch_field($result);

    if (!$meta) {

        echo "没有列<br />\n";

    }

    echo "<pre>

blob:         $meta->blob

max_length:   $meta->max_length

multiple_key: $meta->multiple_key

name:         $meta->name

not_null:     $meta->not_null

numeric:      $meta->numeric

primary_key:  $meta->primary_key

table:        $meta->table

type:         $meta->type

unique_key:   $meta->unique_key

unsigned:     $meta->unsigned

zerofill:     $meta->zerofill

</pre>";

    $i++;

}

mysql_free_result($result);


?>