php定时备份数据库,导出sql文件

备份数据除了人为手动导出文件以外还可以定时自动导出,防止数据出现删除遗失等情况,定时自动备份大大节省了人力,也可修改代码用户端自己导出数据,用户端导出也分为sql文件,此操作便于管理员进行后期数据恢复,也可excel导出,此操作方便用户查看

<?php
//无论客户端是否关闭浏览器,下面的代码都将得到执行。
ignore_user_abort(true);set_time_limit(0);
//function write_txt(){
ini_set("max_execution_time", "180");//避免数据量过大,导出不全的情况出现。

$host="127.0.0.1";//数据库地址
$dbname="test";//这里配置数据库名
$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   =   mysql_connect($host,$username,$passw);//数据库主机,用户名,密码
   $db   =   mysql_select_db($dbname,$dbconn);
mysql_query("SET NAMES 'utf8'");
   $tables =mysql_list_tables($dbname,$dbconn);
   $num_tables   =   @mysql_numrows($tables);
   print   "-- filename=".$filename;
   while($i   <   $num_tables)
   {
     $table=mysql_tablename($tables,$i);
     print   $crlf;
  echo   get_table_structure($dbname,   $table,   $crlf).";$crlf$crlf";
  //echo   get_table_def($dbname,   $table,   $crlf).";$crlf$crlf";
  echo   get_table_content($dbname,   $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   =mysql_db_query($db,   "SHOW CREATE TABLE $table");
    $row=mysql_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   =   mysql_db_query($db,   "SELECT   *   FROM   $table");
          $i   =   0;
          while($row   =   mysql_fetch_row($result))
          {
                  $schema_insert   =   "INSERT INTO `$table` VALUES   (";
                  for($j=0;   $j<mysql_num_fields($result);$j++)
                  {
                          if(!isset($row[$j]))
                                  $schema_insert   .=   " NULL,";
                          elseif($row[$j]   !=   "")
                                  $schema_insert   .=   " '".addslashes($row[$j])."',";
                          else
                                  $schema_insert   .=   " '',";
                  }
                  $schema_insert   =   ereg_replace(",$", "",$schema_insert);
                  $schema_insert   .=   ");$crlf";
                  $temp   =   $temp.$schema_insert   ;
                  $i++;
          }
          return   $temp;
}

//usleep适用于php5.0以上,定时
function do_cron(){usleep(1);write_txt();}

while(1){do_cron();}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值