【问题标题】:How to send MySQL backup to email with cronjob如何使用 cronjob 将 MySQL 备份发送到电子邮件
【发布时间】:2014-07-13 12:59:29
【问题描述】:

我正在尝试使用 cronjob 从 cPanel 自动备份我的数据库。我想在 cronjob 运行时将数据库发送到我的电子邮件地址,并且我已经编写了代码(如下),但它仍然无法正常工作。

mysqldump -e --user=username --password='password' dbname | gzip | uuencode sql_backup.gz | mail example@example.com

在运行 cronjob 时,我在我的电子邮件中收到以下消息:

/usr/local/cpanel/bin/jailshell: mail: command not found
mysqldump: Got errno 32 on write

我一直在参考这篇文章:Automatic MySQL Backup

希望你能理解我的问题并帮助我。

我也尝试过 curl 但仍然无法正常工作。您可以查看我遵循的步骤。

第一步:创建mail_alert.sh 文件并添加以下代码。

#!/bin/bash
curl --url "smtps://smtp.gmail.com:465" --ssl-reqd \
  --mail-from "example@gmail.com" --mail-rcpt "example@gmail.com" \
  --upload-file mail.txt --user "example@gmail.com:mypassword" --insecure

第二步:创建mail.txt并添加以下代码。

From: "Name" example@gmail.com
To: "Name" example@gmail.com
Subject: Backup completed

The backup has been completed.

第三步:在命令行中添加代码。

mysqldump -e --user=username --password='password' dbname | gzip | uuencode sql_backup.gz | sh public_html/sql/mail_alert.sh

在此之后,我在邮件中收到了这条消息。

curl: option --ssl-reqd: is unknown
curl: try 'curl --help' or 'curl --manual' for more information
mysqldump: Got errno 32 on write

