Send Database Backup to Your Mailbox with Backup2Mail
Backup and downloading a web site’s database can be really annoying sometimes. It’s something we don’t want to miss, yet we often do. I never lost any of my databases, but I know some guys who unfortunately did.
This PHP script will — with a help of Cron Jobs — backup your MySQL database and send it to specified e-mail address. I suggest creating a separate account, for example backup@yourdomain.com, which will be accessed via web mail if needed. If you have really big database, you wouldn’t want to download it every day (or few times a day). Gmail will do in case you don’t have overly sensitive data.
So, without further ado, I’m releasing this life-saving piece of code. Download Backup2Mail v0.1b. Instructions included.
If you are adventurous type, uncomment the last line in the config.php and feel free to report me errors. Also ask in the comments if you run into a trouble.

13 Comments
Wow, sounds great. Gonna try it ASAP! Due to various reasons, I have problems with my database, so I really hope this will make my life a bit easier. More feedback when I test this :)
Comment (#) by bojan — 5th June 2005.
May I ask a question? If my database is more than 10MB, does this script still work? Would the attachment be cut apart into several parts? I wanna send my databse to the Gmail. The Gmail attachement maxmium size is 10MB. Thanks.
Comment (#) by roncon — 22nd June 2005.
roncon, the script will probably send attachment, but I don’t know how Gmail will handle this.
If you are dealing with a database of that size, I wouldn’t recommend sending it via e-mail regularly, because some hosting companies count all traffic made by e-mails into your account bandwidth limit. Instead, leave a copy at the root of your server and download it via FTP.
Comment (#) by marko — 22nd June 2005.
Thank you for answering my question. Will you continue developing this script? I have run this script on my website, it worked very well. Thank you very much. I suggest that maybe you could add the FTP function. That user can backup the database through FTP or email way or both. The FTP port may be variable, 21 or 926 ,etc.
Comment (#) by roncon — 22nd June 2005.
What about if your db server is different from your web host server as is sometimes the case on a third party host.
Comment (#) by Jon — 23rd June 2005.
[Fri Jun 24 07:04:54 2005] [error] PHP Warning: passthru() has been disabled for security reasons in /blablabla/backup2mail/index.php on line 29
:-(
nice work, works not for me.
Comment (#) by tobstone — 24th June 2005.
tobstone, apparently you run your web site on a server with safe mode enabled. Create PHP file and place the following code in it:
<?phpphpinfo();
?>
then upload to your server and open it in browser.
Find the safe_mode_exec_dir directive (it’s under Configuration -> PHP Core). This is the directory in which the
passthru()should be allowed. If you run into a trouble, contact your system administrator for exact details.Comment (#) by marko — 24th June 2005.
Jon - i think it is possible to put in a -h option in the mysqldump command part in index.php – give this option the parameter $db_host which you need to add a few lines above as a global and then also in the config file with other vars.
Comment (#) by Luke — 24th June 2005.
I did exactly this for a customer, but i was using ASP and an Access DB. The only problem I had was that i had no way of scheduling the process on the server. So I just stuck a ‘back-up db’ link in the customers admin section of the site, and told them it was up to them to click it regularly. Another trick you could use is to mark the email as spam in gmail, that way you will only ever retain 30 days of backups, which if your backing up regularly should be enough.
Comment (#) by Gibster — 8th July 2005.
Your idea is very good!
I think I will try your tool one day
Comment (#) by Patrick — 31st August 2005.
Awesom script! I’ve tried different cron job scripts and this one works perfectly fine! :)
I hope you could also develop a script that woul backup public_html files and send it to an email.
Comment (#) by Harris — 1st September 2005.
Hi.
great job.
I’m thinking of enhancements in order to use it without the needs for cron.
I hope I’ll have the time to do it.
Thanks for your work
Comment (#) by azimuh — 6th September 2005.
@azimuth: If you place the script somewhere in the public directory, it will backup and send via simple request in a browser.
However, I advise some kind of login system, since I’m sure, you don’t want someone mean to send you tons of e-mails by simply requesting the script’s URL
Comment (#) by marko — 6th September 2005.
Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.