淘主机 发表于 2012-4-24 14:21:52

如何一键导出MySQL数据库(附编码)

有时候,你的用户要求添加一个选项,导出整个数据库到一个SQL文件。虽然phpMyAdmin,以及Navicat有这个功能,但你的用户想要更简单点怎么办?

以下是如何一键导出MySQL数据库的php代码。

新建一个名为backup.php的文件,复制粘贴以下代码,然后编辑数据库连接设置和mysqldump的路径。有必要的话,你还可以添加一个backup.php超链接到你的程序里:<A href="backup.php">导出整个数据库</A>请注意,第一个php代码执行的时候,会导出zip压缩后的sql文件,所以此代码所在文件夹需要可写的权限。
如果你没有写的权限,请使用第二个php代码,缺点是导出的sql文件不会被zip压缩。

此代码需要可写权限:<?php

$username = "root";
$password = "";
$hostname = "localhost";
$dbname   = "cars";

// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";
$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
        --user=$username ";
if ($password)
        $command.= "--password=". $password ." ";
        $command.= $dbname;
        $command.= " > " . $dumpfname;
        system($command);
          
        // zip the dump file
        $zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";
        $zip = new ZipArchive();
        if($zip->open($zipfname,ZIPARCHIVE::CREATE))
        {
           $zip->addFile($dumpfname,$dumpfname);
           $zip->close();
        }
          
        // read zip file and send it to standard output
        if (file_exists($zipfname)) {
          header('Content-Description: File Transfer');
          header('Content-Type: application/octet-stream');
          header('Content-Disposition: attachment; filename='.basename($zipfname));
          flush();
          readfile($zipfname);
          exit;
}
?>
此代码不需要可写权限:<?php
ob_start();

$username = "root";
$password = "";
$hostname = "localhost";
$dbname   = "cars";

// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
    --user=$username ";
if ($password)
      $command.= "--password=". $password ." ";
$command.= $dbname;
system($command);
   
$dump = ob_get_contents();
ob_end_clean();
   
// send dump file to the output
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" .
    date("Y-m-d_H-i-s").".sql"));
flush();
echo $dump;
exit();]]>
?>

淘主机 发表于 2012-4-24 14:23:36

附上用批处理备份数据库的方法

windows下mysql自动定期备份并压缩/以discuz论坛为例

工作环境 Windows Server 2003 ,MySQL安装目录 D:\Program Files\MySQL\MySQL Server 5.0 , WinRAR 安装目录 C:\Program Files\WinRAR
备份数据存储的路径为 E:\dataBackup\MySQL .下面即是windows命令行批处理命令的源文件

代码如下:

set d=%date:~0,10%
set d=%d:-=%
set t=%time:~0,8%
set t=%t::=%
set dzxpath=dzx%p%%d%%t%.sql
set ucpath=uc%p%%d%%t%.sql
set folder=E:\dataBackup\MySQL\
echo off

"D:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe" -hlocalhost --opt -uroot -pyoumysqlrootpassword discuzx > "%folder%%dzxpath%"
"C:\Program Files\WinRAR\RAR.exe" a -ep1 -r -o+ -m5 -s -df "%folder%%dzxpath%".rar "%folder%%dzxpath%"
"D:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe" -hlocalhost --opt -uroot -pyoumysqlrootpassword discuzuc uc_admins uc_applications uc_badwords uc_domains uc_failedlogins uc_feeds uc_friends uc_mailqueue uc_memberfields uc_members uc_mergemembers uc_newpm uc_notelist uc_pms uc_protectedmembers uc_settings uc_sqlcache uc_tags uc_vars > "%folder%%ucpath%"
"C:\Program Files\WinRAR\RAR.exe" a -ep1 -r -o+ -m5 -s -df "%folder%%ucpath%".rar "%folder%%dzxpath%"
rem echo "D:\Program Files\MySQL\MySQL Server 5.0\data\discuzx\%filepath%"

"D:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe" -hlocalhost --opt -e --max_allowed_packet=1048576 --net_buffer_length=16384 -uroot -pyourrootpasswd discuzx > "%folder%%dzxpath%"
rem "C:\Program Files\WinRAR\RAR.exe" a -ep1 -r -o+ -m5 -s -df "%folder%%dzxpath%".rar "%folder%%dzxpath%"

"D:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe" -hlocalhost --opt -e --max_allowed_packet=1048576 --net_buffer_length=16384 -uroot -pyourrootpassw mydb > "%folder%%mydbpath%"

"C:\Program Files\WinRAR\RAR.exe" a -ep1 -r -o+ -m5 -s -df "%folder%%mydbpath%".rar "%folder%%mydbpath%""%folder%%dzxpath%"
rem echo "D:\Program Files\MySQL\MySQL Server 5.0\data\discuzx\%filepath%"
rem pause

----------------------

可以直接下载该.bat批处理文件: mysql_dump

这里需要使用winrar,假定安装在 C:\Program Files\WinRAR\ 上面代码里使用的是 rar.exe 这个命令行版的rar工具(推荐),而不是winrar.exe这个图形化版本。当然要检查你的winrar安装目录里是否有rar.exe这个文件,如果没有,建议重新下载完整版的winrar.

这里假定需要备份数据库discuzx与discuzuc中的几个表(ucenter表) [如果升级安装discuzx,而又没有把ucenter导入discuzx的库里,就是这种情况]

把上面的命令保存为 mysql_dump.bat ,双击即可运行。为了实现无人值守的自动化处理,可以通过系统的任务计划定期执行这个命令。

如不会windows任务计划请自行搜索学习,这里恕不赘述。



这也是windows下的备份方案,linux下类似,使用mysqldump xxxx |gzip -c >bakup.sql.gz 这样的形式更好。

正如楼上所说,shell脚本备份更好,效率高,不易出错。而使用php执行,可靠性低了点,尤其数据库比较大时,比如1G的数据库,使用php备份,多半是要超时而失败的。

另外,php的一些函数一般来说最好禁用,不然容易造成安全隐患,如system(), exec()等
页: [1]
查看完整版本: 如何一键导出MySQL数据库(附编码)