用PHP备份mysql数据库的方法

用PHP备份mysql数据库的方法

方法一:用SQL SELECT INTO OUTFILE备份

<?php 
include 'config.php'; 
include 'opendb.php'; 
 
$tableName  = 'mypet'; 
$backupFile = 'backup/mypet.sql'; 
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName"; 
$result = mysql_query($query); 
 
 
include 'closedb.php'; 
?>

如果要恢复数据:

<?php 
include 'config.php'; 
include 'opendb.php'; 
 
$tableName  = 'mypet'; 
$backupFile = 'backup/mypet.sql'; 
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName"; 
$result = mysql_query($query); 
 
 
include 'closedb.php'; 
?>

方法二:用系统命令system() mysqldump

<?php 
include 'config.php'; 
include 'opendb.php'; 
 
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz'; 
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile"; 
system($command); 
 
include 'closedb.php'; 
?>

你可能感兴趣的:(sql,PHP,mysql)