Tag email

MySQL backup script with emailing

A while ago I found a good script for backing up a MySQL database and sending it via email to a recipient here.

The script is cool, but I didn't like its structure and the fact, that you have to add the database values inline and that it can only backup a single database. For this reason I rewrote it a little and you can download it here.

Features:

  • Backup of mutliple databases
  • Sending of backups to multiple users

For every single database a mail is send and there is no file saved on the server. And wow: it's really easy to configure!

How do I use it?

  • Download the current version (0.1)
  • Adjust the backup.php (you're getting help by my wonderful comments)
  • upload everything to a directory of your choice
  • if applicable, create a cronjob to periodically execute the script

And here the backup.php that calls the appropriate classes and executes the backup (also included in the download):

ini_set("error_reporting", E_ALL);
// include the files
require_once "MySQLConfig.php";
require_once "MySQLBackup.php";

// add some databases to backup
// the domain will be appended to the email subject and is also included within the sql file for identification.
$cfgHost0 = new MySQLConfig("username0", "password0", "database_name0", "domain0");
$cfgHost1 = new MySQLConfig("username1", "password1", "database_name1", "domain1");

$backup = new MySQLBackup();
// the path to the directory where this script is resided
$backup->setExecutionPath("/srv/domain/backup/");
// add the database configs to backup
$backup->addDatabaseToBackup($cfgHost0);
$backup->addDatabaseToBackup($cfgHost1);
// the sender of the backup mail
$backup->setSender("admin@yourdomain.com");
// add some people to receive the backup
$backup->addRecipient("john@yourdomain.com");
$backup->addRecipient("frank@yourdomain.com");
// execute the whole thing
$backup->backup();

If there are any problems or suggestions or feature wishes, please comment this post - thanks!

PS: to create a cronjob log into your server using SSH, then execute crontab -e to edit the crontab and insert for example

0 2 * * 0,3 wget http://yourdomain.com/backup/backup.php -nc -q -O /dev/null

for an execution on sunday and wednesday at 2 am. Save and close the whole thing with :wq and that's it :-)