Backup and Restore MySQL database in PHP

后端 未结 1 850
暖寄归人
暖寄归人 2021-01-01 05:48

I am trying to use PHP to backup and restore a MySQL database:

Backup:

$dbhost = \'localhost\';
$dbuser = \'root\';
$dbpass = \'dbpass\';
$dbname = \         


        
相关标签:
1条回答
  • 2021-01-01 06:23

    Script to backup using Php

    <?php
    define("BACKUP_PATH", "/home/abdul/");
    
    $server_name   = "localhost";
    $username      = "root";
    $password      = "root";
    $database_name = "world_copy";
    $date_string   = date("Ymd");
    
    $cmd = "mysqldump --routines -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
    
    exec($cmd);
    ?>
    

    Script to restore

    <?php
    
    $restore_file  = "/home/abdul/20140306_world_copy.sql";
    $server_name   = "localhost";
    $username      = "root";
    $password      = "root";
    $database_name = "test_world_copy";
    
    $cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} < $restore_file";
    exec($cmd);
    
    ?>
    
    0 讨论(0)
提交回复
热议问题