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. Phillip: See comment 86.

  2. Hi,

    I’ve just configured and uploaded the two files.

    When i navigate to the path in my browser i am given the following error message:

    Warning: mkdir() [function.mkdir]: Permission denied in /websites/LinuxPackage03/fa/nc/yd/fancydressretail.co.uk/public_html/backup_dbs.php on line 155

    Notice: Undefined index: f_log in /websites/LinuxPackage03/fa/nc/yd/fancydressretail.co.uk/public_html/backup_dbs.php on line 85
    Backup directory could not be created in /db_backups
    Notice: Undefined index: f_err in /websites/LinuxPackage03/fa/nc/yd/fancydressretail.co.uk/public_html/backup_dbs.php on line 85
    Backup directory could not be created in /db_backups Backup directory could not be created in /db_backups

    The section where i’m not sure what to put in the config is the $MYSQL_PATH = ‘/usr/bin’; as i am not sure where i need to look to find and alter this path.

    If anyone could let me know how to go about this it woulb be appreciated.

    Many Thanks

  3. Igor: mysqldump is not part of php - It is the program used to dump the contents of mysql databases. You might have to search your server for the location of it.

  4. Hi list,

    I’d LOVE to get this script in plcae on my host!

    I keep getting the same error as Brian (post number 20). Mye error shows:

    2007-02-27 08:28:13: DUMP FAILED
    sh: line 1: /usr/bin/mysqldump: No such file or directory

    So, I need to change the directory in my script config-file but I don’t know to which location? Would a ‘phpinfo’ help to get that info? (mine can be found at http://www.bmwboxersupplies.com/tmp/phpinfo.php)

  5. Paul: It looks like the main problem is those permission denied errors. It causes a chain reaction making the rest of the script fail. Are you running the script as root? Does the root user account have access to the mysql root account?

  6. Hi,Sorry to trouble you but I am experiencing a slight problem. When I run the script, the backup file seems to make it as far as the tmp/backup_temp folder and the two logs are producced in the backups folder. However, I am still receiving an error message (below) and would be really grateful for any advice please.

    Executing MySQL Backup Script v1.4 Processing Databases.. Dumped DB: db139096744 COMPRESSION FAILED nice: tmp/backup_temp/db139096744.sql: Permission denied COMPRESSION FAILED nice: tmp/backup_temp/db139096744.sql: Permission denied Archiving files.. FAILED tar: backups/pco_backup_2007-02-26.tar: Cannot open: No such file or directory tar: Error is not recoverable: exiting now FAILED tar: backups/pco_backup_2007-02-26.tar: Cannot open: No such file or directory tar: Error is not recoverable: exiting now *** ERRORS DETECTED! *** Check your email account paul@xyz.co.uk for more information! There were errors! Emailing error log to paul@xyz.co.uk ..
    Warning: file_get_contents(backups/pco_backup_2007-02-26.err) [function.file-get-contents]: failed to open stream: No such file or directory in /homepages/30/d116518341/htdocs/admin/backup_dbs.php on line 330

    Warning: closedir(): supplied argument is not a valid Directory resource in /homepages/30/d116518341/htdocs/admin/backup_dbs.php on line 372

    Warning: rmdir(tmp/backup_temp) [function.rmdir]: No such file or directory in /homepages/30/d116518341/htdocs/admin/backup_dbs.php on line 375

  7. Dennis: Thanks for the info :)

  8. Oh.. before I forgot, I think we can call this a bug (although I doesn’t hurt anyone :D):
    in the exec function when running mysqldump from shell command there is something like “2>&1 >$BACKUP_TEMP/$db.sql”, which by simple logic will not work properly. I would work good if changed to “>$BACKUP_TEMP/$db.sql 2>&1″, because you should first specify STDOUT point (by “>$BACKUP_TEMP/$db.sql”) and only after that you can point STDERR to STDOUT (by “2>&1″). In your case STDERR is pointed to terminal… :) so you loose all your error output..
    Have a nice day

  9. Nice script. I was just looking for a ready-to-use one to backup my website, cause I get a bit scary when thinking of all my work (and paid members’ profiles :D) crashing down.
    I am just too lazy to write a program myself, but it was a great pleasure for me to modify it a bit (I guess I will send a modified version to you later when I have some time, it is good enough.. at least I think so :D)
    I added a capability to list databases you want to backup, and also tables to exclude for every db listed (as I thought it is much easier to exclude unneeded tables than to list needed ones :D)…
    Have a nice day!

  10. 81
    Mastermind

    Nice examples thanks!
    Can you make a phpbb 3.x multi-forum mod when rc1 version will be released?
    There are thousands of people looking for this…

  11. 80
    Mastermind

    Wow you guys are making a fantastic job by publishing those tutorials and ready to download scripts! I am a php newbie so this is like gold for me, because I’m learning from best secured scripts…
    I want to make a little suggestion for php guru’s here :) > Can you make a multi-forum mod for phpbb 3.x? when rc1 will be released? There are more people looking for this than you can imagine!! Thanks again…

  12. Rob: Here is one method. Find this line in the script:

    $db = $db_row->Database;

    Right after it, add something like this for each one you want to skip:

    if ($db == 'database_whatever') break;

  13. Where are the details on exluding databases? I see the link in the page contents, but not in the page.
    Thanks….

  14. 77
    Erik

    I’ve tried re-downloading, but with no change. I’m not very experienced in the Linux world, is there some special configuration of PHP or MySQL that needs to be taken in concideration? Is there nothing that needs to be changed in the main file? I’ve noticed that there is nowhere I can configure witch database to backup, does the script automatically search for the databases? I really can’t figure this out..

  15. Erik: If it is not adding anything to the error file, this generally means that something is failing early on in the script. Most commonly, a configuration problem.

    It would also not hurt to re-download a fresh copy just in case something was accidentally changed in the code, or a bad download.

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.