1、实例化DbBak需要告诉它两件事:数据服务器在哪里($connectid)、备份到哪个目录($backupDir):
require_once('DbBak.php');
require_once('TableBak.php');
$connectid = mysql_connect('localhost','root','123456');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
2、然后就可以开始备份数据库了,你不仅能够指定备份那个数据库,而且能详细设置只备份那几个表:
2.1如果你想备份mybbs库中的所有表,只要这样:
$DbBak->backupDb('mybbs');
2.2如果你只想备份mybbs库中的board、face、friendlist表,可以用一个一维数组指定:
$DbBak->backupDb('mybbs',array('board','face','friendsite'));
2.3如果只想备份一个表,比如board表:
$DbBak->backupDb('mybbs','board');
3,数据恢复:
对于2.1、2.1、2.3三种情况,只要相应的修改下语句,把backupDb换成restoreDb就能实现数据恢复了:
$DbBak->restoreDb('mybbs');
SQL代码
$DbBak->restoreDb('mybbs',array('board','face','friendsite'));
PHP代码
$DbBak->restoreDb('mybbs','board');
PHP代码
require_once('TableBak.php');
class DbBak {
var $_mysql_link_id;
var $_dataDir;
var $_tableList;
var $_TableBak;
function DbBak($_mysql_link_id,$dataDir)
{
( (!is_string($dataDir)) || strlen($dataDir)==0) && die('error:$datadir is not a string');
!is_dir($dataDir) && mkdir($dataDir);
$this->_dataDir = $dataDir;
$this->_mysql_link_id = $_mysql_link_id;
}
function backupDb($dbName,$tableName=null)
{
( (!is_string($dbName)) || strlen($dbName)==0 ) && die('$dbName must be a string value');
//step1:选择数据库:
mysql_select_db($dbName);
//step2:创建数据库备份目录
$dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;
!is_dir($dbDir) && mkdir($dbDir);
//step3:得到数据库所有表名 并开始备份表
$this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);
if(is_null($tableName)){//backup all table in the db
$this->_backupAllTable($dbName);
return;
}
if(is_string($tableName)){
(strlen($tableName)==0) && die('....');
$this->_backupOneTable($dbName,$tableName);
return;
}
if (is_array($tableName)){
foreach ($tableName as $table){
( (!is_string($table)) || strlen($table)==0 ) && die('....');
}
$this->_backupSomeTalbe($dbName,$tableName);
return;
}
}
function restoreDb($dbName,$tableName=null){
( (!is_string($dbName)) || strlen($dbName)==0 ) && die('$dbName must be a string value');
//step1:检查是否存在数据库 并连接:
@mysql_select_db($dbName) || die("the database $dbName dose not exists");
//step2:检查是否存在数据库备份目录
$dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;
!is_dir($dbDir) && die("$dbDir not exists");
//step3:start restore
$this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);
if(is_null($tableName)){//backup all table in the db
$this->_restoreAllTable($dbName);
return;
}
if(is_string($tableName)){
(strlen($tableName)==0) && die('....');
$this->_restoreOneTable($dbName,$tableName);
return;
}
if (is_array($tableName)){
foreach ($tableName as $table){
( (!is_string($table)) || strlen($table)==0 ) && die('....');
}
$this->_restoreSomeTalbe($dbName,$tableName);
return;
}
}
function _getTableList($dbName)
{
$tableList = array();
$result=mysql_list_tables($dbName,$this->_mysql_link_id);
for ($i = 0; $i array_push($tableList,mysql_tablename($result, $i));
}
mysql_free_result($result);
return $tableList;
}
function _backupAllTable($dbName)
{
foreach ($this->_getTableList($dbName) as $tableName){
$this->_TableBak->backupTable($tableName);
}
}
function _backupOneTable($dbName,$tableName)
{
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");
$this->_TableBak->backupTable($tableName);
}
function _backupSomeTalbe($dbName,$TableNameList)
{
foreach ($TableNameList as $tableName){
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");
}
foreach ($TableNameList as $tableName){
$this->_TableBak->backupTable($tableName);
}
}
function _restoreAllTable($dbName)
{
//step1:检查是否存在所有数据表的备份文件 以及是否可写:
foreach ($this->_getTableList($dbName) as $tableName){
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
. $dbName.DIRECTORY_SEPARATOR
. $tableName.DIRECTORY_SEPARATOR
. $tableName.'.sql';
!is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");
}
//step2:start restore
foreach ($this->_getTableList($dbName) as $tableName){
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
. $dbName.DIRECTORY_SEPARATOR
. $tableName.DIRECTORY_SEPARATOR
. $tableName.'.sql';
$this->_TableBak->restoreTable($tableName,$tableBakFile);
}
}
function _restoreOneTable($dbName,$tableName)
{
//step1:检查是否存在数据表:
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");
//step2:检查是否存在数据表备份文件 以及是否可写:
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
. $dbName.DIRECTORY_SEPARATOR
. $tableName.DIRECTORY_SEPARATOR
. $tableName.'.sql';
!is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");
//step3:start restore
$this->_TableBak->restoreTable($tableName,$tableBakFile);
}
function _restoreSomeTalbe($dbName,$TableNameList)
{
//step1:检查是否存在数据表:
foreach ($TableNameList as $tableName){
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");
}
//step2:检查是否存在数据表备份文件 以及是否可写:
foreach ($TableNameList as $tableName){
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
. $dbName.DIRECTORY_SEPARATOR
. $tableName.DIRECTORY_SEPARATOR
. $tableName.'.sql';
!is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");
}
//step3:start restore:
foreach ($TableNameList as $tableName){
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
. $dbName.DIRECTORY_SEPARATOR
. $tableName.DIRECTORY_SEPARATOR
. $tableName.'.sql';
$this->_TableBak->restoreTable($tableName,$tableBakFile);
}
}
}
?>
//只有DbBak才能调用这个类
class TableBak{
var $_mysql_link_id;
var $_dbDir;
//private $_DbManager;
function TableBak($mysql_link_id,$dbDir)
{
$this->_mysql_link_id = $mysql_link_id;
$this->_dbDir = $dbDir;
}
function backupTable($tableName)
{
//step1:创建表的备份目录名:
$tableDir = $this->_dbDir.DIRECTORY_SEPARATOR.$tableName;
!is_dir($tableDir) && mkdir($tableDir);
//step2:开始备份:
$this->_backupTable($tableName,$tableDir);
}
function restoreTable($tableName,$tableBakFile)
{
set_time_limit(0);
$fileArray = @file($tableBakFile) or die("can open file $tableBakFile");
$num = count($fileArray);
mysql_unbuffered_query("DELETE FROM $tableName");
$sql = $fileArray[0];
for ($i=1;$imysql_unbuffered_query($sql.$fileArray[$i]) or (die (mysql_error()));
}
return true;
}
function _getFieldInfo($tableName){
$fieldInfo = array();
$sql="SELECT * FROM $tableName LIMIT 1";
$result = mysql_query($sql,$this->_mysql_link_id);
$num_field=mysql_num_fields($result);
for($i=0;$i$field_name=mysql_field_name($result,$i);
$field_type=mysql_field_type($result,$i);
$fieldInfo[$field_name] = $field_type;
}
mysql_free_result($result);
return $fieldInfo;
}
function _quoteRow($fieldInfo,$row){
foreach ($row as $field_name=>$field_value){
$field_value=strval($field_value);
switch($fieldInfo[$field_name]){
case "blob": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;
case "string": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;
case "date": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;
case "datetime": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;
case "time": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;
case "unknown": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;
case "int": $row[$field_name] = intval($field_value); break;
case "real": $row[$field_name] = intval($field_value); break;
case "timestamp":$row[$field_name] = intval($field_value); break;
default: $row[$field_name] = intval($field_value); break;
}
}
return $row;
}
function _backupTable($tableName,$tableDir)
{
//取得表的字段类型:
$fieldInfo = $this->_getFieldInfo($tableName);
//step1:构造INSERT语句前半部分 并写入文件:
$fields = array_keys($fieldInfo);
$fields = implode(',',$fields);
$sqltext="INSERT INTO $tableName($fields)VALUES \r\n";
$datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';
(!$handle = fopen($datafile,'w')) && die("can not open file $datafile");
(!fwrite($handle, $sqltext)) && die("can not write data to file $datafile");
fclose($handle);
//step2:取得数据 并写入文件:
//取出表资源:
set_time_limit(0);
$sql = "select * from $tableName";
$result = mysql_query($sql,$this->_mysql_link_id);
//打开数据备份文件:$tableName.xml
$datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';
(!$handle = fopen($datafile,'a')) && die("can not open file $datafile");
//逐条取得表记录并写入文件:
while ($row = mysql_fetch_assoc($result)) {
$row = $this->_quoteRow($fieldInfo,$row);
$record='(' . implode(',',$row) . ");\r\n";
(!fwrite($handle, $record)) && die("can not write data to file $datafile");
}
mysql_free_result($result);
//关闭文件:
fclose($handle);
return true;
}
}
?>
备份mybbs数据库:
SQL代码
//example 1 backup:
require_once('DbBak.php');
require_once('TableBak.php');
$connectid = mysql_connect('localhost','root','123456');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
$DbBak->backupDb('mybbs');
恢复mybbs数据库:
require_once('DbBak.php');
require_once('TableBak.php');
$connectid = mysql_connect('localhost','root','123456');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
$DbBak->restoreDb('mybbs');

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver Mac版
视觉化网页开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。