views:

112

answers:

4

I need to take a backup of a MySQL database daily, and preferably have it email itself to an email address of my choosing.

Is it possible to do this? What is the SQL to generate a backup file? 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.

+2  A: 

You can use exec to call mysqldump in the command-line to create a backup of your database to a file of your choosing.

 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.

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.

Anthony Forloney
You probably want to tar/gzip it as well unless it's trivially small.
Tom
+7  A: 

Simply drop to a command shell and run a mysql_dump command like so:

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).

TravisO
Arg, I ran out of votes today, but very nice answer. Give me 7 hours, I will return to +1
Anthony Forloney
I also use this approach in a crontab at my web host. In addition, I also tar/gzip the associated web app files.
spoulson
This would work via PHP?
Cyclone
Yeah, you can run that through PHP's `exec` function
Anthony Forloney
Can you give an example of the syntax used for that?
Cyclone
+1  A: 

Automatic MySql Backup Script

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. It has been updated since the initial public release and includes several new features.

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

Yada
Is there any way to just make it back up a single database?
Cyclone
A: 

I used to use

MySQL database backup ===================== 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).

jeffkee
I don't have that kind of access unfortunately
Cyclone
What do you mean control? In case I mislead you with talks of FTp etc...This is as simple as installing wordpress, really. Upload it, configure the mysql access username password, select db, select frequencies and you're done. By default the .sql files are stored within the application folder. Or should i say "directory" instead of "folder" since we're talking about apache servers? :P
jeffkee
No, the problem is being able to access /backups/, my website is stored in /home/<myuser>/
Cyclone
no it lays within eaach account... so if you have /backups/ set from your FTP, it'd be under /home/<myuser>/backupsIn my case, I have it under my admin module - which is at /home/jeffkee/admin/db_backup/The folder name/location doesn't matter as long as you set it up right.
jeffkee