淘主机论坛

 找回密码
 成为淘友

QQ登录

只需一步,快速开始

KT服务器促销中 100M带宽 10T流量 超值传送门:会员注册及发帖规则 发帖看过来
查看: 2327|回复: 1

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

[复制链接]
发表于 2012-4-24 14:21:52 | 显示全部楼层 |阅读模式
有时候,你的用户要求添加一个选项,导出整个数据库到一个SQL文件。虽然phpMyAdmin,以及Navicat有这个功能,但你的用户想要更简单点怎么办?

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

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

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

  2. $username = "root";
  3. $password = "";
  4. $hostname = "localhost";
  5. $dbname   = "cars";

  6. // if mysqldump is on the system path you do not need to specify the full path
  7. // simply use "mysqldump --add-drop-table ..." in this case
  8. $dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";
  9. $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
  10.         --user=$username ";
  11. if ($password)
  12.         $command.= "--password=". $password ." ";
  13.         $command.= $dbname;
  14.         $command.= " > " . $dumpfname;
  15.         system($command);
  16.           
  17.         // zip the dump file
  18.         $zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";
  19.         $zip = new ZipArchive();
  20.         if($zip->open($zipfname,ZIPARCHIVE::CREATE))
  21.         {
  22.            $zip->addFile($dumpfname,$dumpfname);
  23.            $zip->close();
  24.         }
  25.           
  26.         // read zip file and send it to standard output
  27.         if (file_exists($zipfname)) {
  28.             header('Content-Description: File Transfer');
  29.             header('Content-Type: application/octet-stream');
  30.             header('Content-Disposition: attachment; filename='.basename($zipfname));
  31.             flush();
  32.             readfile($zipfname);
  33.             exit;
  34. }
  35. ?>
复制代码
此代码不需要可写权限:
  1. <?php
  2. ob_start();

  3. $username = "root";
  4. $password = "";
  5. $hostname = "localhost";
  6. $dbname   = "cars";

  7. // if mysqldump is on the system path you do not need to specify the full path
  8. // simply use "mysqldump --add-drop-table ..." in this case
  9. $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
  10.     --user=$username ";
  11. if ($password)
  12.         $command.= "--password=". $password ." ";
  13. $command.= $dbname;
  14. system($command);
  15.    
  16. $dump = ob_get_contents();
  17. ob_end_clean();
  18.    
  19. // send dump file to the output
  20. header('Content-Description: File Transfer');
  21. header('Content-Type: application/octet-stream');
  22. header('Content-Disposition: attachment; filename='.basename($dbname . "_" .
  23.     date("Y-m-d_H-i-s").".sql"));
  24. flush();
  25. echo $dump;
  26. exit();]]>
  27. ?>
复制代码
 楼主| 发表于 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()等
您需要登录后才可以回帖 登录 | 成为淘友

本版积分规则

小黑屋|手机版|Archiver|淘主机

GMT+8, 2024-4-24 20:43

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表