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: « 1612 11 10 9 8 [7] 6 5 4 3 21 » Show All

  1. Muner: Here are some instructions for importing a database using PHPMyAdmin:

    http://www.modwest.com/help/kb6-335.html

  2. 104
    Muner khan

    how can i restore the back like i m using php interface n want to back n restore from php interface ..i have done creating db dump..but dont know how to recover it ..
    please help needed

  3. 103
    Emerson

    Is it possible to add something at the end of the script to delete yesterday’s backup file and log?

    Thanks ;)

  4. Bill: I have only used this on a few different servers, all of which I had root access to. The only other thing you can do, is in your control panel, where you assigned your user to your database, is make sure you have all permissions allowed, including lock tables. If you cannot do this, your host might be able to set it for you. Otherwise, you may not be able to use this script, as the locking is done by the command that actually dumps the dbs, and I am not aware of a way to bypass it since it needs exclusive access to the db to do it.

  5. I got a response that informed me the problem is the permissions available on shared hosting accounts, the do not allow the locktable command to be used.”

    Is there a solution to this as it seems that you need to “own” the box you are trying to use this script on. I do not have root access since it is a shared hosting environment.

    I would still like to use this script if it is possible.

    Bill

  6. This is on a shared hosting environment. I can log into phpMyAdmin with the exact same user/pass combo to manage my db’s and I can export via the interface. My host said to try localhost.localdomain.

    Your thoughts?

  7. Bill: It sounds like a permission error. Have you tried with the root mysql login?

  8. I have figured out the fodler issue by having to type the entire path, but I am confused as to the below error.

    2007-05-06 10:38:37: Executing MySQL Backup Script v1.4
    2007-05-06 10:38:37: Processing Databases..
    2007-05-06 10:38:37: DUMP FAILED
    mysqldump: Got error: 1044: Access denied for user ‘USERNAME_GOES_HERE’@'localhost’ to database db_name_goes_here_-officials’ when using LOCK TABLES
    2007-05-06 10:38:37: Compressed DB: db_name_goes_here_-_officials
    2007-05-06 10:38:37: Archiving files..
    2007-05-06 10:38:37: Backup complete!
    2007-05-06 10:38:37: *** ERRORS DETECTED! ***
    Check your email account $$$@$$$$$.$$$ for more information!
    2007-05-06 10:38:37: There were errors!
    2007-05-06 10:38:37: Emailing error log to $$$@$$$$$.$$$ ..

  9. It’s worth pointing out that I was having a fair bit of trouble with the script at first. It was failing to create the tar file. I thought it was a permissions issue at first, but then realised that my system didn’t support bzip2. I changed it to gzip and it works a treat now – thanks!

  10. Version 2.1 has been released. It includes a new option, which allows you to specify a comma separated list of databases to exclude from the backup.

  11. Jak: From the error message, it sounds like it is using too much memory. Perhaps the amount of memory PHP is set to use is too low for the size of your databases. You would have to ask your host about that one.

  12. [...] backup: Here is a great site that will allow for auto backup of your SQL database. There is even an option to [...]

  13. I received the following error message from an email that was sent from the script:

    Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to
    allocate 10158081 bytes) in /home/net/dbback/backup_dbs.php on line
    294

    What does the error means and how can I fix it.

    The script is set to email me the backup file. I see the backup (tar) file that was to be sent im the db_backups directory. The error log file in the same directory is empty.

  14. file_get_contents(c:/db_backups/mysql_backup_2007-03-06.zip) [function.file-get-contents]: failed to open stream: No such file or directory in C:\AppServ\www\backup\backup_dbs.php on line 293

    any ideas?

  15. when I ran the script I get the error :

    Compressed DB: balonlac_arkamarka
    Archiving files..
    FAILED
    tar: db_backups/mysql_backup_2007-03-06.tar: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now
    FAILED
    tar: db_backups/mysql_backup_2007-03-06.tar: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now

    ok, here is the part I couldn’t get;
    in the tmp folder I have a file called
    balonlac_arkamarka.sql.bz2
    (so this part works fine, and have the files in zip)
    and in the backup folder I have a file called
    mysql_backup_2007-03-06.log
    (this shows that folder is accsesible by the script)
    but I got the error. In the time being it is ok but’m planning to set up a cron job, so I’ve to fix it but couldn’t find where the problem is.
    (by the way there was a comment look like this one but this is not the same case)
    thanks …

Pages: « 1612 11 10 9 8 [7] 6 5 4 3 21 » 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.