Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Backup mysql file empty

mysql backup

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

Junior Member

5+ Year Member

joined:July 24, 2009
posts: 113
votes: 0


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


$tabel = $_POST['tabel'];

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

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


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

echo $content;

4:43 pm on July 16, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 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. :-)

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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members