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: « 16 15 [14] 13 12 11 10 9 8 7 61 » Show All

  1. Beautiful, Worked the first time with 0 troubles. Thanks!

  2. dont work code …

    Warning: mkdir(/db_backups): Permission denied in /home/.sites/16/site186/web/backup_dbs.php on line 155

    Notice: Undefined index: f_log in /home/.sites/16/site186/web/backup_dbs.php on line 85
    Backup directory could not be created in /db_backups

    Notice: Undefined index: f_err in /home/.sites/16/site186/web/backup_dbs.php on line 85
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

    help me pls ?

  3. Brad,
    Waht versions of PHP and Mysql are you using. I want to install the script on Windows and since yours is working, I would like to install a similar configuration. I am running Windows Server 2008 and IIS.

    Thanks

  4. 207
    Brad

    For those with the infamous zip error that don’t want to buy WinZip, here’s what I did:

    1) Installed 7-zip and added it to my path statement (c:\program files\7-zip)
    2) Modified the line “exec( “zip -mj $BACKUP_TEMP/$db.sql.zip $BACKUP_TEMP/$db.sql 2>&1″ , $output, $res );” to “exec( “7z.exe a $BACKUP_TEMP/$db.sql.zip $BACKUP_TEMP/$db.sql 2>&1″ , $output, $res );”
    3) Modified the line “exec(“zip -j -0 $BACKUP_DEST/$BACKUP_NAME * 2>&1″, $output, $res);” to “exec(“7z a $BACKUP_DEST/$BACKUP_NAME * 2>&1″, $output, $res);”

    It now uses 7-zip for compression and all is well. Now, I don’t know what level of compression it’s using here and for my needs I don’t really care. You may want to look into 7-zip command line options if you want to tweak the compression and save more space, but I’m just backing up a small wiki so I’m not that concerned.

  5. Anyone getting the mkdir() error: Just create those directories manually and set the directory permissions for write access.

    I was getting the mkdir() error, but the script works once I created the directories manually.

  6. 205
    William

    I get the same errors that Aaron posted.

    I’m running PHP 5.3.0.

  7. 204
    Aaron

    Hi,

    I tried your script but for some reason I keep getting errors! To start with I receive:

    Warning: mkdir() [function.mkdir]: Permission denied in New Folder/backup_dbs.php on line 156

    Notice: Undefined index: f_log in New Folder/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups

    Notice: Undefined index: f_err in New Folder/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

    When initially trying to run the script.

  8. Please note you need two dashes on the below parameters such as –skip-opt, this blog removed the first.

  9. I’ve been using this script for years, I love it. I get emailed a backup every 2 hours and added an FTP function so I have backups all over the place.

    Whoever asked for PHP5 support, I’m on PHP5 and it works fine. If you want OPP, well, that’s kinda silly and would be a waste of dev time.

    My server would freeze on the frontend for about 30 seconds. I worked out this is because mysqldump, by default, locks the tables. Since I had very large tables this meant anyone on my website had to wait for pages to load, and this was causing pretty massive server load.

    Go down to //dump db

    Change the first exec() to:

    exec( “$MYSQL_PATH/mysqldump $db_auth –skip-opt –quick –disable-keys –set-charset –create-options $db 2>&1 >$BACKUP_TEMP/$db.sql”, $output, $res);

    No more lag :) It’s the –skip-opt that does it.

  10. Check out this alternative, if your provider does not allow the use of the exec() function, which is essential for Dagon’s awesome script.
    http://gelb3.de/blog/?p=79

  11. Would be really great if you could add support for PHP5

    Thank you in advance!

  12. When email arrives, it contains the code from backup_dbs_config.php. Excatly word for word. So now backup file is created. This is the command line in corn jobs: php /home/xxxxxxxx/xxxxxxxxxx/db_backups/backup_dbs.php . So would like to know where I went wrong.

  13. thanks,

    i’ll try this scripts :)

  14. Vielen Dank für diesen Vorschlag. Ich schicke Dir einen Gruß aus Deutschland.

    Thanks for the suggestion. I send you regards from Germany.

  15. How to organize the automatic start of the script (I have a dedicated server and have the right root). Can I make this script backups several MySql databases?

Pages: « 16 15 [14] 13 12 11 10 9 8 7 61 » 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.