如何使用cronjob将MySQL备份发送到电子邮件

时间:2014-05-24 05:17:39

标签: mysql cron mysqldump

我正在尝试使用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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

好的,我将向您展示如何创建一个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%安全的!