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

PHP Server Side Scripting Forum

Something wrong ? using backtics to back up data base
mysqldump doesn't work programmatically

 8:30 pm on Apr 25, 2012 (gmt 0)

Hi All,

I am trying to write a database backup program utilizing HTML and PHP using an $.ajax call to the server. This will not work using "backtics" or excec($command). If this same php code is called as php -f "FileName.php" it works just fine.

Thanks in advance for any help you can give me.

This is the ajax call:

url: 'DatabaseBakup.php',
data: str,
success: function(data) {

The PHP code is:


//Show errors
ini_set('display_errors', 'On');
ini_set('html_errors', 'On');

//$database=$_GET[db]; call in progress through $.ajax

$dir="/home/rick/DB-Web"; // for testing
$database="accounts"; // ditto

$format="m.d.y_G.i.s"; // month, day, year _ hr, min, sec

$filename=$dir."/$database". "/".strtoupper ($database)."_DUMP_". date($format).".sql.gz";

$command ="mysqldump --host='localhost' --user='rick' --password='#*$!x' $database > $filename";


//`/usr/bin/mysqldump -urick -prick $database | gzip > $filename`; // other calls - that don't work
// `mysqldump -urick -prick $database > $filename`;

echo "File is stored in: ". $filename;




 4:26 pm on Apr 26, 2012 (gmt 0)

Backtick (synonymous with shell exec [php.net]) and system [php.net] both have a return value, exec does not. You may be able to use these to your advantage to find out what's wrong.

$retval = system($command,$command_status);
echo "rval $retval status $command_status";

Here's the bits I use for my backup functions, they work fine. System will return null from mysql dump, so you can sub my custom function "error" for "die(""could not make backup: $bu_result");

# connecting remote requires hostname --h
$cmd = "mysqldump -h $db_host --user=$sql_login --password=$sql_pass $db_name > $backup_dir/$file";
$bu_result = system($cmd);
if ($bu_result) { error("could not make backup: $bu_result"); }

$backup_dir **MUST** be the full file system path to the directory (you've already got that.)

Only other thing I can venture is you're using a relative link there, try

url: '/DatabaseBakup.php',

And last . . . this is dangerous.


At the very least,


I'd also do a pre-check to verify the input database exists.


 10:25 pm on Apr 26, 2012 (gmt 0)

I incorporated your hints in my program.

$dir="/home/rick/DB-Web"; // for testing
//$database="accounts"; // ditto
$database="contacts"; // ditto

$format="m.d.y_G.i.s"; // month, day, year _ hr, min, sec

$filename=$dir."/$database". "/".strtoupper ($database)."_DUMP_". date($format).".sql.gz";

$command ="mysqldump --host='localhost' --user='rick' --password='rick' ".$database." | gzip >". $filename;

$bu_result = system($command);

if ($bu_result) {
error("could not make backup: $bu_result");

This is the error that shows up in apache2/error.log.

sh: cannot create /home/rick/DB-Web/contacts/CONTACTS_DUMP_04.26.12_15.20.25.sql.gz: Permission denied
mysqldump: Got errno 32 on write

I copied the contents of $command directly into a mysql command line and it works fine

After 3 days this is making me crazy!


 3:15 pm on Apr 27, 2012 (gmt 0)

Permission denied is pretty obvious - it means the directory you're writing to isn't writable by the process OR it doesn't exist. Play around with file permissions on that directory. or . . .

if (is_dir($dir) and is_writable($dir)) {
// do the dump
else { die("$dir is not writable or doesn't exist"; }


 5:54 pm on Apr 27, 2012 (gmt 0)


Thanks rocknbil, I/We finally got the right combination. The $dir permissions were set to 755 and therefore not writeable. I guess the combination of Apache2 configurations and changing the $dir to 766 permissions fixed the problem.

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