php备份mysql数据库、表

环境

PHP 5.4.45
MySQL 5.6.29 标准版

代码

1、需要将本文件拷贝到具体项目中才使用
2、需要修改php.ini中的disable_functions,使得exec函数能正常使用

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库备份</title>
<style>
body,td,th {font-family:"宋体"; font-size:12px;}
table{border-collapse:collapse;border:1px solid #CCC;background:#efefef;}
table caption{text-align:left; background-color:#fff; line-height:2em; font-size:14px; font-weight:bold; }
table th{text-align:left; font-weight:bold;height:26px; line-height:26px; font-size:12px; border:1px solid #CCC;}
table td{height:20px; font-size:12px; border:1px solid #CCC;background-color:#fff;}
.c1{ width: 120px;}
.c2{ width: 120px;}
.c3{ width: 70px;}
.c4{ width: 80px;}
.c5{ width: 80px;}
.c6{ width: 270px;}
ul{
	width:327px;
	padding:0px;
	margin:0 auto;
}
ul li{
	list-style:none;
	margin-bottom:10px;
	text-align:right;
	font-size:18px;
	width:440px;
}
li input{
	height:35px;
	width:220px;
}
</style>
</head>
<body>
<form action="" name="form" id="form">
<ul>
<li>
数据库地址:<input name="dblocation" value="<?php if(empty($_GET['dblocation'])){echo "ww.zztdrj.com";}else{echo $_GET['dblocation'];}?>"/>
</li>
<li>
端口号:<input name="dbport" value="<?php if(empty($_GET['dbport'])){}else{echo $_GET['dbport'];}?>"/>
</li>
<li>
用户名:<input name="dbuser" value="<?php if(empty($_GET['dbuser'])){}else{echo $_GET['dbuser'];}?>"/>
</li>
<li>
密码:<input name="dbpwd" value="<?php if(empty($_GET['dbpwd'])){}else{echo $_GET['dbpwd'];}?>"/>
</li>
<li>
数据库名:<input name="dbname" value="<?php if(empty($_GET['dbname'])){e}else{echo $_GET['dbname'];}?>"/>
</li>
<li>
mysqldump.exe所在目录:<input name="mysqldump" value="<?php if(empty($_GET['mysqldump'])){}else{echo $_GET['mysqldump'];}?>"/>
</li>
<li>
备份数据库文件存放目录:<input name="bakPath" value="<?php if(empty($_GET['bakPath'])){}else{echo $_GET['bakPath'];}?>"/>
</li>
<li style="text-align:center">
<input style="width: auto;" type="submit" value="备份数据库"></li>
</ul>
</form>
<?php
if(isset($_GET)){
	$b=0;
	foreach($_GET as $k=>$v){
		if(!(empty($v))){
			$b++;
		}
	}
	if($b==7){
	     //配置数据库
	    $dbname=$_GET['dbname'];
		$sqlPathName=$_GET['bakPath'].'/'.$dbname.".sql";
		/*
		while(is_file($sqlPathName)){
		    $dbname=$dbname.'01';
		    $sqlPathName==$_GET['bakPath'].'/'.$dbname.".sql";
		}
		*/
        $exec=$_GET['mysqldump']."/mysqldump -h ".$_GET['dblocation']." -u".$_GET['dbuser']." -p".$_GET['dbpwd']." -P".$_GET['dbport']." ".$_GET['dbname'].">".$sqlPathName;
        exec($exec,$output,$i);
	}
    
}

运行结果

php备份mysql数据库、表