Cronjob 用于获取所有 MySQL 数据库并将其发送到电子邮件
我想创建 cronjob 来执行 .php 文件,该文件将获取所有数据库,并且该数据将每天直接发送到我的电子邮件。
有什么想法我该怎么做吗?
非常感谢。
I want to create cronjob which would execute .php file and that file would get all database and that databe would be sent directly to my e-mail every day.
Have any ideas how could I do that?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“所有数据库”是什么意思?您想要服务器中保存的数据库列表吗?他们内容的转储?为此,您不需要 PHP,只需使用 shell 脚本即可:
简而言之:转储 mysql 中保存的所有数据库,将输出通过管道传输到邮件程序,邮件程序将转储发送给您。
当然,如果您有大量数据(特别是在数据库中存储文件),您最终会收到一封巨大的电子邮件,并且如果存在任何二进制数据,则电子邮件可能会损坏转储。您很可能希望通过 gzip 运行转储以减少其边长,然后将该 .gz 文件作为附件发送出去,这将比这个非常简单的版本需要更多的脚本编写。
What do you mean, "all database"? You want a listing of the databases held within the server? A dump of their contents? You don't need PHP for that, you can simply use a shell script:
In short: dump all databases held within mysql, pipe that output to the mail program, which sends the dump to you.
Of course, if you have a lot of data (particularly if you're storing files in the database), you're going to end up with a huge email, and emailing could corrupt the dump if there's any binary data. You'd most likely want to run the dump through gzip to reduce its side, and then have that .gz file sent out as an attachment, which would take a bit more scripting that this very simple version.
您可以编写一个执行 mysqldump 的脚本,然后通过电子邮件将文件发送给您...您实际上是在要求某人给您这样的脚本,还是在询问如何将其放入 cron 中?
You could just write a script that does a mysqldump and then emails you the file... Are you actually asking someone to give you such a script or are you asking how to put it into cron?
在 crontab 中包含以下内容
@daily php php_file.php | mail -E -s "主题行" [电子邮件受保护]
邮件是程序发送电子邮件,它从标准输入读取正文文本,并且 -s 定义主题行。 -E 表示仅在有正文的情况下发送电子邮件,您可能需要也可能不需要。
Include the following in your crontab
@daily php php_file.php | mail -E -s "Subject line" [email protected]
mail is a program to send emails, it reads the body text from stdin and -s defines the subject line. -E means only send an email if there is body text, you may or may not want this.
好吧,你首先要编写 php 脚本。
然后,您可以在 crontab 中添加一行,如下所示:
1 1 * * * path/to/your/script
每天运行脚本一次。
Well you would write the php script, first.
Then you would put a line in your crontab something like this:
1 1 * * * path/to/your/script
To run the script once a day.