欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

备份mysql数据库的php代码(一个表一个文件)

程序员文章站 2023-09-09 17:59:56
复制代码 代码如下:
复制代码 代码如下:

<?php
$cfg_dbhost ='localhost';//mysql主机
$cfg_dbname ='sq_test';//数据库名
$cfg_dbuser ='root';//数据库用户名
$cfg_dbpwd ='jb51.net';//数据库用户密码
$cfg_db_language ='utf8';//数据库编码

class dbmysql{
public static $dbhost = 'localhost';
public static $dbname;
public static $dbuser = 'root';
public static $dbpass;
public static $charset = 'utf8';
public static $db = null;
public $querycount = 0;

public function __construct()
{
self::$dbhost = $globals['cfg_dbhost'];
self::$dbname = $globals['cfg_dbname'];
self::$dbuser = $globals['cfg_dbuser'];
self::$dbpass = $globals['cfg_dbpwd'];
self::$charset= $globals['cfg_db_language'];
self::connect();
}
public function connect(){
self::$db=mysql_connect(self::$dbhost,self::$dbuser,self::$dbpass);
if(!self::$db){
self::sqlerror('无法连接服务器!'.self::mysqlerror);exit("无法连接服务器!");;
}
if(!mysql_select_db(self::$dbname)){
self::sqlerror('无法连接数据库('.self::$dbname.')!'.self::mysqlerror);exit("无法连接数据库!");
}
mysql_query("set names '".self::$charset."', character_set_client=binary, sql_mode='';",self::$db);
}

private function mysqlerror(){
return mysql_error();
}

public function gettablesname(){
$res = mysql_query('show tables from '.self::$dbname,self::$db);
$tables=array();
while ($row=mysql_fetch_row($res))$tables[]=$row[0];
mysql_free_result($res);
return $tables;
}
public function getfields($table){
$res=mysql_query('describe '.$table,self::$db);
$tables=array();
while($row=mysql_fetch_row($res))$tables[]=$row[0];
mysql_free_result($res);
return $tables;
}

public function fetch_array($sql){
$res=mysql_query($sql,self::$db);
$r=mysql_fetch_array($res);
mysql_free_result($res);
return $r;
}

public function fetch_assoc($sql){
$q3=mysql_query($sql,self::$db); $ra=array();
while($data=mysql_fetch_assoc($q3)){
$ra[]=$data;
}
mysql_free_result($q3);
return $ra;
}
private function sqlerror($message='',$info ='',$sql=''){//保存错误信息到文件
echo "{".$message."<br/>date: ".date('y-n-j h:i:s')."<br/>error: ".$info."<br/>sql: ".$sql."<br/>}<br/>";
}
public function close(){
self::$db =null;
}
public function __destruct()
{
self::close();
}
}

/*---class end*/

function makedir($dirpath){
if(!$dirpath) return 0;
$dirpath=str_replace("\\","/",$dirpath); $mdir="";
foreach(explode("/",$dirpath) as $val){
$mdir.=$val."/";
if($val==".."||$val==".")continue;
if(!is_dir($mdir)&&!file_exists($mdir)){
if(!@mkdir($mdir,0755)){
exit("创建目录 [".$mdir."]失败.");
}
}
}
return true;
}

function deldirandfile($dirname){
if($handle=opendir($dirname)){
while(false!==($item = readdir($handle))){
if($item !="."&&$item!=".."){
if(is_dir( "$dirname/$item")){
deldirandfile( "$dirname/$item");
}else{ unlink("$dirname/$item"); }
}
}
closedir( $handle );
if( rmdir( $dirname ) )echo "成功删除目录: $dirname<br/>\n";
}
}

function filein($filename="databak/",$table='',$mysql=''){
$fp = fopen($filename.'/'.$table.'.sql','w');
fputs($fp,$mysql);
fclose($fp);
}

header("content-type:text/html;charset=utf-8");

$db=new dbmysql();

$table=$db->gettablesname();

$filename="databak/".date("ymd");
$url=getcwd()."/databak/";
$handle = opendir($url);
while(false!==($file = readdir($handle))){
if ($file!="."&&$file!=".."&&is_dir($url."/".$file)) {
if(date("ymd")-$file>5){deldirandfile($url."/".$file);};
}
}

makedir($filename);
foreach($table as $t){
$s1=$db->fetch_array("show create table `$t`");
$mysql="/*time:".date("y-m-d h:i:s")." */\r\ndrop table if exists `$t`;\r\n".$s1['create table'].";\r\n\r\n";
$a1=$db->fetch_assoc("select * from `$t`");
foreach ($a1 as $data){
$vals=array_values($data);
$vals=array_map('addslashes',$vals);
$vals=join("','",$vals);
$vals="'".$vals."'";
$mysql.="insert into `$t` values ($vals);\r\n";
}
$mysql.="\r\n";
filein($filename,$t,$mysql);
}

echo "数据备份成功,生成备份文件   ".getcwd()."/".$filename."/<br/>程序自动清理5天以前的备份";
?>