PHP备份MSQL源码
在网上经常见PHP备份MySQL的,但我由于项目需要要求PHP备份MSSQL!请个位指教
提问者:孟林 - 初学一级 所有回答(4)http://hi.baidu.com/phpyao/blog/item/94634d8208fe4197f703a618.html
http://download.csdn.net/source/809595
http://www.520home.net/newsHTML/516815688.html
我觉得这种东西你应该先试着去搜索引擎:
http://www.baidu.com/s?wd=php%B1%B8%B7%DD%CA%FD%BE%DD%BF%E2 (百度,关键词:PHP备份数据库)
1个月前 回答者:丁学 - 专家六级
没用过PHP,看看吧!
1个月前 回答者:子夜星辰 - 菜鸟二级
复制存为PHP文件直接使用,你搬弄是非中以搜"PHP版MySQL数据库备份工具
",这是我以前学PHP时用的.
<?php
require('session.php');
?>
<?php
# Name: PHP版MySQL数据库备份工具
# Author: 年华<[email protected]> [QQ:4908220]
# Homepage:http://master8.net/
session_start();
set_magic_quotes_runtime(0);
define('IN_BKUP', TRUE);
$magic_quotes_gpc = get_magic_quotes_gpc();
if(!$magic_quotes_gpc)
{
$_POST = daddslashes($_POST);
$_GET = daddslashes($_GET);
}
@extract($_POST,EXTR_OVERWRITE);
@extract($_GET,EXTR_OVERWRITE);
$PHP_REFERER = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if(!function_exists('file_put_contents'))
{
function file_put_contents($file, $string, $append = '')
{
$mode = $append == '' ? 'wb' : 'ab';
$fp = @fopen($file, $mode) or exit("Can not open file $file !");
flock($fp, LOCK_EX);
$stringlen = @fwrite($fp, $string);
flock($fp, LOCK_UN);
@fclose($fp);
return $stringlen;
}
}
include "./config.php";
$db = new db;
$db->connect($db_host, $db_user, $db_pw, $db_name, $db_pconnect);
if(!$_SESSION['admin']) $action = 'login';
switch($action)
{
case 'export':
if($dosubmit)
{
$fileid = isset($fileid) ? $fileid : 1;
if($fileid==1 && $tables)
{
if(!isset($tables) || !is_array($tables)) message('请选择要备份的数据表!');
$random = mt_rand(1000, 9999);
cache_write('bakup_tables.php', $tables);
}
else
{
if(!$tables = cache_read('bakup_tables.php')) message('请选择要备份的数据表!');
}
$sqldump = '';
$tableid = isset($tableid) ? $tableid - 1 : 0;
$startfrom = isset($startfrom) ? intval($startfrom) : 0;
$tablenumber = count($tables);
for($i = $tableid; $i < $tablenumber && strlen($sqldump) < $sizelimit * 1000; $i++)
{
$sqldump .= sql_dumptable($tables[$i], $startfrom, strlen($sqldump));
$startfrom = 0;
}
if(trim($sqldump))
{
$sqldump = "#Master8.NET Created\n# --------------------------------------------------------\n\n\n".$sqldump;
$tableid = $i;
$filename = $db_name.'_'.date('Ymd').'_'.$random.'_'.$fileid.'.sql';
$fileid++;
$bakfile = './data/'.$filename;
if(!is_writable('./data/')) message('数据无法备份到服务器!请检查 ./data 目录是否可写。', $forward);
file_put_contents($bakfile, $sqldump);
message("备份文件 $filename 写入成功!", '?action='.$action.'&sizelimit='.$sizelimit.'&tableid='.$tableid.'&fileid='.$fileid.'&startfrom='.$startrow.'&random='.$random.'&dosubmit=1');
}
else
{
cache_delete('bakup_tables.php');
message('数据库备份完毕!');
}
exit;
}
else
{
$size = $bktables = $bkresults = $results= array();
$k = 0;
$totalsize = 0;
$query = $db->query("SHOW TABLES FROM ".$db_name);
while($r = $db->fetch_row($query))
{
$tables[$k] = $r[0];
$count = $db->get_one("SELECT count(*) as number FROM $r[0] WHERE 1");
$results[$k] = $count['number'];
$bktables[$k] = $r[0];
$bkresults[$k] = $count['number'];
$q = $db->query("SHOW TABLE STATUS FROM `".$db_name."` LIKE '".$r[0]."'");
$s = $db->fetch_array($q);
$size[$k] = round($s['Data_length']/1024/1024, 2);
$totalsize += $size[$k];
$k++;
}
include template('export');
}
break;
case 'import':
if($dosubmit)
{
if($filename && fileext($filename)=='sql')
{
$filepath = './data/'.$filename;
if(!file_exists($filepath)) message("对不起, $filepath 不存在");
$sql = file_get_contents($filepath);
sql_execute($sql);
message("$filename 中的数据已经成功导入到数据库!");
}
else
{
$fileid = $fileid ? $fileid : 1;
$filename = $pre.$fileid.'.sql';
$filepath = './data/'.$filename;
if(file_exists($filepath))
{
$sql = file_get_contents($filepath);
sql_execute($sql);
$fileid++;
message("数据文件 $filename 导入成功!","?action=".$action."&pre=".$pre."&fileid=".$fileid."&dosubmit=1");
}
else
{
message("数据库恢复成功!");
}
}
}
else
{
$sqlfiles = glob('./data/*.sql');
if(is_array($sqlfiles))
{
$prepre = '';
$info = $infos = array();
foreach($sqlfiles as $id=>$sqlfile)
{
preg_match("/([a-z0-9_]+_[0-9]{8}_[0-9a-z]{4}_)([0-9]+)\.sql/i",basename($sqlfile),$num);
$info['filename'] = basename($sqlfile);
$info['filesize'] = round(filesize($sqlfile)/(1024*1024), 2);
$info['maketime'] = date('Y-m-d H:i:s', filemtime($sqlfile));
$info['pre'] = $num[1];
$info['number'] = $num[2];
if(!$id) $prebgcolor = '#E4EDF9';
if($info['pre'] == $prepre)
{
$info['bgcolor'] = $prebgcolor;
}
else
{
$info['bgcolor'] = $prebgcolor == '#E4EDF9' ? '#F1F3F5' : '#E4EDF9';
}
$prebgcolor = $info['bgcolor'];
$prepre = $info['pre'];
$infos[] = $info;
}
}
include template('import');
}
break;
case 'repair':
if($dosubmit)
{
$tables = is_array($tables) ? implode(',',$tables) : $tables;
if($operation == 'drop' && !$deletetable) message('删除数据表操作已禁用!', $PHP_REFERER);
if($tables && in_array($operation,array('repair','optimize','drop')) ) $db->query("$operation TABLE $tables");
message('数据表操作成功!', $PHP_REFERER);
}
else
{
$tables = array();
$query = $db->query("SHOW TABLES FROM ".$db_name);
while($r = $db->fetch_row($query))
{
$tables[] = $r[0];
}
include template('repair');
}
break;
case 'query':
if(!$runsql) message('运行语句功能已被管理员禁止',$PHP_REFERER);
if($dosubmit)
{
if($operation == 'file')
{
$savepath = './data/tmp_'.$uploadfile_name;
copy($uploadfile, $savepath);
$sql = file_get_contents($savepath);
@unlink($savepath);
}
else
{
$sql = stripslashes($sql);
}
if(trim($sql) != '') sql_execute($sql);
message('您的SQL语句已经成功运行了!', $forward);
}
else
{
include template('query');
}
break;
case 'uploadsql':
if(!$uploadsql) message('上传SQL功能已被管理员禁止',$PHP_REFERER);
if(fileext($uploadfile_name)!='sql') message('只允许上传sql格式文件',$PHP_REFERER);
$savepath = './data/'.$uploadfile_name;
if(copy($uploadfile, $savepath))
{
message('文件上传成功!', $PHP_REFERER);
}
else
{
message('文件上传失败!', $PHP_REFERER);
}
break;
case 'delete':
if(is_array($filenames))
{
foreach($filenames as $filename)
{
if(fileext($filename)=='sql')
{
@unlink('./data/'.$filename);
}
}
}
else
{
if(fileext($filenames)=='sql')
{
@unlink('./data/'.$filenames);
}
}
message('文件删除成功', $PHP_REFERER);
break;
case 'down':
$filename or message('文件名不能为空');
file_down('./data/'.$filename);
break;
case 'logout':
session_destroy();
message("成功退出系统", $PHP_REFERER);
break;
case 'phpinfo':
phpinfo();
exit;
break;
case 'login':
if($dosubmit)
{
if($pws == $pw)
{
$_SESSION['admin'] = true;
message("登陆成功,欢迎回来", $PHP_REFERER);
}
else
{
message("密码错误,登陆失败", $PHP_REFERER);
- 最新评论