Welcome to Dagon Design. In addition to free scripts, WordPress plugins, and articles, we offer a variety of services including custom theme design, plugin creation, and PHP scripting. Contact me for more information.

Version 2.1   Updated Thursday, May 3rd, 2007 at 8:08am

Automatic MySql Backup Script

This script finds all of the MySQL databases on your server, individually backs them up (for easy recovery of specific data), 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.

I would like to thank Oliver Mueller for his many contributions to this release, including additional features, fixes, and testing. Thanks Oliver!

Download v2.1

Other versions

Page Contents

Change log

  • v2.1 (May 3, 2007)

    New option added to allow comma separated list of databases to exclude

  • v2.0 (September 27, 2005)

    Completely rewritten in PHP
    Config is now in separate file
    New logging and error system
    Log files are time-stamped
    Log files can be sent with backup email
    Error log can be emailed to alternate address
    Windows support (more info below)
    Can set max execution time for script
    Option to flush and optimize databases
    Can be executed from browser, cron job, etc..

  • v1.2 (March 30, 2005 - First public release)

    Written in Perl for Unix-based systems
    Automatically finds all of your MySQL databases
    Databases are backed up individually
    The backup files are compressed into a single file
    Backups can be stored on the server and/or emailed

Requirements

There are very few requirements for this script to function. You must of course have PHP. You will also need to supply the script with a username and password for MySQL so that it can extract the databases. It is best to use the root MySQL account so it will be able to perform a complete backup.

This script was originally written for Unix-based systems, but this new version will work on Windows platforms, provided that you make the required configuration changes, and have an acceptable zip and tar replacement (that can properly handle the command line parameters).

Installation

To install the script, just download the two files above, renaming them from .txt to .php. You can put them anywhere on your server, as long as they are together. Since the script is now in PHP you can even run it directly from your browser, although if you do, a password-protected directory is recommended.

Configuration

Before running this script, open up the configuration file - backup_dbs_config.php - and make the required changes. Most of the options do not have to be changed, but some are required for the script to function properly (such as the MySQL connection info).

The logging / error system

Two log files are created every time the script runs. The standard log and error log. What happens to them depends on your configuration.

When you are performing an email backup, the standard log will be placed inside the body of the message. The backup file will of course be attached to this message. You can also choose if the backup file and standard log are left on the server after emailing.

Error messages are still shown in the standard log, but the separate error log allows you to receive notification (to a separate email address) in case of error. This can be useful if you want errors sent to your primary account, while backups are sent to an alternate (that you do not check every day). That way you can always be aware of problems.

Manually running the script

Depending on your configuration and user permissions, you may have to run this script as root.

From the command line, you can type: php /path_to_script/backup_dbs.php

You can also run it directly from your web browser: http://www.domain.com/path/backup_dbs.php

Setting up a cron job

Once again, depending on your configuration and user permissions, you may have to be logged in as root to create a cron job.

type crontab -e and add a new line. Here is an example:

30 0 * * * php /root/scripts/backup_dbs.php

That will configure cron to run the script every day at 12:30 am. To learn more about cron jobs, you can visit this cron guide.

After adding the new line, save the file and exit. Cron may have to be restarted before it will recognize the new job. To do this, simply type:

/etc/init.d/crond restart

On certain servers, emails are sent out containing the output of cron jobs. If you would like to prevent this, you can add the following code to the end of your new cron job line:

> /dev/null 2>&1

If you add this code, do not forget to restart cron again before testing. That line only prevents email notifications after cron jobs execute. This will not effect the script’s log file options.

Final notes

This script has been tested extensively, but I take no responsibility for any damages caused by misuse, improper configuration, or differences in server configurations. If you have any questions or comments, feel free to post them below!

Topic: Scripts | RSS Feed

