backup.php,Manage_backup.php

这段代码实现了一个即使在客户端浏览器关闭的情况下也能继续执行的功能,用于数据库的完整备份。它首先设置忽略用户中断,然后无限循环执行定时任务,生成包含数据库结构和内容的SQL文件。通过设置最大执行时间和避免数据丢失的措施,确保了备份的完整性和效率。
摘要由CSDN通过智能技术生成

//无论客户端是否关闭浏览器,下面的代码都将得到执行。

ignore_user_abort(true);

set_time_limit(0);

//function write_txt(){

ini_set("max_execution_time", "180");

//避免数据量过大,导出不全的情况出现。

$host="127.0.0.1";

//数据库地址

$dbname="EQMIS";

//这里配置数据库名

$username="root";

//用户名

$passw="";

//这里配置密码

$filename=date("Y-m-d_H-i-s")."-".$dbname.".sql";

header("Content-disposition:filename=".$filename);

//所保存的文件名

header("Content-type:application/octetstream");

header("Pragma:no-cache");

header("Expires:0");

//备份数据

$i = 0;

$crlf="\r\n";

global $dbconn;

$dbconn = mysqli_connect($host,$username,$passw);

//数据库主机,用户名,密码

$db = mysqli_select_db($dbconn,$dbname);

$dbconn->query("SET NAMES 'utf8'");

mysqli_set_charset($dbconn, 'utf-8');

$tables = mysqli_query($dbconn,"SHOW TABLES FROM $dbname");

//$tables = mysqli_query($dbconn,$dbname);

$num_tables = mysqli_num_rows($tables);

print "-- filename=".$filename;

while($i < $num_tables) {

$table = mysqli_fetch_assoc($tables);

print $crlf;

echo get_table_structure($dbconn, $table, $crlf) . ";$crlf$crlf";

//echo get_table_def($dbname, $table, $crlf) . ";$crlf$crlf";

echo get_table_content($dbconn, $table, $crlf);

$i++;

}

function get_table_structure($db,$table,$crlf){

global $drop;

$schema_create = "";

if(!empty($drop)){

$schema_create .= "DROP TABLE IF EXISTS `$table`;$crlf";

}

$result = mysqli_query($db,"SHOW CREATE TABLE $table");

$row=mysqli_fetch_array($result);

$schema_create .= $crlf."-- ".$row[0].$crlf;

$schema_create .= $row[1].$crlf;

Return $schema_create;

}

//获得表内容

function get_table_content($db, $table, $crlf){

$schema_create = "";

$temp = "";

$result = mysqli_query($db, "SELECT * FROM $table");

$i = 0;

while($row = mysqli_fetch_row($result)) {

$schema_insert = "INSERT INTO `$table` VALUES (";

for($j=0; $j

if(!isset($row[$j]))

$schema_insert .= " NULL,";

elseif($row[$j] != "")

$schema_insert .= " '".addslashes($row[$j])."',";

else $schema_insert .= " '',";

}

$schema_insert = preg_replace(",$", "",$schema_insert);

$schema_insert .= ");$crlf";

$temp = $temp.$schema_insert ;

$i++;

}

return $temp;

}

//usleep适用于php5.0以上,定时

function do_cron(){

usleep(10000);

write_txt();

}

while(1){

do_cron();

}

?>

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值