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!

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

  1. 60
    Austin

    Bad idea to put a temp directory in a directory that isn’t (I’m sure you realized that by now). You should use something like C:\temp\mysql because if you read the code, this script will delete all the files in the directory it made when it cleans up. I suppose your way will work as well, but that really isn’t how a temporary directory is designed.

  2. 59
    DeletedMYSQL

    Well, I’m running Windows and wanted my backups in c:\mysql\backups.
    For a temp directory I figured it would put the sql files in a dir then remove the sql files it made, naturally. So I set the temp path to c:\mysql\.
    That caused the entire mysql directory’s contents (aside from folders) to be removed.

    I recoded it to delete only the files it created now so there’s no more issue with my install.

  3. 58
    Austin

    Did you happen to change the $BACKUP_TEMP path so it’s just ‘/tmp’ ?? Because that will do it, you should leave it at default. And why did you delete everything in the mysql directory? I’m not sure I’m following you.

  4. 57
    DeletedMYSQL

    This code will delete ALL files in your temp dir, even if they weren’t created by the script. USERS BEWARE!!!

    I deleted everything in my mysql directory :(

  5. Austin’s custom version of this script is now available for download above. It is a modified version which emails each database separately, which may help if you find that the standard version generates attachments that are too large.

  6. Austin: Nice work. If you would like to contribute your version, feel free :) I will add it to the site (email me if interested)

  7. 54
    Austin

    I tweaked it a little bit so it’d email me each database seperately. This was attachments would go through to free email providers like GMail.

  8. On my other server (plesk) i get the access denied for user admin@localhost passowrd:YES.

    I have managed to get the dump work on the command line using:

    /usr/bin/mysqldump -uadmin -p*********** –all-databases > /var/www/vhosts/*********/httpdocs/allbackups.sql

    Does your code need altering for plesk installations? I tried hacking at the $db_auth, but had no joy! It works fine as is on my WHM/cpanel vps.

  9. Great script, i get an error saying the email failed to send. Why might this be? The archive is just 50mb, so it’s not a size issue…

  10. This script really needs to be ran from root either through a shell or cron, and not just from a web browser.

  11. I also recieve the same type error, probally a mistake on my part. Altough I am using sql user access that can see all DB in mysql.

    Warning: Cannot modify header information - headers already sent by (output started at /home/marlarbi/public_html/Cron/backup_dbs_config.php:120) in /home/marlarbi/public_html/Cron/backup_dbs.php on line 132

    Warning: Cannot modify header information - headers already sent by (output started at /home/marlarbi/public_html/Cron/backup_dbs_config.php:120) in /home/marlarbi/public_html/Cron/backup_dbs.php on line 133

    Warning: mkdir(/db_backups): Permission denied in /home/marlarbi/public_html/Cron/backup_dbs.php on line 157

    Notice: Undefined variable: f_log in /home/marlarbi/public_html/Cron/backup_dbs.php on line 87
    Backup directory could not be created in /db_backups
    Notice: Undefined variable: f_err in /home/marlarbi/public_html/Cron/backup_dbs.php on line 87
    Backup directory could not be created in /db_backups Backup directory could not be created in /db_backups

  12. Hi, this script seems absolutely perfect for what a need, an automatic sql backup emailed to a remote server. But i’m getting this error below, any suggestions on what i can do to fix the issue?

    
    
    <b>Warning</b>:  mkdir() [<a href='function.mkdir' rel="nofollow">function.mkdir</a>]: open_basedir restriction in effect. File(/db_backups) is not within the allowed path(s): (/home/duplex:/usr/lib/php:/usr/local/lib/php:/tmp) in <b>/home/duplex/public_html/backup_dbs.php</b> on line <b>155</b>
    
    
    <b>Notice</b>:  Undefined variable:  f_log in <b>/home/duplex/public_html/backup_dbs.php</b> on line <b>85</b>
    Backup directory could not be created in /db_backups
    
    <b>Notice</b>:  Undefined variable:  f_err in <b>/home/duplex/public_html/backup_dbs.php</b> on line <b>85</b>
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  13. [...] Wer keinen direkten Zugriff auf den MySQL Server und somit keine direkte Möglichkeit von seinen Datenbanken Backups zu machen, dem möchte ich gerne dieses PHP MySQL Backup Script empfehlen. [...]

  14. Thanks for all the help getting it sorted for my site Admin, just the cron job to sort out now. I’ve been wanting to get automated db backups running for a long time, and you were a great help.

    Thanks,

    Jason

  15. I know this is not really relevent to your script, but I would appreciate your input on it.

    Let’s say I had access to the most recent version of my database at any time from the url http://username:password@domain.com:2082/getsqlbackup/dbname.gz - would it be possible to set up a script or cron job to download that file automatically and email it to me every 24 hours?

    Thanks,

    Jason

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.