MySQL Backup Skript mit Emailversand.

  • english
  • german

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

6 Comments

  • Hallo,

    bekomme beim aufrufen folgende Fehlermeldung:
    =========
    Warning: file_get_contents(/home/www/web3/html/cron/usr_web3_1_2010-09-24.sql.gz) [function.file-get-contents]: failed to open stream: No such file or directory in /var/www/web8/html/cron/MySQLBackup.php on line 125

    Warning: unlink(usr_web3_1_2010-09-24.sql.gz) [function.unlink]: No such file or directory in /var/www/web3/html/cron/MySQLBackup.php on line 142
    ======

    woran kann es liegen?
    Danke für eine Rückmeldung
    Gruss Hans bach

  • Hast du die Rechte für den “cron” Ordner geprüft, scheinbar kann er da die Datei nicht ablegen. Falls die Rechte stimmen, scheint mysqldump nicht richtig zu laufen, probier mal, es per hand in der shell aus.

  • Hi Alex,

    bin schon am verzweifeln.

    ja die Rechte stehen auf beim “cron” Ordner auf 755. Und ich haben die backup.php manuell ausgelöst. > da kommt die Fehlermeldung.

    Ich bin mittlerweile drauf gekommen, dass die usr_web3_1_2010-09-24.sql.gz nicht in den “cron” Ordner gelegt wird.

    Ich habe darauf hin die usr_web3_1_2010-09-24.sql.gz manuell in den Ordner “cron” gelegt und auch die backup.php manuell ausgelöst. So hat es funktioniert.

    Sagt mir, dass nur die usr_web3_1_2010-09-24.sql.gz nicht erstellt werden kann.

    Warum?

  • hallo nochmal

    habe gerade gesehen, das in line 125 steht:

    $data = chunk_split(base64_encode(file_get_contents($this->path . $filename)));

    kann das mit dem Server zusammenhängen? das hier ein encoder nicht richtig arbeitet?

  • habe soeben erfahren “die PHP-Funktion “base64_encode” gehört zum grundlegenden Funktionsumfang von PHP 5 und ist auf dem System verfügbar.”

    auch arbeitet der mysqldump richtig, denn ich kann normale backups drüber ausführen.

    z.B. mysqldump -u web3 -p`cat /var/www/web3/files/cron.txt` usr_web3_1 > /var/www/web3/files/backup_usr_web3_1.sql funktioniert ganz normal

    wie würde der genaue Befehl bei deinem script für den Cronjob lauten?

  • Der Aufruf ist dieser:

    /path/to/mysqldump –host=localhost –user=someuser –password=somepassword db-schema –quick –lock-tables –add-drop-table | /path/to/gzip/gzip > /targetpath/targetfilename

    Geht gzip bei dir? Scheint das einzige zu sein, was du noch nicht getestet, probier mal.

    Alex

So, what do you think?