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: « 1611 10 9 8 7 6 5 4 3 2 [1] Show All

  1. It may still be a permission error, but I will test it myself and let you know today :)

  2. 14
    henry

    Thanks for the quick reply, adjusted so it can find the exact dir and now get another error. Is this to do with the host not allowing scripts to execute? Ive set chmod 777 on both dirs and still no joy.

    Warning: fopen(backup/mysql_backup_2005-10-20.log): failed to open stream: Permission denied in /home/.sites/16/site186/web/backup_dbs.php on line 165
    Cannot create log file: backup/mysql_backup_2005-10-20.log

    Notice: Undefined index: f_err in /home/.sites/16/site186/web/backup_dbs.php on line 85
    Cannot create log file: backup/mysql_backup_2005-10-20.log
    Cannot create log file: backup/mysql_backup_2005-10-20.log

  3. It sounds like you do not have permission to create those directories. The default is /db_backups but if you do not have root access, you may need to edit the configuration so that it creates the directories in your local user space.

    Or if you are running it from a browser, you may need to create the directories first, and use the full server path to them in the config (/home/user/public_html/backups) etc..

  4. 12
    henry

    Tried on diff servers and get the same error:

    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

  5. The new version has been released! Read for details.

  6. Oh I see.. yes, that issue has been fixed. The version you have assumed that if you ran the script as root, you would automatically have the access (I have a password for mysql root and it works).

    But fear not, the new version has variables added for the mysql stuff. It is actually much much better in general in terms of what it assumes. It also has much nicer logging support, etc.. It can even be used under Windows (assuming you make all the required variable changes, and have an acceptable zip/tar program installed). But you will be able to read all that once the page is updated :)

  7. 9
    Wassim

    It happens the second I run the script. It appears most people don’t use a password for the MySQL root user? Looking forward to the PHP version.

    mysqldump: Got error: 1045: Access denied for user: ‘root@localhost’ (Using password: NO) when trying to connect
    mysqladmin: connect to server at ‘localhost’ failed

  8. Do you get that message when you first run it? Or is it at one particular point of the script? Btw – there is a new version which I am testing and will release soon. It uses php instead of perl, and has many new options. If all else fails, maybe it will work better for you.

  9. 7
    Wassim

    I’m trying to use this but getting access denied running it as root. I can run the command it executes via command line with the -u and -p options and it works, but, when I try to add those options I still access denied. Any ideas?

  10. That would probably be easy to do. I may do that for my next update, so keep checking back. :)

  11. Good work really.

    I have a suggestion.
    It will be great if you can add a feature where the application can upload the backed-up databsed onto a different ftp location. I mean a different domain. The reason, i lost my server once, all backup was also store on the same machine and nothing was spared.

    Good Luck.

  12. Glad to hear it :)

  13. Thanks Admin.
    Worked like a charm. Only caveat was not forgetting the third curly bracket.

  14. Hello. I just updated the tutorial, adding a section on how to exclude databases. Let me know if it works for you.

    As far as the SCP functionality, I just added it by request, and while I have tested it, I do not use it myself. If you have not used SCP before, I would learn the basics before letting the script do it. The connection has to be manually set up at least once, so that it sees the security certificate.

  15. Hello,
    Couple of questions.
    1. Seem to remember that you mentioned the SCP function doesn’t work properly?
    2. Any chance that a parameter can be added to exclude certain databases?
    Thank you.

Pages: « 1611 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.