php mysql Database Backup程式_PHP教程

來源:互聯網
上載者:User
php mysql Database Backup程式/*提供一款執行個體的php mysql Database Backup程式,很好方法的可以對你的資料庫進行線上即時備份,這樣可以儲存資料庫的安全,並且他是以.sql檔案儲存在bakdata目錄還日期產生的Database Backup檔案的。

php教程 mysql教程 資料庫教程備份程式
/*
提供一款執行個體的php mysql Database Backup程式,很好方法的可以對你的資料庫進行線上即時備份,這樣可以儲存資料庫的安全,並且他是以.sql檔案儲存在bakdata目錄還日期產生的Database Backup檔案的。
*/

?>







資料管理



esweb系統資料管理


  • 資料備份

  • 資料還原

  • 資料最佳化


/*--------------介面--------------*/if(!$_post['act']){/*----------------------*/
$msgs[]="伺服器備份目錄為$backup";
$msgs[]="對於較大的資料表,強烈建議使用分卷備份";
$msgs[]="只有選擇備份到伺服器,才能使用分卷備份功能";
//show_msg($msgs);
?>
如果需要備份、還原或者最佳化 大量的資料 , 推薦使用"帝國備份王2010"

/*----*/else{/*--------------主程式-----------------------------------------*/
if($_post['weizhi']=="localpc"&&$_post['fenjuan']=='yes')
{$msgs[]="只有選擇備份到伺服器,才能使用分卷備份功能";
show_msg($msgs); pageend();}
if($_post['fenjuan']=="yes"&&!$_post['filesize'])
{$msgs[]="您選擇了分卷備份功能,但未填寫分卷檔案大小";
show_msg($msgs); pageend();}
if($_post['weizhi']=="server"&&!writeable($backup))
{$msgs[]="備份檔案存放目錄'$backup'不可寫,請修改目錄屬性";
show_msg($msgs); pageend();}

/*----------備份全部表-------------*/if($_post['bfzl']=="quanbubiao"){/*----*/
/*----不分卷*/if(!$_post['fenjuan']){/*--------------------------------*/
if(!$tables=$d->query("show table status from $mysqldb"))
{$msgs[]="讀資料庫結構錯誤"; show_msg($msgs); pageend();}
$sql="";
while($d->nextrecord($tables))
{
$table=$d->f("name");
$sql.=make_header($table);
$d->query("select * from $table");
$num_fields=$d->nf();
while($d->nextrecord())
{$sql.=make_record($table,$num_fields);}
}
$filename=date("ymd",time())."_all.sql";
if($_post['weizhi']=="localpc") down_file($sql,$filename);
elseif($_post['weizhi']=="server")
{if(write_file($sql,$filename))
$msgs[]="全部資料表資料備份完成,產生備份檔案'$backup/$filename'";
else $msgs[]="備份全部資料表失敗";
show_msg($msgs);
pageend();
}
/*-----------------不要卷結束*/}/*-----------------------*/
/*-----------------分卷*/else{/*-------------------------*/
if(!$_post['filesize'])
{$msgs[]="請填寫備份檔案分卷大小"; show_msg($msgs);pageend();}
if(!$tables=$d->query("show table status from $mysqldb"))
{$msgs[]="讀資料庫結構錯誤"; show_msg($msgs); pageend();}
$sql=""; $p=1;
$filename=date("ymd",time())."_all";
while($d->nextrecord($tables))
{
$table=$d->f("name");
$sql.=make_header($table);
$d->query("select * from $table");
$num_fields=$d->nf();
while($d->nextrecord())
{$sql.=make_record($table,$num_fields);
if(strlen($sql)>=$_post['filesize']*1000){
$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="全部資料表-卷-".$p."-資料備份完成,產生備份檔案'$backup/$filename'";
else $msgs[]="備份表-".$_post['tablename']."-失敗";
$p++;
$filename=date("ymd",time())."_all";
$sql="";}
}
}
if($sql!=""){$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="全部資料表-卷-".$p."-資料備份完成,產生備份檔案'$backup/$filename'";}
show_msg($msgs);
/*---------------------分卷結束*/}/*--------------------------------------*/
/*--------備份全部表結束*/}/*---------------------------------------------*/

/*--------備份單表------*/elseif($_post['bfzl']=="danbiao"){/*------------*/
if(!$_post['tablename'])
{$msgs[]="請選擇要備份的資料表"; show_msg($msgs); pageend();}
/*--------不分卷*/if(!$_post['fenjuan']){/*-------------------------------*/
$sql=make_header($_post['tablename']);
$d->query("select * from ".$_post['tablename']);
$num_fields=$d->nf();
while($d->nextrecord())
{$sql.=make_record($_post['tablename'],$num_fields);}
$filename=date("ymd",time())."_".$_post['tablename'].".sql";
if($_post['weizhi']=="localpc") down_file($sql,$filename);
elseif($_post['weizhi']=="server")
{if(write_file($sql,$filename))
$msgs[]="表-".$_post['tablename']."-資料備份完成,產生備份檔案'$backup/$filename'";
else $msgs[]="備份表-".$_post['tablename']."-失敗";
show_msg($msgs);
pageend();
}
/*----------------不要卷結束*/}/*------------------------------------*/
/*----------------分卷*/else{/*--------------------------------------*/
if(!$_post['filesize'])
{$msgs[]="請填寫備份檔案分卷大小"; show_msg($msgs);pageend();}
$sql=make_header($_post['tablename']); $p=1;
$filename=date("ymd",time())."_".$_post['tablename'];
$d->query("select * from ".$_post['tablename']);
$num_fields=$d->nf();
while ($d->nextrecord())
{
$sql.=make_record($_post['tablename'],$num_fields);
if(strlen($sql)>=$_post['filesize']*1000){
$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="表-".$_post['tablename']."-卷-".$p."-資料備份完成,產生備份檔案'$backup/$filename'";
else $msgs[]="備份表-".$_post['tablename']."-失敗";
$p++;
$filename=date("ymd",time())."_".$_post['tablename'];
$sql="";}
}
if($sql!=""){$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="表-".$_post['tablename']."-卷-".$p."-資料備份完成,產生備份檔案'$backup/$filename'";}
show_msg($msgs);
/*----------分卷結束*/}/*--------------------------------------------------*/
/*----------備份單表結束*/}/*----------------------------------------------*/

/*---*/}/*-------------主程式結束------------------------------------------*/

function write_file($sql,$filename)
{
$re=true;
global $backup;
if(!@$fp=fopen($backup."/".$filename,"w+")) {$re=false; echo "failed to open target file";}
if(!@fwrite($fp,$sql)) {$re=false; echo "failed to write file";}
if(!@fclose($fp)) {$re=false; echo "failed to close target file";}
return $re;
}

function down_file($sql,$filename)
{
ob_end_clean();
header("content-encoding: none");
header("content-type: ".(strpos($_server['http_user_agent'], 'msie') ? 'application/octetstream' : 'application/octet-stream'));

header("content-disposition: ".(strpos($_server['http_user_agent'], 'msie') ? 'inline; ' : 'attachment; ')."filename=".$filename);

header("content-length: ".strlen($sql));
header("pragma: no-cache");

header("expires: 0");
echo $sql;
$e=ob_get_contents();
ob_end_clean();
}

function writeable($dir)
{

if(!is_dir($dir)) {
@mkdir($dir, 0777);
}

if(is_dir($dir))
{

if($fp = @fopen("$dir/test.test", 'w'))
{
@fclose($fp);
@unlink("$dir/test.test");
$writeable = 1;
}
else {
$writeable = 0;
}

}

return $writeable;

}

function make_header($table)
{global $d;
$sql="drop table if exists ".$table."n";
$d->query("show create table ".$table);
$d->nextrecord();
$tmp=preg_replace("/n/","",$d->f("create table"));
$sql.=$tmp."n";
return $sql;
}

function make_record($table,$num_fields)
{global $d;
$comma="";
$sql .= "insert into ".$table." values(";
for($i = 0; $i < $num_fields; $i++)
{$sql .= ($comma."'".mysql_escape_string($d->record[$i])."'"); $comma = ",";}
$sql .= ")n";
return $sql;
}

function show_msg($msgs)
{
$title="提示:";
echo "



";echo " ";echo "
".$title."

    ";
    while (list($k,$v)=each($msgs))
    {
    echo "
  • ".$v."
  • ";
    }
    echo "
";
}

function pageend()
{
exit();
}
?>




class db{

var $linkid;
var $sqlid;
var $record;

function db($host="",$username="",$password="",$database="")
{
if(!$this->linkid) @$this->linkid = mysql_connect($host, $username, $password) or die("串連伺服器失敗.");
@mysql_select_db($database,$this->linkid) or die("無法開啟資料庫");
return $this->linkid;}

function query($sql)
{if($this->sqlid=mysql_query($sql,$this->linkid)) return $this->sqlid;
else {
$this->err_report($sql,mysql_error);
return false;}
}

function nr($sql_id="")
{if(!$sql_id) $sql_id=$this->sqlid;
return mysql_num_rows($sql_id);}

function nf($sql_id="")
{if(!$sql_id) $sql_id=$this->sqlid;
return mysql_num_fields($sql_id);}

function nextrecord($sql_id="")
{if(!$sql_id) $sql_id=$this->sqlid;
if($this->record=mysql_fetch_array($sql_id)) return $this->record;
else return false;
}

function f($name)
{
if($this->record[$name]) return $this->record[$name];
else return false;
}

function close() {mysql_close($this->linkid);}

function lock($tblname,$op="write")
{if(mysql_query("lock tables ".$tblname." ".$op)) return true; else return false;}

function unlock()
{if(mysql_query("unlock tables")) return true; else return false;}

function ar() {
return @mysql_affected_rows($this->linkid);
}

function i_id() {
return mysql_insert_id();
}

function err_report($sql,$err)
{
echo "mysql查詢錯誤
";
echo "查詢語句:".$sql."
";
echo "錯誤資訊:".$err;
}
/****************************************類結束***************************/


global $mysqlhost, $mysqluser, $mysqlpwd, $mysqldb, $backup;
$mysqlhost = $mydbhost; //host name
$mysqluser = $mydbuser; //login name
$mysqlpwd = $mydbpw; //password
$mysqldb = $mydbname; //name of database

$d=new db($mysqlhost,$mysqluser,$mysqlpwd,$mysqldb);
$d->query("set names 'utf8'");

http://www.bkjia.com/PHPjc/630798.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630798.htmlTechArticlephp mysql Database Backup程式/*提供一款執行個體的php mysql Database Backup程式,很好方法的可以對你的資料庫進行線上即時備份,這樣可以儲存資料庫的...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.