Problem with making a mysql backup using php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Problem with making a mysql backup using php

Post 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");
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Problem with making a mysql backup using php

Post by Celauran »

You need to remove the space between -p and $dbpass
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Problem with making a mysql backup using php

Post 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
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with making a mysql backup using php

Post 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.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Problem with making a mysql backup using php

Post 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.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Problem with making a mysql backup using php

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Problem with making a mysql backup using php

Post 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?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with making a mysql backup using php

Post 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.
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: Problem with making a mysql backup using php

Post 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.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with making a mysql backup using php

Post 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.
Post Reply