dotj78335 2016-12-04 16:16
浏览 44
已采纳

如何在cron作业中执行php脚本

I want to execute a PHP script automatically every day that make a database backup and sent it to an email. Everything it good when I execute it in my web browser by calling its URL. But when I define a Cron job in my server, I got this error:

sh: /Viana-BackupMailer/mehravar_news_20161204_192302.sql.gz: No such file or directory mysqldump: Got errno 32 on write X-Powered-By: PHP/5.4.45 Content-type: text/html

I use this below command in cron job:

php /home/mehravar/public_html/Viana-BackupMailer.php

and this is my PHP codes:

<?php 
    $TimeZone = "Asia/Tehran";              // Your location time zone
    $SiteName = "Mysite.com";               // Your site name
    $SiteTitle = "My Site Title";           // Your site title
    $DeleteBackup = false;                  // Delete Backup file after send it by email

    $DB_Server = "localhost";               // MySQL server name
    $DB_Name = "DB_Name";                   // MySQL database name
    $DB_Username = "DB_User";               // MySQL database Username
    $DB_Password = "DB_Pass";               // MySQL database Password

    $Mail_To = "myemail@email.com";         // Receiver email
    $Mail_From = "sender@email.com";        // Sender email (Your host email)

    $LogText = "";
    $StorageDir = "Viana-BackupMailer";
    if(!is_dir($_SERVER['DOCUMENT_ROOT']."/".$StorageDir)){
        mkdir($_SERVER['DOCUMENT_ROOT']."/".$StorageDir, 0755);
    }

    date_default_timezone_set($TimeZone);
    $BackupDate = date("Ymd_His");
    $LogDate = date("Ym");
    $Path = $_SERVER["DOCUMENT_ROOT"]."/".$StorageDir."/";
    $LogFile = $Path.$LogDate."_Viana-BackupMailer.log";

    function WriteLog($LogText){
        $Handle = fopen($GLOBALS['LogFile'], (file_exists($GLOBALS['LogFile'])) ? 'a' : 'w');
        fwrite($Handle, "                                   Viana-BackupMailer Log File                                  
");
        fwrite($Handle, "================================================================================================
");
        fclose($Handle);
        file_put_contents($GLOBALS['LogFile'], $LogText, FILE_APPEND | LOCK_EX);
    }

    $DB_BackupFile = ($DB_Name == "" ? "all_databases" : $DB_Name)."_$BackupDate.sql.gz";
    $CMD = "mysqldump -u $DB_Username -h $DB_Server --password='$DB_Password' ".($DB_Name == "" ? "--all-databases" : $DB_Name)." | gzip > ".$Path.$DB_BackupFile;
    passthru($CMD);
    $LogText = date("[Y/m/d H:i:s] (").$DB_BackupFile.") Backup file was CREATED.";
    WriteLog($LogText);

    $Mail_Subject = "[$SiteName] Database Backup";
    $Mail_Message = "
Your database backup detail : 
";
    $Mail_Message .= "----------------------------------------------------------
";
    $Mail_Message .= "You Site Name : $SiteName
";
    $Mail_Message .= "Your Site Title : $SiteTitle
";
    $Mail_Message .= "Your Database Name : ".($DB_Name == "" ? "All Databases" : $DB_Name)."
";
    $Mail_Message .= "Your Backup File : $DB_BackupFile
";
    $Mail_Message .= "----------------------------------------------------------
";
    $Mail_Message .= "Powered by Yousef Rahimy Akhondzadeh

";
    $AttachFile = $Path.$DB_BackupFile;

    $AttachContent = file_get_contents($AttachFile);
    $AttachContent = chunk_split(base64_encode($AttachContent));
    $Separator = md5(time());
    $EOL = "
";

    $MailHeader = "From: $Mail_From".$EOL;
    $MailHeader .= "MIME-Version: 1.0".$EOL;
    $MailHeader .= "Content-Type: multipart/mixed; boundary=\"".$Separator."\"".$EOL;
    $MailHeader .= "Content-Transfer-Encoding: 7bit".$EOL;
    $MailHeader .= "This is a MIME encoded message.".$EOL;

    $MainBody = "--".$Separator.$EOL;
    $MainBody .= "Content-Type: text/plain; charset=\"iso-8859-1\"".$EOL;
    $MainBody .= "Content-Transfer-Encoding: 8bit".$EOL;
    $MainBody .= $Mail_Message.$EOL;
    $MainBody .= "--".$Separator.$EOL;
    $MainBody .= "Content-Type: application/octet-stream; name=\"".$DB_BackupFile."\"".$EOL;
    $MainBody .= "Content-Transfer-Encoding: base64".$EOL;
    $MainBody .= "Content-Disposition: attachment".$EOL;
    $MainBody .= $AttachContent.$EOL;
    $MainBody .= "--".$Separator."--";

    if (mail($Mail_To, $Mail_Subject, $MainBody, $MailHeader)){
        if ($DeleteBackup) {
            unlink($Path.$DB_BackupFile);
            $LogText = date("[Y/m/d H:i:s] (").$DB_BackupFile.") Backup file was REMOVED from disk.
";
            WriteLog($LogText);
        }
        $LogText = date("[Y/m/d H:i:s] (").$DB_BackupFile.") Backup file was SENT to email.
";
        WriteLog($LogText);
    } 
    else{
        $LogText = date("[Y/m/d H:i:s]")."Error on sending email.
";
        WriteLog($LogText);
        $LogText = date("[Y/m/d H:i:s]").error_get_last()."
";
        WriteLog($LogText);
    }
?>

Please help me. Thanks.

  • 写回答

1条回答 默认 最新

  • douluan8828 2016-12-04 16:23
    关注

    Can you share the code in Viana-BackupMailer? There are probably two reasons why it couldn't be executed (both with permissions):

    1. Access permissions - there are no rights to execute this php file by you/cron owner

    2. User who executes this php file doesn't have rights to access folder /Viana-BackupMailer/ or create there the file / OR to run mysqldump command

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题