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

  1. I too am having a issue with the zip portion on Windows. Winzip is installed on the server and included in the path. No matter what I try in the config or backup php I receive the messager ‘zip’ is not configured and an internal or external command.

  2. I manage to get it work, but now is the zip problem..I installed winzip but still have problem, is there a solution for this?

    appreciate for any help.

  3. I using wampserver, these are the error. Can any one help me on this?

    Warning: mkdir() [function.mkdir]: No such file or directory in D:\wamp\www\autobackup\backup_dbs.php on line 160

    Notice: Undefined index: f_log in D:\wamp\www\autobackup\backup_dbs.php on line 86
    Backup temp directory could not be created in /tmp/backup_temp

    Notice: Undefined index: f_err in D:\wamp\www\autobackup\backup_dbs.php on line 86
    Backup temp directory could not be created in /tmp/backup_temp
    Backup temp directory could not be created in /tmp/backup_temp

  4. 151
    kumar

    how to backup mysql database using php coding

  5. 150
    Devin

    Executing MySQL Backup Script v1.4
    Processing Databases..
    DUMP FAILED
    sh: tmp/backup_temp/agp.sql: Permission denied

  6. 149
    Michael

    This is a great script, thanks. I wanted to be able to auto delete old versions of the file, so I googled and found / modified this script to help me. I also execute your script inside of this.

    #!/bin/bash
    ## RSFBS - Really Simple File Backup Script by DaiTengu.
    ##

    ## Variables go here!
    # General base backup directory, All the subdirectories will be
    # created under here.
    scriptdir=/var/www/scripts

    #Execute php backup of mysql db
    php $scriptdir/backup_dbs.php

    basedir=/var/www/db_backups

    # Let’s name our backup file, the date will be appended to this
    backupname=mysql_backup_

    # How many backups do we want to keep? This should be in the format
    # days, weeks, months, etc. Example: keep=”7 days”
    keep=”7 days”

    # Some stuff to properly name the files
    suffix=$(date +%F)
    olddate=$(date –date “$keep ago” +%F)

    ## Delete the old tar file!
    if [ -e $basedir/$backupname$olddate.tar ] ; then
    echo “Deleting old tar file…”
    rm -rf $basedir/$backupname$olddate.tar
    fi

    ## Delete the old log!
    if [ -e $basedir/$backupname$olddate.log ]; then
    echo “Deleting old log…”
    rm -rf $basedir/$backupname$olddate.log
    fi

    ## Oh hay! We’re all done! Let’s clean up and exit gracefully!
    echo “Done!”
    exit 0;

  7. 148
    Mai

    Getting this error. Any advice?

    Warning: mkdir(/db_backups) [function.mkdir]: Permission denied in MY URL on line 156

    Notice: Undefined index: f_log in MY URL on line 86
    Backup directory could not be created in /db_backups

    Notice: Undefined index: f_err in MY URL on line 86
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  8. Thank you

  9. 146
    rolance

    For the previous windows XP users who reported the error: “zip is not recognized as an internal or external command,operable program or batch file” - the problem is either that you do not have zip.exe installed on your system (you will have to hunt it down) or you have zip but have not configured your path environment variable to make it accessible from the update php script. This is a nice script for unix users, but it doesn’t work ‘out of the box’ on windows and will take quite a bit of hacking, so newbies might want to move on.

  10. 145
    Ayuk Rolannd

    Thank you very much for the script this is wnat I have been looking for all day long, but i seen to have some errors in making the script work
    I am using Xampp install on windows so I want I have the following error trying to make the script backup my database automaticaly.

    DUMP FAILED
    ‘C:\Documents’ is not recognized as an internal or external command,
    operable program or batch file.
    DUMP FAILED
    ‘C:\Documents’ is not recognized as an internal or external command,
    operable program or batch file.
    COMPRESSION FAILED
    ‘zip’ is not recognized as an internal or external command,
    operable program or batch file.
    I don’t know how to go about this please try to help me i am new in
    database administration

  11. Thank you very much for the script…
    I keep getting this error…

    Warning: mysql_list_dbs() [function.mysql-list-dbs]: Unable to save MySQL query result in /data/13/1/112/58/1275710/user/1361670/htdocs/mysqldump/backup_dbs.php on line 187

    Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /data/13/1/112/58/1275710/user/1361670/htdocs/mysqldump/backup_dbs.php on line 189

    Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /data/13/1/112/58/1275710/user/1361670/htdocs/mysqldump/backup_dbs.php on line 241
    Archiving files..
    FAILED
    tar: db_backups/mysql_backup_2008-05-17.tar: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now
    FAILED
    tar: db_backups/mysql_backup_2008-05-17.tar: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now

  12. 143
    Miles

    Hi Excelent but cant get it to work, it reports all db’s backed up & compressed it even sends the email witht he attached zip file. when i open the file though it is empty. i have tried adjusting the config file as follows:
    // Path to the mysql commands (mysqldump, mysqladmin, etc..)
    $MYSQL_PATH = 'C:\Program Files\MySQL\MySQL Server 5.0\bin';

    still the same empty zip file although all reports sucessful backup. is there anything else i can try?

  13. 142
    Paul

    Thanks for the script. It was working fine for me on two different servers. However, after updating the script to the most recent version on one of the servers I get

    Warning: Cannot modify header information - headers already sent by (output started at /home/***/public_html/****/backup_dbs.php:1) in /home/***/public_html/***/backup_dbs.php on line 131

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

    Errors.

    Any ideas why?

  14. Hi, first of all thank you for creating this script!

    I’ve succeeded installing your script. When I ran it; the backup file was created successfully although i saw error message as follows:

    
    Failed loading /usr/local/IonCube/ioncube_loader_lin_5.2.so:  /usr/local/IonCube/ioncube_loader_lin_5.2.so: cannot open shared object file: No such file or directory
    

    Do i really need this IonCube?

    Thanks before.

  15. Hi Guys,
    Love this script but im having one issue. See below
    Im using a windows xp machine with winzip installed

    Dumped DB: mainzpanel COMPRESSION FAILED ‘zip’ is not recognized as an internal or external command, operable program or batch file. COMPRESSION FAILED ‘zip’ is not recognized as an internal or external command, operable program or batch file. Emailing mainzpanel backup to

  16. If you are getting the following error and error log will not show anything else …
    “2007-10-24 03:10:02: Executing MySQL Backup Script v1.4
    2007-10-24 03:10:02: Processing Databases..”

    you need to install php5-mysql package.
    Ubuntu you can do so by running the following command:

    for PHP5

    
    # sudo apt-get install php5-mysql   
    

    for PHP4
    
    # sudo apt-get install php4-mysql   
    

    Now everything works so cool … thx for such a cool utility.

    Dash

  17. –> M. Keijzer

    It seems that you *might* need to verify User/Group permissions.

    Check that ‘apache’ (or whatever user and/or group used by your web server) have WRITE permission on the sub-folder(s) you configured.

    You may also need WRITE permission for this user / group on the root or main folder, since sub-folders are created if non-existent.

    I’m not sure about Windows 2003 but I had the same issue running on Cent OS 5.1 and after fixing permissions everything works perfectly.

  18. I use the backup-script on Windows Server 2003 with PHP and MySQL.
    I created the directory’s, it does write a log-file, but is doesnt’t write the backup-files.
    What do I have to configure?

    Of course on my Ubuntu-Linux testing-server it works perfectly…

  19. For anyone using GoDaddy with php 5.0 and MySQL 5. This is the path needed for mysql commands (mysqldump, mysqladmin, etc..)

    $MYSQL_PATH = ‘/usr/local/mysql-5.0/bin’;

  20. 135
    daniele

    hi,
    i’m trying this script but i experienced this:

    The server encountered an internal error or misconfiguration and was unable to complete your request.
    
    Please contact the server administrator, postmaster@site.it and inform them of the time the error occurred, and anything you might have done that may have caused the error.
    
    More information about this error may be available in the server error log.

    what happen?

    thanks a lot, daniele.-

  21. thanks, its great, i added hours/minutes to the filename so that it doesnt overwrite the older file from the same day (useful for reverting to eariler DB from that day)

    $BACKUP_NAME = ‘mysql_backup_’ . date(’Y-m-d_Hi’);

    Thanks!

  22. 133
    Omid

    Hi again
    Now I have this error:

    Warning: mkdir() [function.mkdir]: No such file or directory in /home/user/public_html/mydb/backup_dbs.php on line 156

    Notice: Undefined index: f_log in /home/user/public_html/mydb/backup_dbs.php on line 86
    Backup directory could not be created in home/user/export_mu

    Notice: Undefined index: f_err in /home/user/public_html/mydb/backup_dbs.php on line 86
    Backup directory could not be created in home/user/export_mu
    Backup directory could not be created in home/user/export_mu

    Could you please tell me what`s exact reason of above error ?
    I couldn`t find my answer in other comments :(

  23. 132
    Omid

    Hi
    Warning: exec() has been disabled for security reasons in /home/name/public_html/mydb/backup_dbs.php on line 219
    How could I modify above warning ?

  24. 131
    X.A.

    Works like a charm!

  25. I had hoped that this script would be my saviour, but despite my best efforts I cannot get it to work - purely because of the site I am running it on using PLESK which does not allow me to change database user permissions or to run as root user (despite it being a dedicated box).

    All I get is:

    
    Executing MySQL Backup Script v1.4
    Processing Databases..
    DUMP FAILED
    
    DUMP FAILED
    
    COMPRESSION FAILED
    
    COMPRESSION FAILED
    
    Archiving files.. 
    FAILED
    
    FAILED
    
    
    *** ERRORS DETECTED! ***
    Check your email account *************** for more information!
    
    
    Emailing backup to *************** .. 
    
    There were errors!
    Emailing error log to *************** .. 
    FAILED to email error log.
    FAILED to email error log.
    

    I tried using username root and the password I have for SSH access but that doesnt work either. If anyone can give me any pointers I really would appreciate it.

    I have no doubt that I’ll be using the script on other sites, its just a shame that I cannot get it to work on the one site I needed it on :(

  26. wow.. i was lost in a world of pearl and bashings.. and this script did the work, right off the bat.. thanks a million.

  27. 128
    Karl Roos

    Strange :S
    I get this..

    Warning: mkdir() [function.mkdir]: Permission denied in /home/web28141/domains/roosstudios.com/public_html/Backup/backup_dbs.php on line 156

    Notice: Undefined index: f_log in /home/web28141/domains/roosstudios.com/public_html/Backup/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups

    Notice: Undefined index: f_err in /home/web28141/domains/roosstudios.com/public_html/Backup/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  28. Thanks for this brilliant script!

    I’ve set it up so it backs up to the server daily and emails me a backup weekly :)

    Martin

  29. 126
    Rafaiel Sabatini

    what will be $MYSQL_PATH in windows. Could you please give an example.Thanks

  30. When I manually run the script, This is what I get to see:

    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups

  31. 124
    Tim

    I am running a MySQL DB backup script. The script has been working fine for 1-2 weeks. However, it is getting an error today.

    PHP Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 15288321 bytes) in /home/[deleted]/public_html/[deleted]/backup_dbs.php on line 305

    Here is the code that causes the error.

    $data = chunk_split(base64_encode(file_get_contents($BACKUP_DEST.’/’.$BACKUP_NAME)));

    Do you think it is the way the code that is written can’t handle the growning DB? The memory that it tries to allocate does increase with each failed script execution.

  32. Bump on Post 119. I also would like some information on the EXLUDE_DB function. Otherwise the script works just fine (it backs up my DB:s too a Gmail account, thanks Google for 6 GB storage!).

  33. 122
    yehsay

    I am looking to install this on my Mac OS X Server and was wondering if it works? I have installed it but am receiving this error “2007-11-27 15:41:12: DUMP FAILED
    mysqldump: Got error: 1045: Access denied for user ‘root’@'172.25.64.227′ (using password: YES) when trying to connect”

    My web server has 2 IPs that it is running, how can I limit it to the the other IP. Also, when I manually run the script, my .sql files are 0kb in size. Am i doing something wrong here. Please assist. Thank you.

  34. 121
    Jasper

    Never mind my comment, I just couldn’t figure the odd way the messages are ordered. (from down to up, yet from 1 to 8 - I expected either from up to down, from 1 to 8 or down to up, from 8 to 1. I saw from down to up and falsely concluded it would be from 8 to 1.)

  35. 120
    Jasper

    I had that very, very same errors. CHmodding to 777 helped, though.

    The solution to create the folders already worked as a temporary solution for the first error thrown - it gave me the second error Henry posted. After CHModding the both folders to 777, the script ran without any errors.
    The script deletes the temporary directory however, so creating the directories already is not going to be any step towards a permanent solution.

  36. I would like to use the “Exclude” function, but for some reason, it is not excluding any database. I have the excluded databases listed with a “,” and space in between databases. Is there another way to list then, like with no space between the “,” and next database name? Also, it backs up the “information_schema” database.

    Any ideas on what is or what I am doing wrong? Other than this, it works fine. But I do not want all databases emailed.

    Thanks.

  37. ok i have a strange problem
    i have installed the script and if i run it from IE or firefox it works a treat but if i run it as a cron job it seems to just hang all i get in the log is

    "2007-10-24 03:10:02: Executing MySQL Backup Script v1.4
    2007-10-24 03:10:02: Processing Databases.."

    and nothing else
    the error file is blank
    so it appears the the cron job is starting the script ok but it just doesnt seem like the script will complete
    any idea’s?

  38. I just wanted to tell you a BIG THANK YOU for creating this script.
    It was a real help for me as I have a slow internet connection and its a real pain in the head to manually download those big backups.

  39. Hi I am try desperatley to get your script working. I have set the permissions of the directory and the 2 files to 777, however I still keep geting the following messsage on the screen when I try running it: A file permissions error has occurred. Please check the permissions on the script and the directory it is in and try again. Have you got any ideas for me thanks

  40. 115
    mike

    and what i mean by that (the breaking up files) is not doing each sql separately, but to where it will take a set size, and break the zip files up into those sizes and email each part as a separate email.

    as to the second part, i have a few directories that i need to have backed up with the databases, as they are interlinked (basically the site if you upload a file, will put it in the db (the page reference) and save it to a directory)

  41. 114
    mike

    2 things, is there a way to have it break up zip files (windows) and email the sections separately, as the files can become VERY large, and 2, is there a way to tell it to backup directories and make them part of the backup?

  42. 113
    Jonny

    I had the same problem reported below with the creation of the backup directory failing.

    I fixed it by specifying the full path to where I wanted the backup directory. So if you want them in the directory the backup script is stored, change the path in config to the same path as is shown in the error message (less the backup_dbs.php bit).

    So for:

    /home/chs/calellabarcelona.com/home/html/admin/backup/backup_dbs.php

    Set the backup path in config to:

    /home/chs/calellabarcelona.com/home/html/admin/backup/

  43. @Geo: do you have ‘exec’ function enabled in PHP? If not, this script won’t work…

    I like this script, but I have a problem when restoring the backup: phpMyAdmin returns ‘error in your SQL syntax’. If I do manual backup with phpMyAdmin and restore that, it works. Any ideas?

  44. This seems like it’ll do it but my problem is that it never creates any output at all. It creates the log file and it says it dumped the databases but NADA….
    I need to use gzip since I don’t have tar. I’m on a novell server so I am a little bit uncertain about the paths but since it creates the log file it should be ok.
    I also get the download page, and I don’t know why…..
    Anybody got a clue?

  45. Hi. Thank you very much for a useful code. I have installed it, and have tested it by running the php files from browsers IE (Internet Explorer), Opera and Firefox. The program works fine in Opera and Firefox, and the following code is reported on the screen:

    Executing MySQL Backup Script v1.4
    Processing Databases..
    Dumped DB: XYZ
    Compressed DB: XYZ
    Archiving files..
    Backup complete!

    However, when running from IE the job is being done (DB backup is created), but suddenly an error message appears in the IE browser:

    Internet Explorer can not download backup_dbs.php from http://www.cosmotorg.net.

    Why is the program causing IE to try to download anything at all? It shouldn’t be necessary. The progress should be reported on the screen, as it happens in Opera and Firefox.

  46. Will this script work on a 200MB DB in phpadmin, the .sql backup file is a little over 300MB? If so would it need any changes different than your instructions?

    Of course I do not want the backup e-mailed to me. :-)

    Thank You for your time!
    David

  47. 107
    Mathias

    I am having the exact same problem as Gabi (#106).

    Exact message is:

    <br />
    <b>Warning</b>:  mkdir(/db_backups): Permission denied in <b>/hsphere/local/home/mjcraig/bluenetwork.org/DB_Backup/backup_dbs.php</b> on line <b>156</b><br />
    <br />
    <b>Notice</b>:  Undefined variable:  f_log in <b>/hsphere/local/home/mjcraig/bluenetwork.org/DB_Backup/backup_dbs.php</b> on line <b>86</b><br />
    Backup directory could not be created in /db_backups
    <br />
    <b>Notice</b>:  Undefined variable:  f_err in <b>/hsphere/local/home/mjcraig/bluenetwork.org/DB_Backup/backup_dbs.php</b> on line <b>86</b><br />
    Backup directory could not be created in /db_backups
    Backup directory could not be created in /db_backups
    

  48. 106
    Gabi

    Hi all,

    I’ve installed and configured but have a permission error in mkdir. Then i have created the directory and do 777 perms but have the same result:

    Warning: mkdir() [function.mkdir]: Permission denied in /home/chs/calellabarcelona.com/home/html/admin/backup/backup_dbs.php on line 156

    Notice: Undefined index: f_log in /home/chs/calellabarcelona.com/home/html/admin/backup/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups
    Notice: Undefined index: f_err in /home/chs/calellabarcelona.com/home/html/admin/backup/backup_dbs.php on line 86
    Backup directory could not be created in /db_backups Backup directory could not be created in /db_backups

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

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

  50. 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

  51. 103
    Emerson

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

    Thanks ;)

  52. 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.

  53. 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

  54. 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?

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

  56. 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 $$$@$$$$$.$$$ ..

  57. 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!

  58. 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.

  59. 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.

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

  61. 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.

  62. 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?

  63. 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 …

  64. Phillip: See comment 86.

  65. 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

  66. 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.

  67. 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)

  68. 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?

  69. 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

  70. Dennis: Thanks for the info :)

  71. 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

  72. 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!

  73. 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…

  74. 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…

  75. 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;

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

  77. 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..

  78. 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.

  79. 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?

  80. 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

  81. 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

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

  83. 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

  84. 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.

  85. 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.

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

  87. 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

  88. 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.

  89. 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).

  90. 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).

  91. 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.

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

  93. 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 :)

  94. 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.

  95. 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.

  96. 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.

  97. 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 :(

  98. 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.

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

  100. 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.

  101. 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.

  102. 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…

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

  104. 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

  105. 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

  106. [...] 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. [...]

  107. 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

  108. 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

  109. 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.

  110. [...] 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. [...]

  111. 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?

  112. 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

  113. 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.

  114. 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

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

  116. 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.

  117. 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?

  118. 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;

  119. 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.

  120. 34
    Daniel

    Hello Admin,

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

  121. 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'); ?>

  122. 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

  123. 31
    Richard

    -

  124. Pedro: I have not ever used this script in a Windows environment. I suppose you might be able to use WinZip or something and just adjust the script accordingly. Maybe another Windows user can suggest something for you.

  125. 29
    Pedro Fonseca

    i need the zip.exe for windows 32, right??

    Where can i find it?? thanks

  126. 28