Page 1 of 1

Problem with making a mysql backup using php

Posted: Sat Feb 11, 2012 9:13 am
by MicroBoy
I have a problem while trying to make a backup of mysql using php, I got a script from somewhere on the internet, it saves the file but it is empty, is there anything wrong with the following code or there can be something on my server that does not let to get the backup?

Code: Select all

system("mysqldump -h $dbhost -u $dbuser -p $dbpass $dbname > $backupfile");

Re: Problem with making a mysql backup using php

Posted: Sat Feb 11, 2012 10:17 am
by Celauran
You need to remove the space between -p and $dbpass

Re: Problem with making a mysql backup using php

Posted: Sat Feb 11, 2012 12:31 pm
by MicroBoy
When I do that it creates the sql file blank too and it gives me this in browser:

Code: Select all

Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] For more options, use mysqldump --help

Re: Problem with making a mysql backup using php

Posted: Sat Feb 11, 2012 7:58 pm
by Eric!
It looks ok. Do you have shell access on that system? I usually use this with the special ticks that get run as EXEC shell:

Code: Select all

$output = `mysqldump --opt --host=$host --user=$user --password=$password $current_db $list | gzip > $filename_output.sql.gz`;
You can also run mysqldumps via myphpadmin.

Re: Problem with making a mysql backup using php

Posted: Sun Feb 12, 2012 9:55 am
by MicroBoy
Yep it is a dedicated server so I have full access. What is that $list variable? I want to run through php since I found some scripts on internet that make a backup and send it via email.

Re: Problem with making a mysql backup using php

Posted: Sun Feb 12, 2012 6:49 pm
by MicroBoy
It is really weird, via SSH it worked great. When I write a wrong password in PHP it does not give me a file at all, just when the credentials are correct it give me the blank file.

p.s. is there any way to get the mysql backup through php?

Re: Problem with making a mysql backup using php

Posted: Sun Feb 12, 2012 9:04 pm
by Celauran
If what's working at the command line isn't working via PHP, then I don't know what to tell you. Have you considered using a bash script instead?

Re: Problem with making a mysql backup using php

Posted: Mon Feb 13, 2012 8:36 am
by Eric!
MicroBoy wrote:p.s. is there any way to get the mysql backup through php?
What I posted was how I run MYSQLDUMP with PHP. The special tick quotes cause PHP to run that string in the shell.

Perhaps the directory you're running it in doesn't have permissions for writing when you are in the shell, but via SSH you're user is elevated...? I don't know.

I can't usually run things in the shell on shared hosts, so I use my own PHP backup scripts which also encrypt and FTP the data off site. Probably similar to what you downloaded. The script just uses standard SQL queries to build up a .sql file.

Re: Problem with making a mysql backup using php

Posted: Mon Feb 13, 2012 10:37 am
by MicroBoy
I found a script which make the mysql backup and sends to dropbox, just for information it uses:

Code: Select all

system("mysqldump -h $dbhost -u $dbuser --password='$dbpass' $dbname > $backupfile");
p.s. thanks for your help, best regards.

Re: Problem with making a mysql backup using php

Posted: Tue Feb 14, 2012 12:12 pm
by Eric!
Maybe you don't understand what I posted. These do the same things (mine also GZips the file up). And $output contains the printed results into a PHP variable so you can redisplay them.

Code: Select all

$output = `mysqldump --opt --host=$host --user=$user --password=$password $current_db $list | gzip > $filename_output.sql.gz`;
Is basically the same as the following (just no GZIP or $list where you can select a table out of the database if you want).

Code: Select all

system("mysqldump -h $dbhost -u $dbuser --password='$dbpass' $dbname > $backupfile");
The ticks are called Execution operators http://php.net/manual/en/language.opera ... cution.php

Anyway if either of these two worked then it should have worked for you in the shell.