Front desk one key backup database + PHP implementation

1, Realization ideas

1. Click the Backup button to transfer the parameters to the background. ajax implements:

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<4){
          $("#backupstate2").html('Database backup in progress.');  
        }
        var text = $("#backupstate2").html();
        if (text.length < 9){
          $("#backupstate2").html(text + '.');                    
        } else {
          $("#backupstate2").html('Database backup in progress.');             
        }
      }, 200);
    },
    success:function(data){
      if(data.msg=='1'){
        $("#backupstate2").html('Data backup completed!'); 
        alert('Backup successful.Click OK to download the backup database immediately!');
        $("#db_url").val(data.url);
        window.clearInterval(interval);
      }else{
        alert(data.msg); 
        $("#backupstate2").html(''); 
         window.clearInterval(interval);
      }
    }
  });
}

2. Database backup after parameters are accepted in the background

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){
                // Set SQL file save file name 
                $cfg_dbname = 'asteriskcdrdb';
                $filename=date("Ymd",time())."-".$cfg_dbname.".sql"; 

                // Get the current page file path, and the SQL file will be exported to the specified folder
                // $savePath = './Public/upload/DB/';
                $savePath = '../upload/DB/';

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

                /* //Delete previously backed up data
                $dh=opendir($savePath); 
                if($dh){
                    while ($file=readdir($dh)) { 
                        if($file!="." && $file!="..") { 
                            $fullpath=$savePath."/".$file; 
                            if(!is_dir($fullpath)) { 
                                unlink($fullpath); 
                            }
                        } 
                    } 
                    closedir($dh);
                } //Delete previously backed up data*/

                // Export database with MySQL dump command
                $dbhost = '***.***.***.***';//Host IP address
                $cfg_dbuser = 'root';//user name
                $cfg_dbpwd = '******';//Password
                $bool_dump = exec("mysqldump -h$dbhost -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname > ".$tmpFile);
                if(0 == $bool_dump){
                    $bool_tar = exec("tar -zcvPf /var/spool/asterisk/monitor/system/backup/db.tar /var/spool/asterisk/monitor/upload/DB/$filename");
                    if(0 == $bool_tar){
                        $arr = json_encode(array('msg'=>1,'url'=>$filename));
                        echo $arr;
                    }else{
                        $arr = json_encode(array('msg'=>'Backup failed, please contact the administrator!'));
                        echo $arr;
                    }
                }
            }
            exit;
        }
        default:
            break;
    }
}

3, Renderings:

1. Before backup

2. Backup in progress

3. Backup successful

4. Download backup database

4, Attention

1. Note: different frameworks are implemented in different ways, but they are all the same.
2. Note that the exec() function of php returns 0 after successful execution.
3. In addition to exec(), there are system(), passthru(), shell exec(), execution operators: backquotes ("), and so on, which can be selected according to your own needs.

Tags: Database PHP SQL Asterisk

Posted on Tue, 05 May 2020 16:40:58 -0400 by Festy