|
数据库备份是必要的一般都是使用mysqldump进行备份,我这边写了两种备份方法可以参考一下。
第一种:使用mysqldump结合exec函数进行数据库备份操作。
<span style="color: #008080">header('Content-Type:text/html;charset=utf8'<span style="color: #000000">);
<span style="color: #008080">ini_set("max_execution_time","0");<span style="color: #008000">//<span style="color: #008000">代码运行时间不限制 防止备份失败
<span style="color: #008080">ini_set('memory_limit','128M');<span style="color: #008000">//<span style="color: #008000">设置内存 根据需求可以修改
date_default_timezone_set("PRC"<span style="color: #000000">);
<span style="color: #008000">//<span style="color: #008000">创建需要保存sql文件的文件夹
<span style="color: #800080">$path = 'D:SQLdatabse_backup'<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">定义数据库配置
<span style="color: #800080">$user = ''; <span style="color: #008000">//<span style="color: #008000">数据库账户
<span style="color: #800080">$pwd = ''; <span style="color: #008000">//<span style="color: #008000">数据库密码
<span style="color: #800080">$dbname = ''; <span style="color: #008000">//<span style="color: #008000">数据库名称
//备份数据库命令地址文件
<span style="color: #800080">$sqladdress = 'D:phpStudyMySQLbinmysqldump.exe'<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">备份指定地址
<span style="color: #800080">$time = <span style="color: #008080">time<span style="color: #000000">();
<span style="color: #800080">$path = 'D:SQLdatabse_backup'.''.<span style="color: #008080">date("Ymd",<span style="color: #800080">$time<span style="color: #000000">);
<span style="color: #0000ff">if(!<span style="color: #008080">file_exists(<span style="color: #800080">$path<span style="color: #000000">))
{
<span style="color: #008080">mkdir(<span style="color: #800080">$path,0777,<span style="color: #0000ff">true<span style="color: #000000">);
}
<span style="color: #008000">//<span style="color: #008000">备份的数据库文件名
<span style="color: #800080">$sqlFile = <span style="color: #800080">$dbname."_%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%.sql"<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">判断是否存在密码
<span style="color: #800080">$password = <span style="color: #800080">$pwd== ''?'':' -p'.<span style="color: #800080">$pwd<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">拼接备份命令
<span style="color: #800080">$order = <span style="color: #800080">$sqladdress.' --opt -u'.<span style="color: #800080">$user.<span style="color: #800080">$password.' '.<span style="color: #800080">$dbname.' >'.<span style="color: #800080">$path.''.<span style="color: #800080">$sqlFile<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">执行命令
<span style="color: #008080">exec(<span style="color: #800080">$order);
我使用的是集成的phpstudy里面的mysql下面的mysqldump.exe来备份,备份的数据库名字写法是 数据库名字+年月日时. 上面代码中我对数据库密码进行了判断,我这边有些数据库是不需要密码的。最后使用exec执行命令。
第二种:使用php+mysql+header函数进行数据库备份和下载操作。
('Content-Type:text/html;charset=utf8'("max_execution_time",'1024M');
date_default_timezone_set("PRC"("Content-Type:text/html;charset=utf-8"=""="";
="";
="";
= ("","","",""(,= "set charset utf8;rn";=5.0
(,"SET NAMES 'UTF8'"=(,"show tables"(=(=[0=(,"show create table ``"=(.=['Create Table'].";rnrn";
=(,"select * from ``"(=(=(=('addslashes',=('`,`',="`".."`"=(=('addslashes',=("','",="'".."'".="insert into ``() values();rn"(.="rn"((=('Ymj').".sql";
= = 'D:SQLdatabse_backup'.''.("Ymd",).''
(!(
(, = . = (,'w'(,(=(,"r"=(("Content-type: application/octet-stream"("Accept-Ranges: bytes"("Accept-Length: ".("Content-Disposition: attachment; filename=".
(,(;
个人建议用第一种,第二种太消耗内存了。
第一种可以做成定时备份,windows下可以用定时任务。
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|