Chinaunix首页 | 论坛 | 博客
  • 博客访问: 289473
  • 博文数量: 176
  • 博客积分: 2516
  • 博客等级: 少校
  • 技术积分: 1350
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-01 11:18
文章分类
文章存档

2011年(1)

2010年(18)

2009年(157)

我的朋友

分类: Mysql/postgreSQL

2009-05-20 20:45:58

用PHP备份mysql数据库的方法
方法一:用SQL SELECT INTO OUTFILE备份
include 'config.php';
include 'opendb.php';
$tableName  = 'mypet';
$backupFile = 'backup/mypet.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);

include 'closedb.php';
?>
如果要恢复数据:
include 'config.php';
include 'opendb.php';
$tableName  = 'mypet';
$backupFile = 'backup/mypet.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);

include 'closedb.php';
?>
方法二:用系统命令system() mysqldump
include 'config.php';
include 'opendb.php';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);
include 'closedb.php';
?>

方法三:用PhpMyAdmin
进入PhpMyadmin点击导出选择数据库
本贴来自天极网群乐社区--
 
 
自己写的备份文件的功能模块
 
  1   2 $link = mysql_connect("localhost:3306", "root", "lili0921")
  3         or die("Could not connect : " . mysql_error());
  4     echo "Connected successfully";
  5 mysql_select_db("ykjd",$link) or die("Could not select database");
  6 $tableName  = 'products';
  7 $backupFile = $_POST[backupname];
  8 $query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
  9 $result = mysql_query($query);
 10 mysql_close($link);
 11
 12
 13 ?>
可以在/var/lib/mysql/ykjd中找到该文件
  1   2 $link = mysql_connect("localhost:3306", "root", "lili0921")
  3         or die("Could not connect : " . mysql_error());
  4 mysql_select_db("ykjd",$link) or die("Could not select database");
  5 $tableName  = 'products';
  6 $backupFile = $_POST[recovername];
  7 $query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
  8 $result = mysql_query($query);
  9
 10 mysql_close($link);
 11 ?>
 
 
阅读(527) | 评论(0) | 转发(0) |
0

上一篇:工程: mysql+php

下一篇:C 函数与程序结构

给主人留下些什么吧!~~