How to backup and email a dump of your Mysql database on Linux

A few weeks back, after working on an old, slow & clunking server that I use to run a billing system, I thought to myself ‘hey you know what, it would be great to be able to email myself backups of my MySQL database. That will stop me worrying about loosing all this data if this server ever dies on me.’

So, as you’ve probably guessed, I figured out how to do it. The answer’s actually very simple, and can easily be run as a cron job to automate the process on a daily basis.

First you need to make your MySQL dump file. (I prefer .sql files as I think it makes things way more transferable)

The command to dump all your data from MySQL is this:

				
					mysqldump –u root –password=”yourmysqlpassword” > mysqldumpfilename.sql
							
			

This will output a copy of your entire database to the file called mysqldumpfilename.sql (of course you can call yours whatever you want)

The next step is to get this file emailed to you. Somewhere (I don’t remember where) I found out that you should encode your mail attachments using the uuencode function in Linux.

So to email yourself a copy of your database backup, the command is as follows:

				
					uuencode mysqldumpfilename.sql mysqldumpfilename.sql | mail sylvia@home.com
				
			

Now just put the two together and you can email yourself a backup of your mysql database.