Auto generated MySQL backup Using PHP


Many sites are using MySQL as database. It is a good practice that taking backup in regular intervals. My aim was to develop a package which deliver me database backup files [ having extension .sql] and mail me with the file as attachment.

You can download[ link ] this package from phpclasses.org[ link ]

This package, I named it as myeskuel. This package contains
1. ReadMe File : README.txt
2. Class File : mysql.class.inc
3. Configuration File: config.php
4. Invoking script : backup.php

How To Configure This
All you need to give some details into the config.php file. The configuration is divided into 3 parts:


1. BackUp folder Configuration
$backUpFolder = "backup";


Please change the folder; iff you need to change the backup directory. It is better to give complete path. The line just below it tries to make the directory if not exists and give global (0777) permission for access.


2. Database Configuration Details
    $server['host'] = "localhost";
    $server['port'] = "";
    $server['user'] = "root";
    $server['pass'] = "pass";
    $server['database'] = "test_db";


    In this we need to give the details of the database: hostname, authenticated username, its password and the name of database. Giving port number is not necessary, if you are not sure about leave that position blank; it will take the default port number[3306].


    3. e-mail Configuration Details
    $mailer["FromMail"] = "db_manager@yoursite.com";
    $mailer["ToMail"] = "developer@yoursite.com";
    $mailer["MailBody"] = "Message Body";
    $mailer["DAS"] = false;


    Prerequisite for this facility is default php mail() support for your server and supports sending generated emails [most of the sites are having these options].


    FromMail, is the valid email address which comes in 'from header'. ToMail is considering as the trigger for mailing function. If you are leave this blank, the function for sending e-mail will be disabled. The MailBody contains the message that will be send along with the file as attachment.


    4. DAS – Delete After Send
    $mailer["DAS"] = false;

    This is an important parameter, if you configured as true, it will delete the file from the backup folder [created in the server] after successfully send the mail which configured to. The default is false, a copy will remain in the backup folder.


    EXTRA
    backup.php is the file that invokes the class and assign the variables, you can see generating a random name [on line # 38]. You can use any algorithm if you find it is not sufficient.


    Go through the class file mysql.class.inc. The functions having comments which helps you to modify and suited for you purpose. You can set a cronjob[scheduled Task] for send you the updated database backup on regular intervals. Enjoy.


    Comments and queries are always encouraging”.


    Download: myeskuel.zip




    2 comments:

    Anish said...
    This comment has been removed by the author.
    Anish said...

    @Efootprint Please check the config file and make sure every info has entered. Also please check the mail function will work in the server with attachment.

    Post a Comment