Pages: « 11 10 [9] 8 7 6 5 4 3 2 1 » Show All

  1. 135
    daniele

    hi,
    i’m trying this script but i experienced this:

    The server encountered an internal error or misconfiguration and was unable to complete your request.
    
    Please contact the server administrator, postmaster@site.it and inform them of the time the error occurred, and anything you might have done that may have caused the error.
    
    More information about this error may be available in the server error log.

    what happen?

    thanks a lot, daniele.-

  2. thanks, its great, i added hours/minutes to the filename so that it doesnt overwrite the older file from the same day (useful for reverting to eariler DB from that day)

    $BACKUP_NAME = ‘mysql_backup_’ . date(’Y-m-d_Hi’);

    Thanks!

  3. 133
    Omid

    Hi again
    Now I have this error:

    Warning: mkdir() [function.mkdir]: No such file or directory in /home/user/public_html/mydb/backup_dbs.php on line 156

    Notice: Undefined index: f_log in /home/user/public_html/mydb/backup_dbs.php on line 86
    Backup directory could not be created in home/user/export_mu

    Notice: Undefined index: f_err in /home/user/public_html/mydb/backup_dbs.php on line 86
    Backup directory could not be created in home/user/export_mu
    Backup directory could not be created in home/user/export_mu

    Could you please tell me what`s exact reason of above error ?
    I couldn`t find my answer in other comments :(

  4. 132
    Omid

    Hi
    Warning: exec() has been disabled for security reasons in /home/name/public_html/mydb/backup_dbs.php on line 219
    How could I modify above warning ?

  5. 131
    X.A.

    Works like a charm!

  6. I had hoped that this script would be my saviour, but despite my best efforts I cannot get it to work - purely because of the site I am running it on using PLESK which does not allow me to change database user permissions or to run as root user (despite it being a dedicated box).

    All I get is:

    
    Executing MySQL Backup Script v1.4
    Processing Databases..
    DUMP FAILED
    
    DUMP FAILED
    
    COMPRESSION FAILED
    
    COMPRESSION FAILED
    
    Archiving files.. 
    FAILED
    
    FAILED
    
    
    *** ERRORS DETECTED! ***
    Check your email account *************** for more information!
    
    
    Emailing backup to *************** .. 
    
    There were errors!
    Emailing error log to *************** .. 
    FAILED to email error log.
    FAILED to email error log.
    

    I tried using username root and the password I have for SSH access but that doesnt work either. If anyone can give me any pointers I really would appreciate it.

    I have no doubt that I’ll be using the script on other sites, its just a shame that I cannot get it to work on the one site I needed it on :(

  7. wow.. i was lost in a world of pearl and bashings.. and this script did the work, right off the bat.. thanks a million.

  8. 128
    Karl Roos

    Strange :S
    I get this..

    Warning: mkdir() [function.mkdir]: Permission denied in /home/web28141/domains/roosstudios.com/public_html/Backup/backup_dbs.php on line 156

    Notice: Undefined index: f_log in /home/web28141/domains/roosstudios.com/public_html/Backup/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups

    Notice: Undefined index: f_err in /home/web28141/domains/roosstudios.com/public_html/Backup/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  9. Thanks for this brilliant script!

    I’ve set it up so it backs up to the server daily and emails me a backup weekly :)

    Martin

  10. 126
    Rafaiel Sabatini

    what will be $MYSQL_PATH in windows. Could you please give an example.Thanks

  11. When I manually run the script, This is what I get to see:

    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  12. 124
    Tim

    I am running a MySQL DB backup script. The script has been working fine for 1-2 weeks. However, it is getting an error today.

    PHP Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 15288321 bytes) in /home/[deleted]/public_html/[deleted]/backup_dbs.php on line 305

    Here is the code that causes the error.

    $data = chunk_split(base64_encode(file_get_contents($BACKUP_DEST.’/’.$BACKUP_NAME)));

    Do you think it is the way the code that is written can’t handle the growning DB? The memory that it tries to allocate does increase with each failed script execution.

  13. Bump on Post 119. I also would like some information on the EXLUDE_DB function. Otherwise the script works just fine (it backs up my DB:s too a Gmail account, thanks Google for 6 GB storage!).

  14. 122
    yehsay

    I am looking to install this on my Mac OS X Server and was wondering if it works? I have installed it but am receiving this error “2007-11-27 15:41:12: DUMP FAILED
    mysqldump: Got error: 1045: Access denied for user ‘root’@'172.25.64.227′ (using password: YES) when trying to connect”

    My web server has 2 IPs that it is running, how can I limit it to the the other IP. Also, when I manually run the script, my .sql files are 0kb in size. Am i doing something wrong here. Please assist. Thank you.

  15. 121
    Jasper

    Never mind my comment, I just couldn’t figure the odd way the messages are ordered. (from down to up, yet from 1 to 8 - I expected either from up to down, from 1 to 8 or down to up, from 8 to 1. I saw from down to up and falsely concluded it would be from 8 to 1.)

Pages: « 11 10 [9] 8 7 6 5 4 3 2 1 » Show All

Leave a Comment

Before you comment: If you are having an issue with a script, please make sure you have read the entire article. Also, please read through the comments because most common issues have already been discussed many times. Thanks.


Be sure to wrap all code in <code></code> tags.