如何使用cronjob将MySQL备份发送到电子邮件
我正在尝试使用cronjob从cPanel自动备份数据库。当cronjob运行时,我想将数据库发送到我的电子邮件地址,我已经编写了代码(见下文),但它仍然不工作如何使用cronjob将MySQL备份发送到电子邮件,mysql,cron,mysqldump,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/ja
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
我一直在引用这篇文章:
我希望你能理解我的问题并帮助我
我也尝试过卷发,但仍然不起作用。您可以查看我遵循的步骤。
第一步:创建mail\u 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
看来
邮件
不可供您使用或未安装。
另一个要考虑的选项是使用CURL发送电子邮件,如这里所描述的:
以下是如何使用上面链接中的代码实现此功能:mail\u alert.sh
文件内容
From: "User Name" <username@gmail.com>
To: "John Smith" <john@example.com>
Subject: Backup completed
The backup has been completed.
#/bin/bash
curl--url“smtps://smtp.gmail.com:465“--ssl请求\
--邮寄自“username@gmail.com“--邮件rcpt”john@example.com" \
--上载文件mail.txt--用户“username@gmail.com:密码”--不安全
mail.txt
文件内容
From: "User Name" <username@gmail.com>
To: "John Smith" <john@example.com>
Subject: Backup completed
The backup has been completed.
好的,我将向您展示如何创建一个php脚本,在不使用phpMyAdmin的情况下备份MySQL数据库,然后将.sql文件附加到电子邮件中 今天我需要创建一个备份数据库的小脚本,然后通过电子邮件发送。我发现最好的方法是使用mysqldump程序。通常,您有权运行此程序,即使是在经销商托管软件包上 在linux中,程序通常位于 代码: 好的,让我们开始吧。 首先,我们需要设置包含MySQL凭据、要发送到的电子邮件地址、存储sql文件的路径、mysqldump程序的绝对路径的变量 代码: 使用mysqldump备份MySQL数据库: mysqldump非常易于使用,有关更多信息,请访问此处: 现在我们只需添加shell_exec来告诉mysqldump备份数据库 代码: 现在可以运行此脚本,并查看它是否在指定的文件夹中实际创建了.sql文件 用PHP发送附件 好的,我们知道我们的文件位于$path$tmpFilename让我们继续处理复杂的附件电子邮件 代码: 现在,您可以通过cron作业自动执行此操作,将cron作业设置为每天午夜运行脚本 我希望这能帮助一些人
PS:在使用这个脚本之后,我意识到.sql转储上没有真正的安全性,在发送电子邮件之前,我发现在.sql文件上使用openssl或类似的东西是100%安全的 您好,我已经更新了我的问题,所以您能检查我的步骤并纠正我的错误吗?它在没有
--ssl reqd
的情况下对我有效,所以请删除它并重试您的意思是:#/bin/bash curl--url“smtps://smtp.gmail.com:465“\--来自的邮件”example@gmail.com“--邮件rcpt”example@gmail.com“\--上传文件mail.txt--用户”example@gmail.com:mypassword”--不安全
/usr/bin/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
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
}
<?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
}
?>
order allow,deny
deny from all