【问题讨论】:

    标签: mysql cron mysqldump


    【解决方案1】:

    您似乎无法使用或未安装mail

    另一个需要考虑的选项是使用 curl 发送电子邮件,如下所述:https://stackoverflow.com/a/16069786/280842

    您可以使用上面链接中的代码来实现这一点:

    mail_alert.sh文件内容

    #!/bin/bash
    curl --url "smtps://smtp.gmail.com:465" --ssl-reqd \
      --mail-from "username@gmail.com" --mail-rcpt "john@example.com" \
      --upload-file mail.txt --user "username@gmail.com:password" --insecure
    

    mail.txt文件内容

    From: "User Name" <username@gmail.com>
    To: "John Smith" <john@example.com>
    Subject: Backup completed
    
    The backup has been completed.
    

    通过考试被认为是一种糟糕的安全做法 通过命令行参数的帐户凭据。这 以上示例仅用于演示目的。

    然后将新创建的脚本添加到现有的 cron 作业中

    mysqldump -e --user=username --password='password' dbname | gzip | uuencode sql_backup.gz | sh /home/myuser/mail_alert.sh
    

    【讨论】:

    • 嗨,我已经更新了我的问题,如果我错了,你能检查我的步骤并纠正我吗?
    • 没有--ssl-reqd 对我有用,所以删除它并重试
    • 你的意思是这样的:#!/bin/bash curl --url "smtps://smtp.gmail.com:465" \ --mail-from "example@gmail.com" --mail-rcpt "example@gmail.com" \ --upload-file mail.txt --user "example@gmail.com:mypassword" --insecure
    【解决方案2】:

    好的,我将向您展示如何创建一个 php 脚本,该脚本在没有 phpMyAdmin 的情况下备份 MySQL 数据库,然后将 .sql 文件附加到电子邮件中。

    今天我需要创建一个小脚本来备份数据库,然后通过电子邮件发送。我发现最好的方法是使用 mysqldump 程序。通常,即使在经销商托管包上,您也有权运行此程序。

    在 linux 中,程序通常位于 代码:

    /usr/bin/mysqldump
    

    好的,让我们开始吧。 首先,我们需要设置变量,包括 MySQL 凭据、要发送到的电子邮件地址、存储 sql 文件的路径、mysqldump 程序的绝对路径。

    代码:

    ini_set("memory_limit","250M"); // We don't want any nasty memory error messages
    
    $SendTo[] = 'myemailaddress@thephpanswers.com'; // This is your email address, you can copy this line and add another recipient 
    
    $path = '/home/website/public_html/backupSQL/sql/' // This is the absolute path to where we are going to save the .sql files - please note you should place a .htaccess to deny any users browsing the directory
    
    $tmpFilename = time() .'_mysql.sql'; // This is the tmp filename for the sql, needs to be different everytime
    
    define('mysqlUser','mysqlusername'); // This is the username for the MySQL database
    define('mysqlPass','mysqlpassword'); // Password for the username
    define('mysqlDatabase','mysqldatabase'); // The database you wish to backup
    define('mysqldump','/usr/bin/mysqldump'); // The absolute path to the mysqldump program
    

    使用mysqldump备份MySQL数据库: mysqldump 非常易于使用,更多信息请访问:http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html 现在我们只需添加 shell_exec 来告诉 mysqldump 备份数据库。

    代码:

    shell_exec(mysqldump . ' -u ' . mysqlUser .' -p' . mysqlPass .' ' . mysqlDatabase .' > ' . $path .$tmpFilename); // See the > $path . $tmpFilename these are populated from the variables you set above
    

    您现在可以运行此脚本,看看它是否真的在您指定的文件夹中创建了 .sql 文件。

    在 PHP 中发送附件 好的,所以我们知道我们的文件位于 $path 。 $tmpFilename 所以让我们继续复杂的电子邮件发送附件。

    代码:

    $from = "Backup <backup@domain.co.uk>"; // Who the email is coming from
    $subject = 'MySQL Database backup'; // The subject of the email
    $absoluteFile = $path . $tmpFilename; // Keep it simple, creates a variable of where the file is
    $fileType = 'text/plain'; // Content type
    $mailBodyText = '<h1>MySQL Database attached</h1>'; // Our HTML body of the email
    
    $mineBoundaryStr=md5(time()); // Needs to be random for the mime
    // Advanced headers from http://xahlee.org/php/send_mail_attachment.html
    $headers= <<<EEEEEEEEEEEEEE
    From: $from
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary="$mineBoundaryStr"
    
    EEEEEEEEEEEEEE;
    $mailBodyEncodedText = <<<TTTTTTTTTTTTTTTTT
    This is a multi-part message in MIME format.
    
    --{$mineBoundaryStr}
    Content-Type: text/html; charset=UTF-8
    Content-Transfer-Encoding: quoted-printable
    
    $mailBodyText
    
    TTTTTTTTTTTTTTTTT;
    
    $file = fopen($absoluteFile,'rb'); 
    $data = fread($file,filesize($absoluteFile)); 
    fclose($file);
    $data = chunk_split(base64_encode($data));
    
    $mailBodyEncodedText .= <<<FFFFFFFFFFFFFFFFFFFFF
    --$mineBoundaryStr
    Content-Type: $fileType;
    name=$tmpFilename
    Content-Disposition: attachment;
    filename="$tmpFilename"
    Content-Transfer-Encoding: base64
    
    $data
    
    --$mineBoundaryStr--
    
    FFFFFFFFFFFFFFFFFFFFF;
    
    foreach($SendTo as $k => $v) { // Loop through all our recipients
          mail( $v , date("H:i - jS \of F Y") . 'MySQL Database backup' , $mailBodyEncodedText, $headers ); // Send the emails 
    }
    

    所以让我们把所有脚本放在一起,你应该有这个: 代码:

    <?php ini_set("memory_limit","250M"); // We don't want any nasty memory error messages
    
    $SendTo[] = 'myemailaddress@thephpanswers.com'; // This is your email address, you can copy this line and add another recipient 
    
    $path = '/home/website/public_html/backupSQL/sql/' // This is the absolute path to where we are going to save the .sql files - please note you should place a .htaccess to deny any users browsing the directory
    
    $tmpFilename = time() .'_mysql.sql'; // This is the tmp filename for the sql, needs to be different everytime
    
    define('mysqlUser','mysqlusername'); // This is the username for the MySQL database
    define('mysqlPass','mysqlpassword'); // Password for the username
    define('mysqlDatabase','mysqldatabase'); // The database you wish to backup
    define('mysqldump','/usr/bin/mysqldump'); // The absolute path to the mysqldump program
    
    shell_exec(mysqldump . ' -u ' . mysqlUser .' -p' . mysqlPass .' ' . mysqlDatabase .' > ' . $path .$tmpFilename); // See the > $path . $tmpFilename these are populated from the variables you set above
    
    $from = "Backup <backup@domain.co.uk>"; // Who the email is coming from
    $subject = 'MySQL Database backup'; // The subject of the email
    $absoluteFile = $path . $tmpFilename; // Keep it simple, creates a variable of where the file is
    $fileType = 'text/plain'; // Content type
    $mailBodyText = '<h1>MySQL Database attached</h1>'; // Our HTML body of the email
    
    $mineBoundaryStr=md5(time()); // Needs to be random for the mime
    // Advanced headers from http://xahlee.org/php/send_mail_attachment.html
    $headers= <<<EEEEEEEEEEEEEE
    From: $from
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary="$mineBoundaryStr"
    
    EEEEEEEEEEEEEE;
    $mailBodyEncodedText = <<<TTTTTTTTTTTTTTTTT
    This is a multi-part message in MIME format.
    
    --{$mineBoundaryStr}
    Content-Type: text/html; charset=UTF-8
    Content-Transfer-Encoding: quoted-printable
    
    $mailBodyText
    
    TTTTTTTTTTTTTTTTT;
    
    $file = fopen($absoluteFile,'rb'); 
    $data = fread($file,filesize($absoluteFile)); 
    fclose($file);
    $data = chunk_split(base64_encode($data));
    
    $mailBodyEncodedText .= <<<FFFFFFFFFFFFFFFFFFFFF
    --$mineBoundaryStr
    Content-Type: $fileType;
    name=$tmpFilename
    Content-Disposition: attachment;
    filename="$tmpFilename"
    Content-Transfer-Encoding: base64
    
    $data
    
    --$mineBoundaryStr--
    
    FFFFFFFFFFFFFFFFFFFFF;
    
    foreach($SendTo as $k => $v) { // Loop through all our recipients
          mail( $v , date("H:i - jS \of F Y") . 'MySQL Database backup' , $mailBodyEncodedText, $headers ); // Send the emails 
    }
    ?>
    

    您确实应该保护您为 .SQL 文件选择的目录,这可以通过创建一个名为 .htaccess 的文件并将其保存在目录中来完成。 .htaccess 的内容如下: 代码:

    order allow,deny
    
    deny from all
    

    您现在可以使用 cron 作业自动执行此操作,将 cron 作业设置为每天午夜运行脚本

    我希望这可以帮助一些人!

    PS:使用此脚本后,我意识到 .sql 转储上没有真正的安全性,我发现在通过电子邮件发送给 .sql 文件之前使用 openssl 或类似的东西是 100% 安全的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2017-12-12
      • 2018-09-02
      相关资源
      最近更新 更多