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. Jason Golod: I do not have a version for directories, although you could just create a little shell script to do it, and set it to a cron job.

    Jason: Now that I look at your error messages again, you would have some problems with this script even if you could use that one function. You may not have much luck with any script like this – you may have some way to automate backups from your host’s control panel though, if you have one.

  2. [...] If you are looking for a way to backup all of your MySQL tables daily and have them emailed to you, I found an awesome MySQL backup script that takes about 1 minute to configure and works perfectly right out of the box. Of course, you may have trouble emailing all of the files to yourself if you have a lot of dbs but, the script can make the backup to a local drive and you can then setup another cron job to ftp that file somewhere or not. [...]

  3. Hey, wanted to say thanks. This is not only a great script, but well documented as well. Thanks for making it public. BTW, do you have a modified version to backup a set of directories?

  4. I guess my host has disabled it for security reasons, so I can’t see them enabling it just for me.

    Am I likely to have this problem with all database backup scripts, or should I continue my search?

    Thanks,

    Jason

  5. Jason: The problem starts with that first function (mysql_list_dbs). It uses that to get a list of the databases. If it cannot do that, it cannot back them up. I am not sure really how you would go about enabling that – I have not ever had to. Perhaps your host can change the option? You may not have access to it.

  6. I get the following error when trying to execute the cript:

    Executing MySQL Backup Script v1.4
    Processing Databases..

    Warning: mysql_list_dbs() has been disabled for security reasons in /home/bzone/public_html/backup_dbs.php on line 182

    Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bzone/public_html/backup_dbs.php on line 184

    Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/bzone/public_html/backup_dbs.php on line 231
    Archiving files..

    Notice: Undefined variable: output in /home/bzone/public_html/backup_dbs.php on line 244

    Notice: Undefined variable: res in /home/bzone/public_html/backup_dbs.php on line 244

    Warning: exec() has been disabled for security reasons in /home/bzone/public_html/backup_dbs.php on line 244

    Notice: Undefined variable: res in /home/bzone/public_html/backup_dbs.php on line 248
    Backup complete!
    Emailing backup to jason@badgezone.com ..

    Warning: file_get_contents(/home/bzone/public_html/db_backups/bz_db_backup_2006-09-27.tar) [function.file-get-contents]: failed to open stream: No such file or directory in /home/bzone/public_html/backup_dbs.php on line 293

    Warning: rmdir(/home/bzone/public_html/tmp/backup_temp) [function.rmdir]: Permission denied in /home/bzone/public_html/backup_dbs.php on line 375

    Will I not be able to make use of this script? I don’t have access to the server, but I do have access to cPanel and WHM.

    Thanks.

    Jason

  7. Daniel: Ah, good to hear! :) I will have to remember that one in case I see it happen again.

  8. 38
    Daniel

    I just wanted to let you know the problem is solved. PHP was choking on the memory limit. I set memory_limit = 16M in php.ini, restarted the server, and everything worked perfectly. It was originally at 8 MB.

  9. 37
    Daniel

    OK, I did some more testing and the error is definitly at the mail function. I set $EMAIL_BACKUP =false; and it does delete the temp files and everything works perfectly. I have triple checked and the mail variables are set the same as the other servers which work fine. The only difference is the size of the database (3.07 MB when compressed). Is there a size limit on mail attatchments in the php mail() function?

  10. 36
    Daniel

    I did try a different email address, but no luck. When I run the script through SSH it says that it is sending the email but it does not say “deleting temp files” afterwards. It does say that when I run it on the other servers. I have $DEL_AFTER = true;

  11. Daniel: Strange. Have you tried having the backup sent to another email address? I am not sure what else to suggest – I am thinking though :) If something is failing you really should be seeing some sort of error.

  12. 34
    Daniel

    Hello Admin,

    Yes, the test works and the email goes through correctly.

  13. Daniel: Are you sure you have access to the PHP mail() function on this server? You could test it in a PHP script:

    <?php mail('you@domain.com', 'Subject', 'Message', '-f you@domain.com'); ?>

  14. 32
    Daniel

    Thank you for writing such an excellent script. I have installed this on 5 of my accounts and it works extremely well. I have one account though that it refuses to work on and I have run out of ideas to fix it. It is installed in exactly the same location as my other accounts, and the variables are set exactly the same (except the password). All accounts are VPS’s on the same dedicated server and are set up exactly the same.

    The problem is that the first time it runs, it appears to complete (when initiating it through SSH) but it never emails the backup. It doesn’t delete the temp file, and then the next time it runs, it has an issue because the file already exists. It looks like the first installation ended up with the temp files being assigned different permissions so I can not delete them.

    I really need to get this working because this is my most important account.

    TIA, Daniel

  15. 31
    Richard

    -

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.