本文介绍了在PHP中备份和还原MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用PHP备份和还原MySQL数据库:
I am trying to use PHP to backup and restore a MySQL database:
备份:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/backup/test.sql";
exec("D:/xampp/mysql/bin/mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname > $output");
echo "Backup complete!";
还原:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/restore/test.sql";
exec("D:/xampp/mysql/bin/mysql --opt -h $dbhost -u $dbuser -p $dbpass $dbname < $output");
echo "Restore complete!";
但是两者都不起作用.备份完成后,我将检查test.sql
文件是否为空白.还原完成后,数据库仍为空白.
But both are not working. When Backup is complete then I check test.sql
file that is blank. When the restore is complete, the database is still blank.
我该如何解决?
推荐答案
使用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);
?>
要还原的脚本
<?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);
?>
这篇关于在PHP中备份和还原MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!