简体   繁体   English

通过PHP以编程方式备份​​(和通过电子邮件发送)完整的MySQL数据库

[英]Programatically backing up (and emailing) a full MySQL database via PHP

I need to take a backup of a MySQL database daily, and preferably have it email itself to an email address of my choosing. 我需要每天备份一个MySQL数据库,最好让它自己发送电子邮件到我选择的电子邮件地址。

Is it possible to do this? 是否有可能做到这一点? What is the SQL to generate a backup file? 生成备份文件的SQL是什么? How can I add this file as an "attachment" to the email which is sent? 如何将此文件作为“附件”添加到发送的电子邮件中?

Thanks for the help, I really need to make these backups! 感谢您的帮助,我真的需要进行这些备份!

I do know how to call a script via cronjob, so the daily thing is not a problem. 我知道如何通过cronjob调用脚本,所以日常事情不是问题。

Simply drop to a command shell and run a mysql_dump command like so: 只需转到命令shell并运行mysql_dump命令,如下所示:

mysqldump <DB name> -u<user> -p<password> | gzip -c > /backup/`date +%Y-%m-%d`.sql.gz

From this point you can just use the mail command (I don't know the syntax off hand to attach a file). 从这一点开始,您只需使用mail命令(我不知道附加文件的语法)。

You can use exec to call mysqldump in the command-line to create a backup of your database to a file of your choosing. 您可以使用exec在命令行中调用mysqldump来创建数据库备份到您选择的文件。

 mysqldump -u username -p password databaseName > databaseFile.sql

Then in PHP, you can locate the file and upload as an attachment and send it out. 然后在PHP中,您可以找到该文件并上传为附件并将其发送出去。

I do not know how safe sending the database is via e-mail, if there is alternative solutions I could suggest, I would. 我不知道通过电子邮件发送数据库是多么安全,如果有其他解决方案我可以建议,我会的。

Per Tom's comment, take a look at PHP Zlib and creating GZIP files "on the fly" to zip files through PHP. 根据Tom的评论,看看PHP Zlib“动态”创建GZIP文件,通过PHP压缩文件。

Automatic MySql Backup Script 自动MySql备份脚本

This script finds all of the MySQL databases on your server, individually backs them up, compresses them, and stores the file in a specified directory and/or emails it to you. 此脚本查找服务器上的所有MySQL数据库,单独备份它们,压缩它们,并将文件存储在指定目录中和/或通过电子邮件发送给您。 It has been updated since the initial public release and includes several new features. 它自首次公开发布以来已更新,并包含一些新功能。

http://www.dagondesign.com/articles/automatic-mysql-backup-script/ http://www.dagondesign.com/articles/automatic-mysql-backup-script/

I used to use 我以前用过

MySQL database backup ===================== http://restkultur.ch/personal/wolf/scripts/db_backup MySQL数据库备份===================== http://restkultur.ch/personal/wolf/scripts/db_backup

However it's a bit old, and I'm not sure if they upgraded the version etc. But it works well, and givse you lot of backup options (how many days to keep, months, as well as where to store the file - it can be uploaded via FTP to another server for redundant security). 然而它有点旧了,我不确定他们是否升级了版本等。但它运行良好,并提供了很多备份选项(保留了多少天,几个月,以及存储文件的位置 - 它可以通过FTP上传到另一台服务器以实现冗余安全性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM