Hello,

I am a real newbie, trying to learn how to use mysqldump. This script is not working to backup all the databases on my server:

<?php  
$dbhost = 'localhost'; $dbuser = 'myuser'; $dbpass = 'mypass'; $backupdir = '/home/user/www/mysqlbackups/'; $command = "mysqldump --opt -h$dbhost -u$dbuser -p$dbpass --all-databases > $backupdir"; system($command) or die('could not backup'); ?>

I have searched through countless examples of mysqldump scripts, yet I cannot see why my script isn't working. The only output that I get is "could not backup".

Does anyone know if I have to specify a name for the dump file? If so, where in the script? Also, does my path to the backup directory seem ok? Do I need the forward slashes both at the beginning and end of the string?

I think that this script should save the dump file on the server, in the directory specified by $backupdir. And I should be able to see the file online. Am I wrong?

Finally, is there anything extra that I would need to include in the script, since I am saving the dump file in a password protected directory?

I would really appreciate any help that you could give me. Thank you.

    put mysql_error() instead of could not backup

    MTG

      Run it from the command line. Don't try to kick of mysqldump from PHP.

      Just use the CLI, run it from a shell script from cron or something.

      Mark

        mrtechguy,

        I tried the script with mysql_error() instead of "could not backup", but it did not output anything, and still no mysql dump file. Thank you for any suggestions.

        MarkR,

        Thank you! I think I will try to read up on that, and figure out how to do that with cron.

          I think everyone may simply be overlooking the obvious fact that your user name on your hosted account is not very likely "user". It sounds to me like you copy/pasted the example code and didn't specify the actual, correct directory name on the volume to which you are attempting to dump the file, thus the error message.

          $backupdir should probably be something more like '/home/phencesgirl/www/mysqlbackups/'. And make sure that the mysqlbackups directory actually exists and has the relevant file permissions, or the dump will probably fail with the same, uninformative error message.

            Thank you for your reply, cbj4074. However, in the actual script that I am working with, I have replaced "user" with my real user name. And I have a directory called mysqlbackups (which is password- protected) in the www folder. Is it possible that the password protection is causing the script not to work?

              Yes, try removing the password to the directory and see if that help, also does the apache user have the privledge to write to that directory?

              MTG

                Hello,
                I am still having problems with trying to mysqldump all my databases into a file (preferably an sql file). I have tried running mysqldump from shell, which gave me a blank file in the backup directory that I specified. Here is the shell script:

                #!/bin/bash\par
                TIMESTAMP=date +%m-%d-%y-%H%M\par
                cd /home/myuser/www/mysqlbackups\par
                find /home/myuser/www/mysqlbackups/*.tar.gz -mtime +7 -exec rm -f {} ;\par

                my database\par

                mysqldump --opt -c -e -Q -u myuser -p'*********' --all-databases > alldata.sql tar czpf alldata.$TIMESTAMP.tar.gz alldata.sql rm -f alldata.sql\par
                }

                Also, removing the password to the directory does not seem to help. And the user does have privilege to write to the mysqlbackups directory.

                Now, my goal is to create a php script that I can set up with cron to run each night at 1am.
                Here is another php script that I am trying to get to work:

                <?php    //this is mysqlbackups2
                $host  = "localhost"; 
                $user  = "myuser"; 
                $pass  = "****************";
                
                $backupDir  = "/home/user/www/mysqlbackups/";
                $backupFileName = "blah.sql"; 
                
                $conn = mysql_connect($host, $user, $pass) or die(mysql_error());
                
                $back = $backupDir.$backupFileName;
                $strCommand = sprintf("mysqldump --force --all-databases -h%s -u%s -p%s > %s",$host,$user,$pass,$back) or die (mysql_error());
                echo 'Running: ' . $strCommand;
                echo "\n";
                echo shell_exec($strCommand);
                echo "Backupfile: $back";
                ?>
                

                With this script, the echos work, but there is no file created in the $backupDir. Can anyone tell me why this isn't working? Thank you so much!

                  1. Login to your shell account
                  2. Run the mysqldump command from the command line redirecting the output into a file with the > operator
                  3. Any errors which occur will either appear in the shell window or be sent into the output file
                  4. Remove any PHP scripts which invoke mysqldump, or any CGI or similar which are web-accessible.

                  Mark

                    a month later

                    Here may be a solution to no files / empty files when trying to do mysqldump from a php script for backup purposes.

                    /*

                    I downloaded this original script from: http://www.sematopia.com/?p=61

                    I had visited many different websites on google -

                    I had what appears to be a very common problem among users... They attempted
                    to have a php script that backed up their mysql database via mysqldump ...

                    The common problem was that the script generated an empty file....

                    I had spent many hours and finally, made it to this script, which is WORKING
                    for me.....

                    For your convenience, I included the original script, as well as my implementation
                    below. My actual implementation is a version without email functionality.

                    It is dropping the files into my webhosting home directory......

                    The database server is different than my webhosting home directory which makes
                    things more difficult but that is addressed in my implementation of the script.

                    I am also using a YmdHis implementation of the date which generates file names
                    as follows: cwphpbackup20060913195214 - and allows me to backup once per second,
                    but the reason I did this is so that if I am attacked, I can find the appropriate
                    file to restore to..... This sure beats the daily cron jobs allowed by my web host...

                    Now when I want to backup, I just open up my browser, and click the backup bookmark
                    which is http://www.example.com/backup.php .....

                    */

                    <?php

                    /*
                    ORIGINAL SCRIPT
                    Quickly and easily backup your MySQL database and have the
                    tgz emailed to you.

                    You need PEAR installed with the Mail and Mail_Mime packages
                    installed.  Read more about PEAR here: [url]http://pear.php.net[/url]
                    
                    This will work in any *nix enviornment.  Make sure you have
                    write access to your /tmp directory.

                    */

                    require_once('Mail.php');
                    require_once('Mail/mime.php');
                    
                    // location of your temp directory
                    $tmpDir = "/tmp/";
                    // username for MySQL
                    $user = "root";
                    // password for MySQl
                    $password = "pass";
                    // database name to backup
                    $dbName = "db";
                    // the zip file emailed to you will have this prefixed
                    $prefix = "db_";
                    
                    // email settings...
                    $to = "someone@gmail.com";
                    $from = "another@gmail.com";
                    $subject = "db - backup";
                    $sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql";
                    $attachment = $tmpDir.$prefix.date('Y_m_d').".tgz";
                    
                    $creatBackup = "mysqldump -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;
                    $createZip = "tar cvzf $attachment $sqlFile";
                    exec($creatBackup);
                    exec($createZip);
                    
                    $headers = array('From'    => $from, 'Subject' => $subject);
                    $textMessage = $attachment;
                    $htmlMessage = "";
                    
                    $mime = new Mail_Mime("\n");
                    $mime->setTxtBody($textMessage);
                    $mime->setHtmlBody($htmlMessage);
                    $mime->addAttachment($attachment, 'text/plain');
                    $body = $mime->get();
                    $hdrs = $mime->headers($headers);
                    $mail = &Mail::factory('mail');
                    $mail->send($to, $hdrs, $body);
                    
                    unlink($sqlFile);
                    unlink($attachment);

                    ?>

                    =====================

                    <?php

                    /
                    MY IMPLEMENTATION
                    /
                    // location of your temp directory
                    $tmpDir = "/usr/home/username/";
                    // username for MySQL
                    $user = "username";
                    // password for MySQl
                    $password = "password";
                    // database name to backup
                    $dbName = "databasename not table name";
                    // the zip file emailed to you will have this prefixed
                    $prefix = "cwphpbackup";

                    // email settings...
                    $to = "email@example.com";
                    $from = "email@example.com";
                    $subject = "db - backup";
                    $sqlFile = $tmpDir.$prefix.date('YmdHis').".sql";
                    $attachment = $tmpDir.$prefix.date('YmdHis').".tgz";
                    
                    $creatBackup = "/usr/local/bin/mysqldump -u ".$user." --password=".$password." -hdatabaseserver.example.com ".$dbName." > ".$sqlFile;
                    $createZip = "tar cvzf $attachment $sqlFile";
                    exec($creatBackup);
                    exec($createZip);

                    echo "backed up";
                    ?>

                      Write a Reply...