I am trying to write a php script that will backup all my databases. However, this isn't working:

<?php

$host = 'myhost';
$user = 'myuser';
$pass = 'mypassword';
$backupDir = "/home/user/www/mysqlbackups";
$backupFileName = "backup.sql";

$back = $backupDir.$backupFileName;
system(sprintf("mysqldump --all-databases -h %s -u %s -p%s > %s",$host,$user,$pass,$back));
?>

I have also tried adding "or die (mysql_error());" to the end of the command, but I do not get any error message from this. However, if I try to echo something after the die statement, then it does echo...so I don't know what that means.

Anyway, I am a real newbie at this, so all help would be appreciated. Thank you very much!

    It looks like your $backupDir is wrong. Dont you need a '/' at the end of the string?

      Try doing this and posting the output.. (and you really need the / at the end of your backupdir)

      <?php
      $host = 'myhost';
      $user = 'myuser';
      $pass = 'mypassword';
      $backupDir = "/home/user/www/mysqlbackups/";
      $backupFileName = "backup.sql";

      $back = $backupDir.$backupFileName;
      $strCommand = sprintf("mysqldump --all-databases -h %s -u %s -p%s > %s",$host,$user,$pass,$back);
      echo 'Running: ' . $strCommand;
      echo "\n";
      echo shell_exec($strCommand);
      ?>

        Thank you, martinkronstad, for your help.
        Ok..here is the output:

        Running: mysqldump --all-databases -h myhost -u myuser -pmypassword >

        However, I still cannot retrieve the backup file...how do I know if the file is made? I've tried looking for it on the server, and on my browser, using the path from the $backupDir, however, with no success. Thank you for your help!

          Hi!

          Your output is missing the filename ... it should look like:

          mysqldump --all-databases -h myhost -u myuser -pmypassword > /home/user/www/mysqlbackups/backup.sql
          Are you running the exact code I gave you?

          Try do echo "Backupfile: $back";

          Also, I think (not sure) that you need to use -h%s and -u%s instead of -h %s and -u %s

            Hello!

            Thank you. I was missing this line of code:

            $back = $backupDir.$backupFileName;
            

            That would explain why $back did not output anything. Ok...here is the new output:

            Running: mysqldump --all-databases -hmyhost-umyuser -pmypassword> /home/user/mysqlbackups/backup.sql Backupfile: /home/user/mysqlbackups/backup.sql

            However, I am not sure if the dump actually happened. I have looked for the file on my server and pointed the browser to it too, but have had no success. Thank you for your help.

              Write a Reply...