php实现MySQL数据库备份与还原类实例
这篇文章主要介绍了php实现MySQL数据库备份与还原类,可实现针对MySQL数据库的导入与导出功能,并且包含了针对数据库操作时的加锁与解锁等功能,非常具有实用价值,
本文实例讲述了php实现MySQL数据库备份与还原类。分享给大家供大家参考。具体分析如下:
这是一个非常简单的利用php来备份mysql数据库的类文件,我们只要简单的在dbmange中配置好连接地址用户名与数据库即可,下面我们一起来看这个例子,代码如下:
复制代码 代码如下:
/**
* 创建时间: 2012年5月21日
*
* 说明:分卷文件是以_v1.sql为结尾(20120522021241_all_v1.sql)
* 功能:实现mysql数据库分卷备份,选择表进行备份,实现单个sql文件及分卷sql导入
* 使用方法:
*
* ------1. 数据库备份(导出)------------------------------------------------------------
//分别是主机,用户名,密码,数据库名,数据库编码
$db = new DBManage ( 'localhost', 'root', 'root', 'test', 'utf8' );
// 参数:备份哪个表(可选),备份目录(可选,默认为backup),分卷大小(可选,默认2000,,即2M)
$db->backup ();
* ------2. 数据库恢复(导入)------------------------------------------------------------
//分别是主机,用户名,密码,数据库名,数据库编码
$db = new DBManage ( 'localhost', 'root', 'root', 'test', 'utf8' );
//参数:sql文件
$db->restore ( './backup/20120516211738_all_v1.sql');
*----------------------------------------------------------------------
*/
class DBManage
{
var $db; // 数据库连接
var $database; // 所用数据库
var $sqldir; // 数据库备份文件夹
var $record;
// 换行符
private $ds = "n";
// 存储SQL的变量
public $sqlContent = "";
// 每条sql语句的结尾符
public $sqlEnd = ";";
/**
* 初始化
*
* @param string $host
* @param string $username
* @param string $password
* @param string $thisatabase
* @param string $charset
*/
function __construct($host = 'localhost', $username = 'root', $password = '', $thisatabase = 'test', $charset = 'utf8')
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $thisatabase;
$this->charset = $charset;
// 连接数据库
$this->db = mysql_connect ( $this->host, $this->username, $this->password ) or die ( "数据库连接失败." );
// 选择使用哪个数据库
mysql_select_db ( $this->database, $this->db ) or die ( "无法打开数据库" );
// 数据库编码方式
mysql_query ( 'SET NAMES ' . $this->charset, $this->db );
}
/*
* ------------------------------------------数据库备份start----------------------------------------------------------
*/
/**
* 数据库备份
* 参数:备份哪个表(可选),备份目录(可选,默认为backup),分卷大小(可选,默认2000,即2M)
*
* @param $string $dir
* @param int $size
* @param $string $tablename
*/
function backup($tablename = '', $dir = '', $size = 2000)
{
//$dir = $dir ? $dir : 'backup/';
//$size = $size ? $size : 2000;
$sql = '';
// 只备份某个表
if (! emptyempty ( $tablename ))
{
echo '正在备份表' . $tablename . '
';
// 插入dump信息
$sql = $this->_retrieve();
// 插入表结构信息
$sql .= $this->_insert_table_structure ( $tablename );
// 插入数据
$data = mysql_query ( "select * from " . $tablename );
// 文件名前面部分
$filename = date ( 'YmdHis' ) . "_" . $tablename;
// 字段数量
$num_fields = mysql_num_fields ( $data );
// 第几分卷
$p = 1;
// 循环每条记录
while ( $record = mysql_fetch_array ( $data ) )
{
// 单条记录
$sql .= $this->_insert_record ( $tablename, $num_fields, $record );
// 如果大于分卷大小,则写入文件
if (strlen ( $sql ) >= $size * 1000)
{
$file = $filename . "_v" . $p . ".sql";
if ($this->_write_file ( $sql, $file, $dir ))
{
echo "表-" . $tablename . "-卷-" . $p . "-数据备份完成,生成备份文件 $dir$filename
";
}
else
{
echo "备份表-" . $tablename . "-失败
";
}
// 下一个分卷
$p ++;
// 重置$sql变量为空,重新计算该变量大小
$sql = "";
}
}
// sql大小不够分卷大小
if ($sql != "")
{
$filename .= "_v" . $p . ".sql";
if ($this->_write_file ( $sql, $filename, $dir ))
{
echo "表-" . $tablename . "-卷-" . $p . "-数据备份完成,生成备份文件 $dir$filename
";
}
else
{
echo "备份卷-" . $p . "-失败
";
}
}
}
else
{ // 备份全部表
if ($tables = mysql_query ( "show table status from " . $this->database ))
{
echo "读取数据库结构成功!
";
}
else
{
exit ( "读取数据库结构成功!
" );
}
// 插入dump信息
$sql .= $this->_retrieve();
// 文件名前面部分
$filename = date ( 'YmdHis' ) . "_all";
// 查出所有表
$tables = mysql_query ( 'SHOW TABLES' );
// 第几分卷
$p = 1;
// 循环所有表
while ( $table = mysql_fetch_array ( $tables ) )
{
// 获取表名
$tablename = $table [0];
// 获取表结构
$sql .= $this->_insert_table_structure ( $tablename );
$data = mysql_query ( "select * from " . $tablename );
$num_fields = mysql_num_fields ( $data );
// 循环每条记录
while ( $record = mysql_fetch_array ( $data ) )
{
// 单条记录
$sql .= $this->_insert_record ( $tablename, $num_fields, $record );
// 如果大于分卷大小,则写入文件
if (strlen ( $sql ) >= $size * 1000)
{
$file = $filename . "_v" . $p . ".sql";
// 写入文件
if ($this->_write_file ( $sql, $file, $dir ))
{
echo "-卷-" . $p . "-数据备份完成,生成备份文件$dir$file
";
}
else
{
echo "备份卷-" . $p . "-失败
";
}
// 下一个分卷
$p ++;
// 重置$sql变量为空,重新计算该变量大小
$sql = "";
}
}
}
// sql大小不够分卷大小
if ($sql != "")
{
$filename .= "_v" . $p . ".sql";
if ($this->_write_file ( $sql, $filename, $dir ))
{
echo "-卷-" . $p . "-数据备份完成,生成备份文件 $dir$filename
";
}
else
{
echo "备份卷-" . $p . "-失败
";
}
}
}
}
/**
* 插入数据库备份基础信息
*
* @return string
*/
private function _retrieve() {
$value = '';
$value .= '--' . $this->ds;
$value .= '-- MySQL database dump' . $this->ds;
$value .= '-- Created by DBManage class, Power By yanue. ' . $this->ds;
$value .= '-- ' . $this->ds;
$value .= '--' . $this->ds;
$value .= '-- 主机: ' . $this->host . $this->ds;
$value .= '-- 生成日期: ' . date ( 'Y' ) . ' 年 ' . date ( 'm' ) . ' 月 ' . date ( 'd' ) . ' 日 ' . date ( 'H:i' ) . $this->ds;
$value .= '-- MySQL版本: ' . mysql_get_server_info () . $this->ds;
$value .= '-- PHP 版本: ' . phpversion () . $this->ds;
$value .= $this->ds;
$value .= '--' . $this->ds;
$value .= '-- 数据库: `' . $this->database . '`' . $this->ds;
$value .= '--' . $this->ds . $this->ds;
$value .= '-- -------------------------------------------------------';
$value .= $this->ds . $this->ds;
return $value;
}
/**
* 插入表结构
*
* @param unknown_type $table
* @return string
*/
private function _insert_table_structure($table) {
$sql = '';
$sql .= "--" . $this->ds;
$sql .= "-- 表的结构" . $table . $this->ds;
$sql .= "--" . $this->ds . $this->ds;
// 如果存在则删除表
$sql .= "DROP TABLE IF EXISTS `" . $table . '`' . $this->sqlEnd . $this->ds;
// 获取详细表信息
$res = mysql_query ( 'SHOW CREATE TABLE `' . $table . '`' );
$row = mysql_fetch_array ( $res );
$sql .= $row [1];
$sql .= $this->sqlEnd . $this->ds;
// 加上
$sql .= $this->ds;
$sql .= "--" . $this->ds;
$sql .= "-- 转存表中的数据 " . $table . $this->ds;
$sql .= "--" . $this->ds;
$sql .= $this->ds;
return $sql;
}
/**
* 插入单条记录
*
* @param string $table
* @param int $num_fields
* @param array $record
* @return string
*/
private function _insert_record($table, $num_fields, $record) {
// sql字段逗号分割
$insert = $comma = "";
$insert .= "INSERT INTO `" . $table . "` VALUES(";
// 循环每个子段下面的内容
for($i = 0; $i
$insert .= ($comma . "'" .mysql_real_escape_string ( $record [$i] ) . "'");
$comma = ",";
}
$insert .= ");" . $this->ds;
return $insert;
}
/**
* 写入文件
*
* @param string $sql
* @param string $filename
* @param string $dir
* @return boolean
*/
private function _write_file($sql, $filename, $dir) {
$dir = $dir ? $dir : './backup/';
// 不存在文件夹则创建
if (! file_exists ( $dir )) {
mkdir ( $dir );
}
$re = true;
if (! @$fp = fopen ( $dir . $filename, "w+" )) {
$re = false;
echo "打开文件失败!";
}
if (! @fwrite ( $fp, $sql )) {
$re = false;
echo "写入文件失败,请文件是否可写";
}
if (! @fclose ( $fp )) {
$re = false;
echo "关闭文件失败!";
}
return $re;
}
/*
*
* -------------------------------上:数据库导出-----------分割线----------下:数据库导入--------------------------------
*/
/**
* 导入备份数据
* 说明:分卷文件格式20120516211738_all_v1.sql
* 参数:文件路径(必填)
*
* @param string $sqlfile
*/
function restore($sqlfile)
{
// 检测文件是否存在
if (! file_exists ( $sqlfile ))
{
exit ( "文件不存在!请检查" );
}
$this->lock ( $this->database );
// 获取数据库存储位置
$sqlpath = pathinfo ( $sqlfile );
$this->sqldir = $sqlpath ['dirname'];
// 检测是否包含分卷,将类似20120516211738_all_v1.sql从_v分开,有则说明有分卷
$volume = explode ( "_v", $sqlfile );
$volume_path = $volume [0];
echo "请勿刷新及关闭浏览器以防止程序被中止,如有不慎!将导致数据库结构受损
";
echo "正在导入备份数据,请稍等!
";
if (emptyempty ( $volume [1] ))
{
echo "正在导入sql:" . $sqlfile . '
';
// 没有分卷
if ($this->_import ( $sqlfile )) {
echo "数据库导入成功!";
}
else
{
exit ( '数据库导入失败!' );
}
}
else
{
//$volume_id = array();
// 存在分卷,则获取当前是第几分卷,循环执行余下分卷
$volume_id = explode ( ".sq", $volume [1] );
// 当前分卷为$volume_id
$volume_id = intval ( $volume_id [0] );
while ( $volume_id )
{
$tmpfile = $volume_path . "_v" . $volume_id . ".sql";
// 存在其他分卷,继续执行
if (file_exists ( $tmpfile )) {
// 执行导入方法
echo "正在导入分卷" . $tmpfile . '
';
if ($this->_import ( $tmpfile ))
{
}
else
{
exit ( "导入分卷" . $tmpfile . '失败!可能是数据库结构已损坏!请尝试从分卷1开始导入' );
}
}
else
{
echo "此分卷备份全部导入成功!
";
return;
}
$volume_id++;
}
}
}
/**
* 将sql导入到数据库(普通导入)
*
* @param string $sqlfile
* @return boolean
*/
private function _import($sqlfile) {
// sql文件包含的sql语句数组
$sqls = array ();
$f = fopen ( $sqlfile, "rb" );
// 创建表缓冲变量
$create = '';
while ( ! feof ( $f ) ) {
// 读取每一行sql
$line = fgets ( $f );
// 如果包含'-- '等注释,或为空白行,则跳过
if (trim ( $line ) == '' || preg_match ( '/--*?/', $line, $match )) {
continue;
}
// 如果结尾包含';'(即为一个完整的sql语句,这里是插入语句),并且不包含'ENGINE='(即创建表的最后一句),
if (! preg_match ( '/;/', $line, $match ) || preg_match ( '/ENGINE=/', $line, $match )) {
// 将本次sql语句与创建表sql连接存起来
$create .= $line;
// 如果包含了创建表的最后一句
if (preg_match ( '/ENGINE=/', $create, $match )) {
// 则将其合并到sql数组
$sqls [] = $create;
// 清空当前,准备下一个表的创建
$create = '';
}
// 跳过本次
continue;
}
$sqls [] = $line;
}
fclose ( $f );
// 循环sql语句数组,分别执行
foreach ( $sqls as $sql ) {
str_replace ( "n", "", $sql );
if (! mysql_query ( trim ( $sql ) )) {
echo mysql_error ();
return false;
}
}
return true;
}
/*
* -------------------------------数据库导入end---------------------------------
*/
// 关闭数据库连接
private function close() {
mysql_close ( $this->db );
}
// 锁定数据库,以免备份或导入时出错
private function lock($tablename, $op = "WRITE") {
if (mysql_query ( "lock tables " . $tablename . " " . $op ))
return true;
else
return false;
}
// 解锁
private function unlock() {
if (mysql_query ( "unlock tables" ))
return true;
else
return false;
}
// 析构
function __destruct() {
mysql_query ( "unlock tables", $this->db );
mysql_close ( $this->db );
}
}
$db = new DBManage ( 'localhost', 'root', '', 'tao', 'gbk' );
//$db->backup ('tao_admin');
$db->restore ( './backup/20140228222713_tao_admin_v1.sql');
?>
希望本文所述对大家的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
