Home > Database > SQL > body text

What are the two methods of database backup?

步履不停
Release: 2020-09-12 16:34:30
Original
32142 people have browsed it

数据库备份的两种方法是:1、使用mysqldump结合exec函数进行数据库备份;2、使用【php+mysql+header】函数进行数据库备份。

What are the two methods of database backup?

数据库备份是必要的一般都是使用mysqldump进行备份,我这边写了两种备份方法可以参考一下。

第一种:使用mysqldump结合exec函数进行数据库备份操作。

代码如下:

/**
 * Subject: php-mysql 实现数据库备份.
 * User: luokakale
 * Date: 2018/11/9
 * Time: 13:31
 */

header('Content-Type:text/html;charset=utf8');
ini_set("max_execution_time", "0");//代码运行时间不限制  防止备份失败
ini_set('memory_limit', '128M');//设置内存 根据需求可以修改
date_default_timezone_set("PRC");
//创建需要保存sql文件的文件夹
$path = 'D:\SQL\databse_backup';
//定义数据库配置
$user = ''; //数据库账户
$pwd  = ''; //数据库密码
$dbname = ''; //数据库名称

//备份数据库命令地址文件
$sqladdress = 'D:\phpStudy\MySQL\bin\mysqldump.exe';

//备份指定地址
$time = time();
$path = 'D:\SQL\databse_backup'.'\\'.date("Ymd",$time);
if(!file_exists($path))
{
    mkdir($path,0777,true);
}

//备份的数据库文件名
$sqlFile = $dbname."_%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%.sql";
//判断是否存在密码
$password = $pwd== ''?'':'  -p'.$pwd;
//拼接备份命令
$order =  $sqladdress.' --opt  -u'.$user.$password.' '.$dbname.' >'.$path.'\\'.$sqlFile;
//执行命令
exec($order);
Copy after login

我使用的是集成的phpstudy里面的mysql下面的mysqldump.exe来备份,备份的数据库名字写法是 数据库名字+年月日时. 上面代码中我对数据库密码进行了判断,我这边有些数据库是不需要密码的。最后使用exec执行命令。

第二种:使用php+mysql+header函数进行数据库备份和下载操作。

代码如下:

header('Content-Type:text/html;charset=utf8');
ini_set("max_execution_time", "0");//代码运行时间不限制  防止备份失败
ini_set('memory_limit', '1024M');//设置内存 根据需求可以修改
date_default_timezone_set("PRC");
header("Content-Type:text/html;charset=utf-8");
$host="";
$user="";//账户
$password="";//密码
$dbname="";//数据库名称
$con =  mysqli_connect("$host","$user","$password","$dbname");
mysqli_select_db($con,$dbname);
$mysql= "set charset utf8;\r\n";#for mysql>=5.0
mysqli_query($con,"SET NAMES 'UTF8'");
$q1=mysqli_query($con,"show tables");
while($t=mysqli_fetch_array($q1)){
    $table=$t[0];
    $q2=mysqli_query($con,"show create table `$table`");
    $sql=mysqli_fetch_array($q2);
    $mysql.=$sql['Create Table'].";\r\n\r\n";#DDL
    $q3=mysqli_query($con,"select * from `$table`");
    while($data=mysqli_fetch_assoc($q3))
    {
        $keys=array_keys($data);
        $keys=array_map('addslashes',$keys);
        $keys=join('`,`',$keys);
        $keys="`".$keys."`";
        $vals=array_values($data);
        $vals=array_map('addslashes',$vals);
        $vals=join("','",$vals);
        $vals="'".$vals."'";
        $mysql.="insert into `$table`($keys) values($vals);\r\n";
        unset($data);
    }
    $mysql.="\r\n";
    unset($t);
}
mysqli_close($con);
$filename=date('Ymj').".sql"; //文件名为当天的日期
$time = time();
$path = 'D:\SQL\databse_backup'.'\\'.date("Ymd",$time).'\\';
// 检查目录是否存在
if(!is_dir($path)){
    // 新建目录
    mkdir($path, 0777, true);
}
$file_name = $path.$filename;
$fp = fopen($file_name,'w');
fputs($fp,$mysql);
fclose($fp);
$fp=fopen($file_name,"r");
$file_size=filesize($file_name);
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".$file_size);
header("Content-Disposition: attachment; filename=".$filename);
//这里一定要使用echo 进行输出,否则下载的文家是空白的
echo fread($fp,$file_size);
fclose($fp);
exit;
Copy after login

个人建议用第一种,第二种太消耗内存了。

第一种可以做成定时备份,windows下可以用定时任务。

更多SQL的相关技术文章,请访问SQL教程栏目进行学习!

The above is the detailed content of What are the two methods of database backup?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!