Home php教程 php手册 php mysql 数据库备份程序

php mysql 数据库备份程序

May 26, 2016 pm 03:22 PM
mysql php database backup

提供一款实例的php mysql 数据库备份程序,很好方法的可以对你的数据库进行在线实时备份,这样可以保存数据库的安全,并且他是以.sql文件保存在bakdata目录还日期生成的数据库备份文件的,代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
	<html xmlns="http://www.w3.org/1999/xhtml"> 
	<head> 
	<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
	<link type="text/css" rel="stylesheet" href="common/control.css"> 
	<script type="text/javascript" language="javascript" src="common/admin.otable.js"></script> 
	<script type="text/javascript" language="javascript" src="common/include.func.js"></script> 
	<title>数据管理</title> 
	<style type="text/css"> 
	span {padding-left:8px;} 
	</style> 
	</head> 
	<body class="contentbody"> 
	<div class="maindiv"> 
	<span class="ctitle">esweb系统数据管理</span> 
	<ul class="cmenu"> 
	 <li><a href="sql_backup.php">数据备份</a></li> 
	    <li><a href="sql_restore.php">数据还原</a></li> 
	 <li><a href="sql_optimize.php">数据优化</a> 
	</ul> 
	<div class="concontent"> 
	<?php 
	/*--------------界面--------------*/if(!$_post[&#39;act&#39;]){/*----------------------*/ 
	$msgs[]="服务器备份目录为$backup"; 
	$msgs[]="对于较大的数据表,强烈建议使用分卷备份"; 
	$msgs[]="只有选择备份到服务器,才能使用分卷备份功能"; 
	//show_msg($msgs); 
	 
	<div class="thead"><span class="left"><img  src="/static/imghw/default1.png"  data-src="images/icon2/into.gif"  class="lazy"   align="absmiddle" / alt="php mysql 数据库备份程序 " > 如果需要备份、还原或者优化<font color="red"> 大量的数据 </font>, 推荐使用"<a href="http://www.phome.net/ebak2010/" target="_blank">帝国备份王2010</a>"</span> <span class="right"></span></div> 
	<form name="myform" method="post" action="sql_backup.php"> 
	<table cellpadding="" cellspacing="0" border="0" width="100%" class="ctable" id="otable" align="center"> 
	<tr> 
	 <th colspan="2">esweb系统数据备份</th> 
	</tr> 
	<tr> 
	 <td align="right" width="250">选择备份方式</td> 
	    <td><input type="radio" name="bfzl" value="quanbubiao" checked="checked" />备份全部数据  
	    <input type="radio" name="bfzl" value="danbiao" />备份单张表数据   
	    <select name="tablename"><option value="">请选择</option> 
	    <?php 
	    $d->query("show table status from $mysqldb"); 
	    while($d->nextrecord()){ 
	    echo "<option value=&#39;".$d->f(&#39;name&#39;)."&#39;>".$d->f(&#39;name&#39;)."</option>";} 
	     
	    </select></td> 
	</tr> 
	<tr> 
	 <td align="right">选择目标位置</td> 
	    <td><input type="radio" name="weizhi" value="server" checked="checked" />备份到服务器  
	    <input type="radio" name="weizhi" value="localpc" />备份到本地</td> 
	</tr> 
	<tr> 
	 <td align="right">使用分卷备份</td> 
	    <td><input type="checkbox" name="fenjuan" value="yes" checked="checked" /> 
	    分卷备份 <input name="filesize" type="text" value="1260" size="10" class="text2" /> k <span>(只有选择备份到服务器,才能使用分卷备份功能)</span></td> 
	</tr> 
	<tr> 
	 <td align="right">程序说明</td> 
	    <td>1.对于较大的数据表,强烈建议使用分卷备份. 
	    <br>2.只有选择备份到服务器,才能使用分卷备份功能. 
	    </td> 
	</tr> 
	<tr> 
	 <td align="center" colspan="2"><input type="submit" id="act" name="act" value="备 份" class="button" /> <input type="reset" value="重 置" class="button" /></td> 
	</tr> 
	</table> 
	</form> 
	<?php /*-------------界面结束-------------*/}/*---------------------------------*/ 
	/*----*/else{/*--------------主程序-----------------------------------------*/ 
	if($_post[&#39;weizhi&#39;]=="localpc"&&$_post[&#39;fenjuan&#39;]==&#39;yes&#39;) 
	{$msgs[]="只有选择备份到服务器,才能使用分卷备份功能"; 
	show_msg($msgs); pageend();} 
	if($_post[&#39;fenjuan&#39;]=="yes"&&!$_post[&#39;filesize&#39;]) 
	{$msgs[]="您选择了分卷备份功能,但未填写分卷文件大小"; 
	show_msg($msgs); pageend();} 
	if($_post[&#39;weizhi&#39;]=="server"&&!writeable($backup)) 
	{$msgs[]="备份文件存放目录&#39;$backup&#39;不可写,请修改目录属性"; 
	show_msg($msgs); pageend();} 
	/*----------备份全部表-------------*/if($_post[&#39;bfzl&#39;]=="quanbubiao"){/*----*/ 
	/*----不分卷*/if(!$_post[&#39;fenjuan&#39;]){/*--------------------------------*/ 
	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[&#39;weizhi&#39;]=="localpc") down_file($sql,$filename); 
	elseif($_post[&#39;weizhi&#39;]=="server") 
	{if(write_file($sql,$filename)) 
	$msgs[]="全部数据表数据备份完成,生成备份文件&#39;$backup/$filename&#39;"; 
	else $msgs[]="备份全部数据表失败"; 
	show_msg($msgs); 
	pageend(); 
	} 
	/*-----------------不要卷结束*/}/*-----------------------*/ 
	/*-----------------分卷*/else{/*-------------------------*/ 
	if(!$_post[&#39;filesize&#39;]) 
	{$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[&#39;filesize&#39;]*1000){ 
	     $filename.=("_v".$p.".sql"); 
	     if(write_file($sql,$filename)) 
	     $msgs[]="全部数据表-卷-".$p."-数据备份完成,生成备份文件&#39;$backup/$filename&#39;"; 
	     else $msgs[]="备份表-".$_post[&#39;tablename&#39;]."-失败"; 
	     $p++; 
	     $filename=date("ymd",time())."_all"; 
	     $sql="";} 
	} 
	} 
	if($sql!=""){$filename.=("_v".$p.".sql");   
	if(write_file($sql,$filename)) 
	$msgs[]="全部数据表-卷-".$p."-数据备份完成,生成备份文件&#39;$backup/$filename&#39;";} 
	show_msg($msgs); 
	/*---------------------分卷结束*/}/*--------------------------------------*/ 
	/*--------备份全部表结束*/}/*---------------------------------------------*/ 
	/*--------备份单表------*/elseif($_post[&#39;bfzl&#39;]=="danbiao"){/*------------*/ 
	if(!$_post[&#39;tablename&#39;]) 
	{$msgs[]="请选择要备份的数据表"; show_msg($msgs); pageend();} 
	/*--------不分卷*/if(!$_post[&#39;fenjuan&#39;]){/*-------------------------------*/ 
	$sql=make_header($_post[&#39;tablename&#39;]); 
	$d->query("select * from ".$_post[&#39;tablename&#39;]); 
	$num_fields=$d->nf(); 
	while($d->nextrecord()) 
	{$sql.=make_record($_post[&#39;tablename&#39;],$num_fields);} 
	$filename=date("ymd",time())."_".$_post[&#39;tablename&#39;].".sql"; 
	if($_post[&#39;weizhi&#39;]=="localpc") down_file($sql,$filename); 
	elseif($_post[&#39;weizhi&#39;]=="server") 
	{if(write_file($sql,$filename)) 
	$msgs[]="表-".$_post[&#39;tablename&#39;]."-数据备份完成,生成备份文件&#39;$backup/$filename&#39;"; 
	else $msgs[]="备份表-".$_post[&#39;tablename&#39;]."-失败"; 
	show_msg($msgs); 
	pageend(); 
	} 
	/*----------------不要卷结束*/}/*------------------------------------*/ 
	/*----------------分卷*/else{/*--------------------------------------*/ 
	if(!$_post[&#39;filesize&#39;]) 
	{$msgs[]="请填写备份文件分卷大小"; show_msg($msgs);pageend();} 
	$sql=make_header($_post[&#39;tablename&#39;]); $p=1;  
	$filename=date("ymd",time())."_".$_post[&#39;tablename&#39;]; 
	$d->query("select * from ".$_post[&#39;tablename&#39;]); 
	$num_fields=$d->nf(); 
	while ($d->nextrecord())  
	{  
	    $sql.=make_record($_post[&#39;tablename&#39;],$num_fields); 
	      if(strlen($sql)>=$_post[&#39;filesize&#39;]*1000){ 
	     $filename.=("_v".$p.".sql"); 
	     if(write_file($sql,$filename)) 
	     $msgs[]="表-".$_post[&#39;tablename&#39;]."-卷-".$p."-数据备份完成,生成备份文件&#39;$backup/$filename&#39;"; 
	     else $msgs[]="备份表-".$_post[&#39;tablename&#39;]."-失败"; 
	     $p++; 
	     $filename=date("ymd",time())."_".$_post[&#39;tablename&#39;]; 
	     $sql="";} 
	} 
	if($sql!=""){$filename.=("_v".$p.".sql");   
	if(write_file($sql,$filename)) 
	$msgs[]="表-".$_post[&#39;tablename&#39;]."-卷-".$p."-数据备份完成,生成备份文件&#39;$backup/$filename&#39;";} 
	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[&#39;http_user_agent&#39;], &#39;msie&#39;) ? &#39;application/octetstream&#39; : &#39;application/octet-stream&#39;)); 
	    
	header("content-disposition: ".(strpos($_server[&#39;http_user_agent&#39;], &#39;msie&#39;) ? &#39;inline; &#39; : &#39;attachment; &#39;)."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", &#39;w&#39;)) 
	    { 
	@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." "; 
	$d->query("show create table ".$table); 
	$d->nextrecord(); 
	$tmp=preg_replace("/ /","",$d->f("create table")); 
	$sql.=$tmp." "; 
	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."&#39;".mysql_escape_string($d->record[$i])."&#39;"); $comma = ",";} 
	$sql .= ") "; 
	return $sql; 
	} 
	function show_msg($msgs) 
	{ 
	$title="提示:"; 
	echo "<table width=&#39;100%&#39; border=&#39;0&#39; cellpadding=&#39;0&#39; cellspacing=&#39;0&#39;>"; 
	echo "<tr><td>".$title."</td></tr>"; 
	echo "<tr><td><br><ul>"; 
	while (list($k,$v)=each($msgs)) 
	{ 
	echo "<li>".$v."</li>"; 
	} 
	echo "</ul></td></tr></table>"; 
	} 
	function pageend() 
	{ 
	exit(); 
	} 
	 
	</div><br /><br /> 
	</div> 
	</div> 
	</body> 
	</html> 
	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查询错误<br>"; 
	echo "查询语句:".$sql."<br>"; 
	echo "错误信息:".$err; 
	}//开源代码phprm.com 
	/****************************************类结束***************************/ 
	 
	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 &#39;utf8&#39;");
Copy after login

本文链接:

收藏随意^^请保留教程地址.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

See all articles