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

  If you have found this page useful, please consider donating. Thanks!

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!

Pages: « 14 13 12 11 [10] 9 8 7 6 5 41 » Show All

  1. I using wampserver, these are the error. Can any one help me on this?

    Warning: mkdir() [function.mkdir]: No such file or directory in D:\wamp\www\autobackup\backup_dbs.php on line 160

    Notice: Undefined index: f_log in D:\wamp\www\autobackup\backup_dbs.php on line 86
    Backup temp directory could not be created in /tmp/backup_temp

    Notice: Undefined index: f_err in D:\wamp\www\autobackup\backup_dbs.php on line 86
    Backup temp directory could not be created in /tmp/backup_temp
    Backup temp directory could not be created in /tmp/backup_temp

  2. 149
    Devin

    Executing MySQL Backup Script v1.4
    Processing Databases..
    DUMP FAILED
    sh: tmp/backup_temp/agp.sql: Permission denied

  3. 148
    Michael

    This is a great script, thanks. I wanted to be able to auto delete old versions of the file, so I googled and found / modified this script to help me. I also execute your script inside of this.

    #!/bin/bash
    ## RSFBS – Really Simple File Backup Script by DaiTengu.
    ##

    ## Variables go here!
    # General base backup directory, All the subdirectories will be
    # created under here.
    scriptdir=/var/www/scripts

    #Execute php backup of mysql db
    php $scriptdir/backup_dbs.php

    basedir=/var/www/db_backups

    # Let’s name our backup file, the date will be appended to this
    backupname=mysql_backup_

    # How many backups do we want to keep? This should be in the format
    # days, weeks, months, etc. Example: keep=”7 days”
    keep=”7 days”

    # Some stuff to properly name the files
    suffix=$(date +%F)
    olddate=$(date –date “$keep ago” +%F)

    ## Delete the old tar file!
    if [ -e $basedir/$backupname$olddate.tar ] ; then
    echo “Deleting old tar file…”
    rm -rf $basedir/$backupname$olddate.tar
    fi

    ## Delete the old log!
    if [ -e $basedir/$backupname$olddate.log ]; then
    echo “Deleting old log…”
    rm -rf $basedir/$backupname$olddate.log
    fi

    ## Oh hay! We’re all done! Let’s clean up and exit gracefully!
    echo “Done!”
    exit 0;

  4. 147
    Mai

    Getting this error. Any advice?

    Warning: mkdir(/db_backups) [function.mkdir]: Permission denied in MY URL on line 156

    Notice: Undefined index: f_log in MY URL on line 86
    Backup directory could not be created in /db_backups

    Notice: Undefined index: f_err in MY URL on line 86
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  5. 146
    rolance

    For the previous windows XP users who reported the error: “zip is not recognized as an internal or external command,operable program or batch file” – the problem is either that you do not have zip.exe installed on your system (you will have to hunt it down) or you have zip but have not configured your path environment variable to make it accessible from the update php script. This is a nice script for unix users, but it doesn’t work ‘out of the box’ on windows and will take quite a bit of hacking, so newbies might want to move on.

  6. 145
    Ayuk Rolannd

    Thank you very much for the script this is wnat I have been looking for all day long, but i seen to have some errors in making the script work
    I am using Xampp install on windows so I want I have the following error trying to make the script backup my database automaticaly.

    DUMP FAILED
    ‘C:\Documents’ is not recognized as an internal or external command,
    operable program or batch file.
    DUMP FAILED
    ‘C:\Documents’ is not recognized as an internal or external command,
    operable program or batch file.
    COMPRESSION FAILED
    ‘zip’ is not recognized as an internal or external command,
    operable program or batch file.
    I don’t know how to go about this please try to help me i am new in
    database administration

  7. Thank you very much for the script…
    I keep getting this error…

    Warning: mysql_list_dbs() [function.mysql-list-dbs]: Unable to save MySQL query result in /data/13/1/112/58/1275710/user/1361670/htdocs/mysqldump/backup_dbs.php on line 187

    Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /data/13/1/112/58/1275710/user/1361670/htdocs/mysqldump/backup_dbs.php on line 189

    Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /data/13/1/112/58/1275710/user/1361670/htdocs/mysqldump/backup_dbs.php on line 241
    Archiving files..
    FAILED
    tar: db_backups/mysql_backup_2008-05-17.tar: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now
    FAILED
    tar: db_backups/mysql_backup_2008-05-17.tar: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now

  8. 143
    Miles

    Hi Excelent but cant get it to work, it reports all db’s backed up & compressed it even sends the email witht he attached zip file. when i open the file though it is empty. i have tried adjusting the config file as follows:
    // Path to the mysql commands (mysqldump, mysqladmin, etc..)
    $MYSQL_PATH = 'C:\Program Files\MySQL\MySQL Server 5.0\bin';

    still the same empty zip file although all reports sucessful backup. is there anything else i can try?

  9. 142
    Paul

    Thanks for the script. It was working fine for me on two different servers. However, after updating the script to the most recent version on one of the servers I get

    Warning: Cannot modify header information – headers already sent by (output started at /home/***/public_html/****/backup_dbs.php:1) in /home/***/public_html/***/backup_dbs.php on line 131

    Warning: Cannot modify header information – headers already sent by (output started at /home/****/public_html/***/backup_dbs.php:1) in /home/****/public_html/***/backup_dbs.php on line 132

    Errors.

    Any ideas why?

  10. Hi, first of all thank you for creating this script!

    I’ve succeeded installing your script. When I ran it; the backup file was created successfully although i saw error message as follows:

    
    Failed loading /usr/local/IonCube/ioncube_loader_lin_5.2.so:  /usr/local/IonCube/ioncube_loader_lin_5.2.so: cannot open shared object file: No such file or directory
    

    Do i really need this IonCube?

    Thanks before.

  11. Hi Guys,
    Love this script but im having one issue. See below
    Im using a windows xp machine with winzip installed

    Dumped DB: mainzpanel COMPRESSION FAILED ‘zip’ is not recognized as an internal or external command, operable program or batch file. COMPRESSION FAILED ‘zip’ is not recognized as an internal or external command, operable program or batch file. Emailing mainzpanel backup to

  12. If you are getting the following error and error log will not show anything else …
    “2007-10-24 03:10:02: Executing MySQL Backup Script v1.4
    2007-10-24 03:10:02: Processing Databases..”

    you need to install php5-mysql package.
    Ubuntu you can do so by running the following command:

    for PHP5

    
    # sudo apt-get install php5-mysql   
    

    for PHP4
    
    # sudo apt-get install php4-mysql   
    

    Now everything works so cool … thx for such a cool utility.

    Dash

  13. –> M. Keijzer

    It seems that you *might* need to verify User/Group permissions.

    Check that ‘apache’ (or whatever user and/or group used by your web server) have WRITE permission on the sub-folder(s) you configured.

    You may also need WRITE permission for this user / group on the root or main folder, since sub-folders are created if non-existent.

    I’m not sure about Windows 2003 but I had the same issue running on Cent OS 5.1 and after fixing permissions everything works perfectly.

  14. I use the backup-script on Windows Server 2003 with PHP and MySQL.
    I created the directory’s, it does write a log-file, but is doesnt’t write the backup-files.
    What do I have to configure?

    Of course on my Ubuntu-Linux testing-server it works perfectly…

  15. For anyone using GoDaddy with php 5.0 and MySQL 5. This is the path needed for mysql commands (mysqldump, mysqladmin, etc..)

    $MYSQL_PATH = ‘/usr/local/mysql-5.0/bin’;

Pages: « 14 13 12 11 [10] 9 8 7 6 5 41 » 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.