homepage Welcome to WebmasterWorld Guest from 54.204.127.56
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Backup mysql file empty
mysql backup
chrissim




msg:4171543
 2:38 pm on Jul 16, 2010 (gmt 0)

hi,

i tried to backup one of my table, the sql file backup & created but empty content. What would be the problem here anyway?


<?php

$tabel = $_POST['tabel'];

$listTabel = "";
foreach($tabel as $namatabel)
{
$listTabel .= $namatabel." ";
}

$command = "mysqldump -u".$dbUser." -p".$dbPass." ".$dbName." ".$listTabel." > ".$dbName.".sql";

exec($command);

header("Content-Disposition: attachment; filename=".$dbName.".sql");
header("Content-type: application/download");
$fp = fopen($dbName.".sql", 'r');
$content = fread($fp, filesize($dbName.".sql"));
fclose($fp);

echo $content;
exit;
?>


 

rocknbil




msg:4171650
 4:43 pm on Jul 16, 2010 (gmt 0)

When you do this

>

you are executing a system command to dump into "file" and it will dump whatever is output from mysql dump. mysqldump is returning nothing.

These can be a bear to debug. It varies from system to system, anything from an authentication error to a missing switch to the switches not being in the right order to . . . who knows. :-)

I'm guessing, a couple things, no space or = setting the variable, no hostname or --h switch, but it could be other things. Even if this works,

-u".$dbUser." -p".$dbPass."

you have

-uusername -ppassword

when you need

-u username -p password

Try this one, it works for me, sub out your values. I have defined them as constants (the parts IN_CAPITALS) in this program, use variables if you like. As you see, I left a comment in for one that hung me up for a couple hours. :-)

$result=null;
$file = DB . '-' . $current_date . '.sql'; // DB = database name
// So you get "your-database-2010-07-16.sql"
// DB_BU_DIR = '/path/to/backup/directory/' make sure it's writable

# connecting remote requires hostname --h
$cmd = 'mysqldump -h ' . DBHOST . ' --user=' . DBUSER . ' --password=' . DBPASSWORD . ' ' . DB . ' > ' . DB_BU_DIR . "$file";

$result = system($cmd);

if ($result) { die("Error, something went goofy: $result"); }
else { echo "Success"; }

$result should be null or 0, but it's no guarantee it won't give you an empty file if something is . . . goofy. It will most often have a numeric value, and you'll need to look up the documentation for your O.S. to find out what the number means. Like I said, can be a bear . . .

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved