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!

Topic: Scripts | RSS Feed

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

  1. 75
    Erik

    Hi, I’ve tried getting this to wotk for a long time now. I’m running as root, and when I run “php backup_dbs.php” it says “Executing MySQL Backup Script v1.4 - Processing Databases..”
    Then when I check the folder /db_backups, there are only the .err and the .log file there, no backup file! Argh! The .err file is empty, and the .log file just says the same thing as the script. Of course no email is sent. What am I doing wrong?

  2. 74
    Sean

    Got it! The problem was the location of the output dir ($BACKUP_DEST). When the script begins to compress files, its current location is your $BACKUP_TEMP. When it tried to place files into $BACKUP_DEST it couldn’t find it because that location is actually two folders down (i.e. ../../$BACKUP_DEST).

    So here is my change -
    Line 244: exec("cd $BACKUP_TEMP ; $USE_NICE tar cf ../../$BACKUP_DEST/$BACKUP_NAME * 2>&1", $output, $res);

    I also had to leave out the first preceding “/” in my $BACKUP_DEST and $BACKUP_TEMP variables.

    Thanks for your help - I hope this will help someone else!
    Sean

  3. 73
    Sean

    Thanks for the quick reply! The folder is empty, the file never gets created. I made sure the script was able to write to the directory… all of the .sql and bz2 files are sitting in the temp folder, but when it goes to compress them all into one file, I get that pesky error.

    I am understanding the script correctly, right? It takes all the files in the temp folder and then compresses them into one file, right?

    Thanks again for your help!
    Sean

  4. Sean: Have you looked in that directory to see if the file is there, but with perhaps another name? Or is it empty?

  5. 71
    Sean

    I have been at this for a 1.5 hours now and it’s breakin me down :P I keep getting the following…

    tar: /db_backups/mysql_backup_2007-01-17.tar: Cannot open: No such file or directory

    I’ve tried a few things on my own as well as some research, but no luck. Any help would be appreciated!

    Sean

  6. Omar: Since you do not have full control of the server, this script will not work for you very well.

    The good news is, it is very likely that your host has some sort of backup system in place already. Or you could ask them if there is some way you can download the backups regularly. Most will let you do this.

  7. Well the thing is I am not the host of the server and I am not on a dedicated server either. I just have my own account on the server which hosts many people.

  8. Omar: Are you running this as root? If so you should not be getting the permission denied error.

  9. I get this error everytime I try to run the files and cannot figure out why

    Warning: mkdir(/db_backups) [function.mkdir]: Permission denied in /home/hellomo/public_html/BAK/backup_dbs.php on line 155

    Notice: Undefined variable: f_log in /home/hellomo/public_html/BAK/backup_dbs.php on line 85
    Backup directory could not be created in /db_backups

    Notice: Undefined variable: f_err in /home/hellomo/public_html/BAK/backup_dbs.php on line 85
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  10. Ruta: I personally have not ever used it on a Windows server, but it should certainly work. Someone I was working with had contributed some code including support for Windows, which was tested and used by him.

  11. 65
    Ruta Lietuvninkas

    Has anyone actually gotten this script to work on Windows? If so, PLEASE let us know how cause I’ve been trying to get this to work for hours (no problem on Linux).

  12. Hi there… I have this lines in the start of the dumb .sql file
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=’NO_AUTO_VALUE_ON_ZERO’ */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

    The problem is that my db is using latin1 and the collate is utf8_general_ci and the back up is very wrong ( encoding).

  13. Backup: I do not think this will work very well on shared hosting - you probably do not even have shell access on it. You could always ask your host to see if they have any kind of backup system you can use though.

  14. How can configure it on shared hosting ? do we need extra privilages ?

  15. 61
    DeletedMYSQL

    Ok…I figured it out after losing the files. Temporary doesn’t have to mean that the script owns the directory. I’m sure some users will use c:\windows\temp or %temp% as their directory, which could have the same results if another program is using the directory or they have downloaded unzipped/unrar’d files open.

    I’m not slamming the script, I love it. I just think it needs to be a little more picky about what it touches :)

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.