用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) |