天天看點

前台一鍵備份資料庫+PHP實作方式一、實作思路

一、實作思路

1、單擊備份按鈕傳遞參數到背景,ajax實作:

function backupDatabase(){
  var back = 'backupDatabase';
  $.ajax({
    url:'system_backup.php?do=backupDatabase',
    type:'POST',
    data:back,
    dataType:'json',
    beforeSend:function(){
      interval=window.setInterval(function(){
        var text = $("#backupstate2").html();
        if(text.length<){
          $("#backupstate2").html('資料庫備份中.');  
        }
        var text = $("#backupstate2").html();
        if (text.length < ){
          $("#backupstate2").html(text + '.');                    
        } else {
          $("#backupstate2").html('資料庫備份中.');             
        }
      }, );
    },
    success:function(data){
      if(data.msg=='1'){
        $("#backupstate2").html('資料備份完成!'); 
        alert('備份成功.點選确定後,請立刻單擊下載下傳備份資料庫!');
        $("#db_url").val(data.url);
        window.clearInterval(interval);
      }else{
        alert(data.msg); 
        $("#backupstate2").html(''); 
         window.clearInterval(interval);
      }
    }
  });
}
           

2、背景接受參數後進行資料庫備份

header("Content-Type: text/html; charset=utf-8");
require_once('../configs/config.php');
require_once("../models/websurvey/webSurvey.class.php");
SysUser::authIsLogin("../login.php");
$user = new SysUser();
if(!empty($_GET['do'])){
    $do = $_GET['do'];
    switch($do){
        case "backupDatabase":
            $data = $_POST['back'];
            if('backupDatabase' == $data){
                // 設定SQL檔案儲存檔案名 
                $cfg_dbname = 'asteriskcdrdb';
                $filename=date("Ymd",time())."-".$cfg_dbname.".sql"; 

                // 擷取目前頁面檔案路徑,SQL檔案就導出到指定檔案夾内
                // $savePath = './Public/upload/DB/';
                $savePath = '../upload/DB/';

                if(!file_exists($savePath)){
                    mkdir($savePath,,true);
                }
                $tmpFile = $savePath.$filename;
                fopen($tmpFile, "r");
                chmod($tmpFile,);

                /* //删除之前備份的資料
                $dh=opendir($savePath); 
                if($dh){
                    while ($file=readdir($dh)) { 
                        if($file!="." && $file!="..") { 
                            $fullpath=$savePath."/".$file; 
                            if(!is_dir($fullpath)) { 
                                unlink($fullpath); 
                            }
                        } 
                    } 
                    closedir($dh);
                } //删除之前備份的資料*/

                // 用MySQLDump指令導出資料庫
                $dbhost = '***.***.***.***';//主機IP位址
                $cfg_dbuser = 'root';//使用者名
                $cfg_dbpwd = '******';//密碼
                $bool_dump = exec("mysqldump -h$dbhost -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname > ".$tmpFile);
                if( == $bool_dump){
                    $bool_tar = exec("tar -zcvPf /var/spool/asterisk/monitor/system/backup/db.tar /var/spool/asterisk/monitor/upload/DB/$filename");
                    if( == $bool_tar){
                        $arr = json_encode(array('msg'=>,'url'=>$filename));
                        echo $arr;
                    }else{
                        $arr = json_encode(array('msg'=>'備份失敗,請聯系管理者!'));
                        echo $arr;
                    }
                }
            }
            exit;
        }
        default:
            break;
    }
}
           

三、效果圖:

1、備份前

前台一鍵備份資料庫+PHP實作方式一、實作思路

2、備份中

前台一鍵備份資料庫+PHP實作方式一、實作思路

3、備份成功

前台一鍵備份資料庫+PHP實作方式一、實作思路

4、下載下傳備份資料庫

前台一鍵備份資料庫+PHP實作方式一、實作思路

四、注意

1、注意:不同的架構實作的方式不同,但都是大同小異。

2、注意php的exec()函數,執行成功後的傳回值是0。

3、除了exec()外,還有system()、passthru()、shell_exec()、執行運算符:反引号(“)等等,根據自己的需要進行選擇。