1

I'm trying to dump a mysql database (back-up). I have the following script. The passthru works and it downloads the file to the client, but I also want it to save a copy of the back-up .gz file on the server, in the /backup/ folder. I can't get it to work. The .gz file downloads to the user, but doesn't get put into the /backup folder.

    $filename = $thismoment . "_" . $_GET["t"] . ".gz";
    $mime = "application/x-gzip";

    system('mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip > /backup/' . $filename);

    header( "Content-Type: " . $mime );
    header( 'Content-Disposition: attachment; filename="' . $filename . '"' );

    $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";   

    passthru( $cmd );

    exit(0);
Laurens Swart
  • 1,234
  • 9
  • 24
  • Do you have write permission for that directory? Actually does user `www` have permission ;) – Todor Simeonov May 21 '17 at 00:20
  • On a Windows server, how would I make sure that my PHP script can write a file? – Laurens Swart May 21 '17 at 00:29
  • I've re-made the /backup/ directory following this tutorial: http://stackoverflow.com/questions/2900690/how-do-i-give-php-write-access-to-a-directory And the .txt file gets created. The back-up file however still doesn't. – Laurens Swart May 21 '17 at 00:31
  • Two thoughts. (1) have you confirmed that you have `system()` access on your IIS? (2) have you tried using the *full windows path* `C:\\backup\\` instead of just `/backup/` ? – Abela May 21 '17 at 01:38
  • 1. I've also tried exec() if that makes any difference. 2. Still doesn't work.... – Laurens Swart May 21 '17 at 08:40

1 Answers1

0

Managed to get it working by combining the two commands into one and specifying the full path to the mysqldump (without .exe...)

    $filename = $thismoment . "_" . $_GET["t"] . ".gz";
    $mime = "application/x-gzip";

    header( "Content-Type: " . $mime );
    header( 'Content-Disposition: attachment; filename="' . $filename . '"' );

    $cmd = 'D:\Web\XAMPP\mysql\bin\mysqldump --opt -u '.$_user.' -p'.$_pass.' '.$_db.' | gzip --best > backup/' . $filename; 

    passthru( $cmd );

    exit(0);

Thanks for the help! You pointed me in the right direction.

Laurens Swart
  • 1,234
  • 9
  • 24