示例备份脚本

时间:2012-04-23 16:12:08

标签: php mysql

尝试运行简单的备份脚本,它只打印错误消息。有什么我做错了吗?这是完整的代码。我在linux上并且可以完全访问数据库。

它只打印'系统出现问题导致备份无法完成'

<?php
$dbhost = "localhost"; // usually localhost
$dbuser = "myuser";
$dbpass = "mypass";
$dbname = "mydb";
$sendto = "Webmaster <webmaster@mydomain.com>";
$sendfrom = "Automated Backup <info@mydomain.com>";
$sendsubject = "Daily Mysql Backup";
$bodyofemail = "Here is the daily backup.";


$headers = 'My Shop <myname@mydomain.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";

$backupfile = $dbname . date("Y-m-d") . '.sql';

if(system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile")){
   mail('receiver@yahoo.com','Back Up Service','Back Up successfully completed',$headers );
      echo'Back up saved successfully.';
  }else {
    echo'There was an issue with your system that prevented the backup from completing';
   }
?>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的命令会出现问题:

mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile

这看起来不错,但是值得将它打印出来,以便您可以在命令行中查看它以查看结果。

编辑: 查看此处提供的系统文档:system它看起来像返回命令输出的最后一行。如果mysqldump成功运行但没有创建任何输出,则if将返回false。使用:

if(system("mysqldump blah blah") === false){

或使用return_var参数来检查状态代码(成功时应为0)。