Home Database Mysql Tutorial MySQL数据库中备份/恢复的两方法介绍_MySQL

MySQL数据库中备份/恢复的两方法介绍_MySQL

Jun 01, 2016 pm 02:03 PM
introduce backup recover data database document method Table of contents were able

下面介绍MySQL数据库备份/恢复的两种方法。

方法一:

 

<ccid_code></ccid_code><?php /*
 * 功能:数据备份/恢复文件简易方法
 *   以日期为单位,一天一个备份文件,以当天最后备份为准
 *   用提交表单的形式进行操作,
 *  其中<p style="TEXT-INDENT: 2em">下面介绍MySQL数据库备份/恢复的两种方法。 
<p style="TEXT-INDENT: 2em"><strong>方法一:</strong> </p>
<p style="TEXT-INDENT: 2em"> </p>
<ccid_nobr></ccid_nobr>
Copy after login
___FCKpd___0
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

方法二:

想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:

第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。

第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!

越想越不对劲,没有办法,看能不能从PHP操作数据库入手,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。

备份数据库有两种方式,一种是只备份数据库的结构,一种把是结构和所有的数据都备份出来,当然是第二种方法好啦,不过我为了考虑可能的需求就都作啦。

 

<ccid_code></ccid_code>/******  备份数据库结构 ******/

/*
函数名称:table2sql()
函数功能:把表的结构转换成为SQL
函数参数:$table: 要进行提取的表名
返 回 值:返回提取后的结果,SQL集合
函数作者:heiyeluren
*/

function table2sql($table) 
{
 global $db;
 $tabledump = "DROP TABLE IF EXISTS $table;\n";
 $createtable = $db->query("SHOW CREATE TABLE $table");
 $create = $db->fetch_row($createtable);
 $tabledump .= $create[1].";\n\n";

 return $tabledump;
}


/****** 备份数据库结构和所有数据 ******/
/*
函数名称:data2sql()
函数功能:把表的结构和数据转换成为SQL
函数参数:$table: 要进行提取的表名
返 回 值:返回提取后的结果,SQL集合
函数作者:heiyeluren
*/
function data2sql($table) 
{
 global $db;
 $tabledump = "DROP TABLE IF EXISTS $table;\n";
 $createtable = $db->query("SHOW CREATE TABLE $table");
 $create = $db->fetch_row($createtable);
 $tabledump .= $create[1].";\n\n";

 $rows = $db->query("SELECT * FROM $table");
 $numfields = $db->num_fields($rows);
 $numrows = $db->num_rows($rows);
 while ($row = $db->fetch_row($rows))
 {
  $comma = "";
  $tabledump .= "INSERT INTO $table VALUES(";
  for($i = 0; $i Host\n".
  "# 数据库:$db->Database\n".
  "# 备份编号: ". create_sess_id() ."\n". // 这里有一个生成session id的函数
  "# 备份时间: ".time_to_date('',6)."\n". // 这里就是获取当前时间的函数
  "#\n".
  "# 管理员:$admin ($admin_email)\n". // 管理员的用户名和邮箱地址
  "# $copyright\n".
  "# --------------------------------------------------------\n\n\n".
  $sqldump;
 
 // 保存到本地
 if($saveto == "local") 
 {
  ob_end_clean();
  header('Content-Encoding: none');
  header('Content-Type: '.(strpos
($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE') ?
 'application/octetstream' : 'application/octet-stream'));
  header('Content-Disposition: '.(strpos
($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 'MSIE') ?
 'inline; ' : 'attachment; ').'filename="'.$local_filename);
  header('Content-Length: '.strlen($sqldump));
  header('Pragma: no-cache');
  header('Expires: 0');
  echo $sqldump;
 } 
 // 保存到本地结束
 
 // 保存在服务器
 if($saveto == "server") 
 {
  if($filename != "") 
  {
   @$fp = fopen($filename, "w+");
   if ($fp)
   {
    @flock($fp, 3);
    if(@!fwrite($fp, $sqldump)) 
    {
     @fclose($fp);
     exit_msg("数据文件无法保存到服务器,请检查目录属性你是否有写的权限。");
    } 
    else 
    {
 exit_msg("数据成功备份至服务器 <a href="%5C%22%24filename%5C%22">$filename</a> 中。");
    }
   }
   else
   {
    exit_msg("无法打开你指定的目录". $filename .",
请确定该目录是否存在,或者是否有相应权限"); 
   }
  } 
  else 
  {
   exit_msg("您没有输入备份文件名,请返回修改。");
  }
 }
 // 保存到服务器结束
}
else
{
 exit_msg("数据表没有任何内容");
}
/* 备份数据库结束 */
Copy after login

呵呵,基本上这样就结束了,然后涉及到的一个问题是如何把数据恢复到数据库中,我想这个是不复杂的,但是最好能够满足有从客户端和从服务器恢复数据的功能。

POST["tbl_name"]为预备份表名称数组 *

下面介绍MySQL数据库备份/恢复的两种方法。

方法一:

 

___FCKpd___0
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

方法二:

想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:

第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。

第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!

越想越不对劲,没有办法,看能不能从PHP操作数据库入手,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。

备份数据库有两种方式,一种是只备份数据库的结构,一种把是结构和所有的数据都备份出来,当然是第二种方法好啦,不过我为了考虑可能的需求就都作啦。

 

___FCKpd___1
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

呵呵,基本上这样就结束了,然后涉及到的一个问题是如何把数据恢复到数据库中,我想这个是不复杂的,但是最好能够满足有从客户端和从服务器恢复数据的功能。

POST["sqlfile"]为预恢复数据文件的名称 * 注意:该备份没有结构备份,只有数据备份 * * 备份文件格式: * `表名称1`{{数据1}}`表名称2`{{数据2}}`表名称3`{{数据3}}... * * 创建时间:2005-02-25 * E-mail: kingerq AT msn.com * 来源:http://blog.csdn.net/kingerq */ include ("../inc/globals.inc.php");//省略包函文件db_mysql.inc和MYSQL连接信息 set_time_limit(0); $dbdir = "d:/site/dbbak/";//用绝对路径 $txtname = array(); if(

下面介绍MySQL数据库备份/恢复的两种方法。

方法一:

 

___FCKpd___0
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

方法二:

想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:

第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。

第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!

越想越不对劲,没有办法,看能不能从PHP操作数据库入手,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。

备份数据库有两种方式,一种是只备份数据库的结构,一种把是结构和所有的数据都备份出来,当然是第二种方法好啦,不过我为了考虑可能的需求就都作啦。

 

___FCKpd___1
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

呵呵,基本上这样就结束了,然后涉及到的一个问题是如何把数据恢复到数据库中,我想这个是不复杂的,但是最好能够满足有从客户端和从服务器恢复数据的功能。

POST){ if(!is_writable($dbdir)) { echo "对不起!指定的备份目录不可写!请修改权限"; exit; } //op为一个隐形域,识别备份或者恢复 if(

下面介绍MySQL数据库备份/恢复的两种方法。

方法一:

 

___FCKpd___0
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

方法二:

想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:

第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。

第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!

越想越不对劲,没有办法,看能不能从PHP操作数据库入手,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。

备份数据库有两种方式,一种是只备份数据库的结构,一种把是结构和所有的数据都备份出来,当然是第二种方法好啦,不过我为了考虑可能的需求就都作啦。

 

___FCKpd___1
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

呵呵,基本上这样就结束了,然后涉及到的一个问题是如何把数据恢复到数据库中,我想这个是不复杂的,但是最好能够满足有从客户端和从服务器恢复数据的功能。

POST["op"]){//备份数据 //生成每个表的临时备份文件 foreach(

下面介绍MySQL数据库备份/恢复的两种方法。

方法一:

 

___FCKpd___0
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

方法二:

想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:

第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。

第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!

越想越不对劲,没有办法,看能不能从PHP操作数据库入手,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。

备份数据库有两种方式,一种是只备份数据库的结构,一种把是结构和所有的数据都备份出来,当然是第二种方法好啦,不过我为了考虑可能的需求就都作啦。

 

___FCKpd___1
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

呵呵,基本上这样就结束了,然后涉及到的一个问题是如何把数据恢复到数据库中,我想这个是不复杂的,但是最好能够满足有从客户端和从服务器恢复数据的功能。

POST["tbl_name"] as $tbl){ $txtname[] = $tbl.".txt"; $sql = "SELECT * FROM `$tbl` INTO OUTFILE '".$dbdir.end($txtname)."'"; $db->query($sql); } //将生成的临时备份文件合在一起 $outfile = date("Y-m-d").".sql"; if(file_exists($dbdir.$outfile)) @unlink($dbdir.$outfile); $fpr = fopen($dbdir.$outfile, "a"); foreach($txtname as $txt){ if(file_exists($dbdir.$txt)){ //读取临时备份文件 $tdata = readfiles($dbdir.$txt); //生成备份文件 $tbl = explode(".", $txt); $str = "`".$tbl[0]."`{{".$tdata."}}"; if(fwrite($fpr, $str)){ echo $tbl[0]."...写入 $outfile 成功!
\n"; }else{ echo $tbl[0]."...写入 $outfile 失败!
\n"; } @unlink($dbdir.$txt); } } fclose($fpr); }else{//恢复数据 $tdata = readfiles($dbdir.

下面介绍MySQL数据库备份/恢复的两种方法。

方法一:

 

___FCKpd___0
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

方法二:

想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:

第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。

第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!

越想越不对劲,没有办法,看能不能从PHP操作数据库入手,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。

备份数据库有两种方式,一种是只备份数据库的结构,一种把是结构和所有的数据都备份出来,当然是第二种方法好啦,不过我为了考虑可能的需求就都作啦。

 

___FCKpd___1
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

呵呵,基本上这样就结束了,然后涉及到的一个问题是如何把数据恢复到数据库中,我想这个是不复杂的,但是最好能够满足有从客户端和从服务器恢复数据的功能。

POST["sqlfile"]); preg_match_all("/`(.*)`\{\{(.*)\}\}/isU", $tdata, $data_ar); foreach($data_ar[1] as $k => $tt){ if(empty($data_ar[2][$k])) continue; $tfile = $dbdir.$tt.".txt"; $fp = fopen($tfile, "w"); if(fwrite($fp, $data_ar[2][$k])){ //清空表 $sql = "TRUNCATE TABLE `$tt`"; $db->query($sql); //重新装入数据 $sql = "LOAD DATA LOW_PRIORITY INFILE ' ".$dbdir.$tt.".txt"."' INTO TABLE `$tt`"; if($db->query($sql)){ fclose($fp); echo $tt."表数据恢复成功!
\n"; unlink($dbdir.$tt.".txt"); }else{ echo $tt."表数据恢复失败!
\n"; } } } //echo $tdata; //print_r($data_ar); //exit; } } /* * 读取文件内容 * 参数 $file 为文件名及完整路径 * 返回文件内容 */ function readfiles($file){ $tdata = ""; $fp = fopen($file, "r"); if(filesize($file)

方法二:

想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:

第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。

第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!

越想越不对劲,没有办法,看能不能从PHP操作数据库入手,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。

备份数据库有两种方式,一种是只备份数据库的结构,一种把是结构和所有的数据都备份出来,当然是第二种方法好啦,不过我为了考虑可能的需求就都作啦。

 

___FCKpd___1
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

Detailed introduction of Samsung S24ai functions Detailed introduction of Samsung S24ai functions Jun 24, 2024 am 11:18 AM

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold

China Mobile: Humanity is entering the fourth industrial revolution and officially announced 'three plans” China Mobile: Humanity is entering the fourth industrial revolution and officially announced 'three plans” Jun 27, 2024 am 10:29 AM

According to news on June 26, at the opening ceremony of the 2024 World Mobile Communications Conference Shanghai (MWC Shanghai), China Mobile Chairman Yang Jie delivered a speech. He said that currently, human society is entering the fourth industrial revolution, which is dominated by information and deeply integrated with information and energy, that is, the "digital intelligence revolution", and the formation of new productive forces is accelerating. Yang Jie believes that from the "mechanization revolution" driven by steam engines, to the "electrification revolution" driven by electricity, internal combustion engines, etc., to the "information revolution" driven by computers and the Internet, each round of industrial revolution is based on "information and "Energy" is the main line, bringing productivity development

See all articles